public function setUp() { $this->em = $this->prophesize(EntityManagerInterface::class); $this->em->persist(Argument::any())->willReturn(null); $this->em->flush()->willReturn(null); $this->service = new ShortUrlService($this->em->reveal()); }
/** * @test */ public function saveVisitsPersistsProvidedVisit() { $visit = new Visit(); $this->em->persist($visit)->shouldBeCalledTimes(1); $this->em->flush()->shouldBeCalledTimes(1); $this->visitService->saveVisit($visit); }
/** * @test */ public function keyIsProperlyCreatedWithExpirationDate() { $this->em->flush()->shouldBeCalledTimes(1); $this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledTimes(1); $date = new \DateTime('2030-01-01'); $key = $this->service->create($date); $this->assertSame($date, $key->getExpirationDate()); }
/** * @test */ public function trackUsesForwardedForHeaderIfPresent() { $shortCode = '123ABC'; $test = $this; $repo = $this->prophesize(EntityRepository::class); $repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl()); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1); $this->em->persist(Argument::any())->will(function ($args) use($test) { /** @var Visit $visit */ $visit = $args[0]; $test->assertEquals('4.3.2.1', $visit->getRemoteAddr()); })->shouldBeCalledTimes(1); $this->em->flush()->shouldBeCalledTimes(1); $this->visitsTracker->track($shortCode, ServerRequestFactory::fromGlobals(['REMOTE_ADDR' => '1.2.3.4'])->withHeader('X-Forwarded-For', '4.3.2.1,99.99.99.99')); }
public function setUp() { $this->httpClient = $this->prophesize(ClientInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $conn = $this->prophesize(Connection::class); $conn->isTransactionActive()->willReturn(false); $this->em->getConnection()->willReturn($conn->reveal()); $this->em->flush()->willReturn(null); $this->em->commit()->willReturn(null); $this->em->beginTransaction()->willReturn(null); $this->em->persist(Argument::any())->will(function ($arguments) { /** @var ShortUrl $shortUrl */ $shortUrl = $arguments[0]; $shortUrl->setId(10); }); $repo = $this->prophesize(ObjectRepository::class); $repo->findOneBy(Argument::any())->willReturn(null); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->cache = new ArrayCache(); $this->urlShortener = new UrlShortener($this->httpClient->reveal(), $this->em->reveal(), $this->cache); }