BastienSander
BastienSander

Reputation: 1838

Symfony - PHPUnit Prophecy not triggering calls

I am trying to make the easiest prophecy test with Symfony and phpunit.

If I put a BreakPoint in the persist method, I can validate that is pass by it.

        $em = $this->prophesize(EntityManagerInterface::class);
        $em->persist(Argument::any())->shouldBeCalledTimes(1);
       

        $realEm = self::$container->get('doctrine.orm.entity_manager');
        $realEm->persist($contract->getSensorConfiguration());

But It always says me it's never called.

Some predictions failed:
Double\EntityManagerInterface\P1:
  Expected exactly 1 calls that match:
      Double\EntityManagerInterface\P1->persist(*)
    but none were made.

I also tried with createMock and expects

        $em = $this->createMock(EntityManager::class);
        $em->expects($this->once())->method('persist');

but I get the same result...

Expectation failed for method name is equal to 'persist' when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

Upvotes: 0

Views: 340

Answers (1)

T. van den Berg
T. van den Berg

Reputation: 364

It looks like you expect $em to be the same as the entitymanager you get from the container. But these are completely different objects.

I do not recommend it but you could first do self::$container->set('doctrine.orm.entity_manager', $em) (not tested). So now every class that uses this service will use you mock.

Upvotes: 2

Related Questions