'Testing persisted entities within Phpunit

I'd like to assert that a method creates and persists certain entities. The method's class has an instance of EntityManagerInterface injected into it.

Within my Phpunit test I can do:

    $mockEntityManager = $this->getMockBuilder(EntityManagerInterface::class)
        ->disableOriginalConstructor()
        ->getMock();

and then instantiate the class and execute the method. However how can I check what entities get persisted?

Ideally I would just be able to override $mockEntityManager's persist method with some arbitrary closure, e.g. one which appends to some $expectedPersistedEntities array, then I can make assertions against this array. However it seems overriding methods with custom behaviour is not possible in PHP.

If it was only one object being persisted then I could do e.g.

        $mockEntityManager->expects(self::once())
            ->method('persist')
            ->with($expectedSyncedObject);

however I'm expecting multiple different objects to get persisted and would like to assert all of them do so.

Is this possible?



Solution 1:[1]

You can check every instance being persisted using withConsecutive see the doc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Khali