em = $this->prophesize(EntityManager::class); $this->visitService = new VisitService($this->em->reveal()); } /** * @test */ public function locateVisitsIteratesAndLocatesUnlocatedVisits() { $unlocatedVisits = [ [new Visit(new ShortUrl('foo'), Visitor::emptyInstance())], [new Visit(new ShortUrl('bar'), Visitor::emptyInstance())], ]; $repo = $this->prophesize(VisitRepository::class); $findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits); $getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal()); $persist = $this->em->persist(Argument::type(Visit::class))->will(function () { }); $flush = $this->em->flush()->will(function () { }); $clear = $this->em->clear()->will(function () { }); $this->visitService->locateVisits(function () { return []; }, function () { $args = func_get_args(); $this->assertInstanceOf(VisitLocation::class, array_shift($args)); $this->assertInstanceOf(Visit::class, array_shift($args)); }); $findUnlocatedVisits->shouldHaveBeenCalledOnce(); $getRepo->shouldHaveBeenCalledOnce(); $persist->shouldHaveBeenCalledTimes(count($unlocatedVisits)); $flush->shouldHaveBeenCalledTimes(count($unlocatedVisits)); $clear->shouldHaveBeenCalledTimes(count($unlocatedVisits)); } /** * @test */ public function visitsWhichCannotBeLocatedAreIgnored() { $unlocatedVisits = [ [new Visit(new ShortUrl('foo'), Visitor::emptyInstance())], ]; $repo = $this->prophesize(VisitRepository::class); $findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits); $getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal()); $persist = $this->em->persist(Argument::type(Visit::class))->will(function () { }); $flush = $this->em->flush()->will(function () { }); $clear = $this->em->clear()->will(function () { }); $this->visitService->locateVisits(function () { throw new IpCannotBeLocatedException('Cannot be located'); }); $findUnlocatedVisits->shouldHaveBeenCalledOnce(); $getRepo->shouldHaveBeenCalledOnce(); $persist->shouldNotHaveBeenCalled(); $flush->shouldNotHaveBeenCalled(); $clear->shouldNotHaveBeenCalled(); } }