So a very simple test: a method persisting some objects gets an array of data.
class A
{
public function __construct(private BucketRepository $repository){}
public function saveObjects(array $data)
{
foreach ($data as $row) {
$this->repository->persist(new Bucket($data['amount']));
}
$this->repository->flush();
}
}
How would I test that all calls of the ‘persist’ method are called with the right Bucket object?
In my test code I’d have something like this
public function testSaveObjects()
{
$mock = $this->createMock(BucketRepository::class);
$subject = new A($mock);
// Something here to check it correctly
$mock->expects($this->exactly(3))
->method('persist')
->with(....)
$subject->saveObjects([
[
'amount' => 2
],
[
'amount' => 6
],
[
'amount' => 9
]
];
}
How do I assert that at the first call, the Bucket has an amount of 2, and at the second an amount of 6. I’d need to somehow get the actual argument object of the call at the n-th index and then verify it. But I see no way to do it in either PHPUnit or Mockery.
PHPunit used to have the at
matcher, which was literally perfect but that is deprecated and unavailable now in newer versions.