prophesize(EntityRepository::class); $repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl()); $em = $this->prophesize(EntityManager::class); $em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1); $em->persist(Argument::any())->shouldBeCalledTimes(1); $em->flush()->shouldBeCalledTimes(1); $visitsTracker = new VisitsTracker($em->reveal()); $visitsTracker->track($shortCode); } }