Example #1
0
 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());
 }
Example #2
0
 /**
  * @test
  */
 public function saveVisitsPersistsProvidedVisit()
 {
     $visit = new Visit();
     $this->em->persist($visit)->shouldBeCalledTimes(1);
     $this->em->flush()->shouldBeCalledTimes(1);
     $this->visitService->saveVisit($visit);
 }
Example #3
0
 /**
  * @test
  * @expectedException \Shlinkio\Shlink\Common\Exception\RuntimeException
  */
 public function exceptionIsThrownWhenOrmThrowsException()
 {
     $conn = $this->prophesize(Connection::class);
     $conn->isTransactionActive()->willReturn(true);
     $this->em->getConnection()->willReturn($conn->reveal());
     $this->em->rollback()->shouldBeCalledTimes(1);
     $this->em->close()->shouldBeCalledTimes(1);
     $this->em->flush()->willThrow(new ORMException());
     $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
 }
Example #4
0
 /**
  * @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'));
 }
Example #5
0
 /**
  * @test
  */
 public function disableReturnsDisabledKeyWhenFOund()
 {
     $key = new ApiKey();
     $repo = $this->prophesize(EntityRepository::class);
     $repo->findOneBy(['key' => '12345'])->willReturn($key)->shouldBeCalledTimes(1);
     $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
     $this->em->flush()->shouldBeCalledTimes(1);
     $this->assertTrue($key->isEnabled());
     $returnedKey = $this->service->disable('12345');
     $this->assertFalse($key->isEnabled());
     $this->assertSame($key, $returnedKey);
 }