From 83352f0af9f88c885a271a022e69070b3e9c33c9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 1 May 2016 17:54:56 +0200 Subject: [PATCH] Added more tests --- src/Service/VisitsTracker.php | 2 +- tests/Factory/CacheFactoryTest.php | 46 +++++++++++++++++++++++++++++ tests/Service/VisitsTrackerTest.php | 30 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tests/Factory/CacheFactoryTest.php create mode 100644 tests/Service/VisitsTrackerTest.php diff --git a/src/Service/VisitsTracker.php b/src/Service/VisitsTracker.php index 80598053..453627d9 100644 --- a/src/Service/VisitsTracker.php +++ b/src/Service/VisitsTracker.php @@ -42,7 +42,7 @@ class VisitsTracker implements VisitsTrackerInterface $visit = new Visit(); $visit->setShortUrl($shortUrl) ->setUserAgent($this->getArrayValue($visitorData, 'HTTP_USER_AGENT')) - ->setReferer($this->getArrayValue($visitorData, 'REFERER')) + ->setReferer($this->getArrayValue($visitorData, 'HTTP_REFERER')) ->setRemoteAddr($this->getArrayValue($visitorData, 'REMOTE_ADDR')); $this->em->persist($visit); $this->em->flush(); diff --git a/tests/Factory/CacheFactoryTest.php b/tests/Factory/CacheFactoryTest.php new file mode 100644 index 00000000..a6920d56 --- /dev/null +++ b/tests/Factory/CacheFactoryTest.php @@ -0,0 +1,46 @@ +factory = new CacheFactory(); + } + + public static function tearDownAfterClass() + { + putenv('APP_ENV'); + } + + /** + * @test + */ + public function productionReturnsApcAdapter() + { + putenv('APP_ENV=pro'); + $instance = $this->factory->__invoke(new ServiceManager(), ''); + $this->assertInstanceOf(ApcuCache::class, $instance); + } + + /** + * @test + */ + public function developmentReturnsArrayAdapter() + { + putenv('APP_ENV=dev'); + $instance = $this->factory->__invoke(new ServiceManager(), ''); + $this->assertInstanceOf(ArrayCache::class, $instance); + } +} diff --git a/tests/Service/VisitsTrackerTest.php b/tests/Service/VisitsTrackerTest.php new file mode 100644 index 00000000..7e6707c7 --- /dev/null +++ b/tests/Service/VisitsTrackerTest.php @@ -0,0 +1,30 @@ +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); + } +}