From ce4877d4ac93931d30d8d8fc951a0e31464d10f0 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Jul 2016 22:55:28 +0200 Subject: [PATCH] Improved VisitsTrackerTest --- .../Core/test/Service/VisitsTrackerTest.php | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/module/Core/test/Service/VisitsTrackerTest.php b/module/Core/test/Service/VisitsTrackerTest.php index 2a441e04..4d8a68fe 100644 --- a/module/Core/test/Service/VisitsTrackerTest.php +++ b/module/Core/test/Service/VisitsTrackerTest.php @@ -5,11 +5,29 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use PHPUnit_Framework_TestCase as TestCase; use Prophecy\Argument; +use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Entity\ShortUrl; +use Shlinkio\Shlink\Core\Entity\Visit; +use Shlinkio\Shlink\Core\Repository\VisitRepository; use Shlinkio\Shlink\Core\Service\VisitsTracker; class VisitsTrackerTest extends TestCase { + /** + * @var VisitsTracker + */ + protected $visitsTracker; + /** + * @var ObjectProphecy + */ + protected $em; + + public function setUp() + { + $this->em = $this->prophesize(EntityManager::class); + $this->visitsTracker = new VisitsTracker($this->em->reveal()); + } + /** * @test */ @@ -19,12 +37,32 @@ class VisitsTrackerTest extends TestCase $repo = $this->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); + $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1); + $this->em->persist(Argument::any())->shouldBeCalledTimes(1); + $this->em->flush()->shouldBeCalledTimes(1); - $visitsTracker = new VisitsTracker($em->reveal()); - $visitsTracker->track($shortCode); + $this->visitsTracker->track($shortCode); + } + + /** + * @test + */ + public function infoReturnsVisistForCertainShortCode() + { + $shortCode = '123ABC'; + $shortUrl = (new ShortUrl())->setOriginalUrl('http://domain.com/foo/bar'); + $repo = $this->prophesize(EntityRepository::class); + $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl); + $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1); + + $list = [ + new Visit(), + new Visit(), + ]; + $repo2 = $this->prophesize(VisitRepository::class); + $repo2->findVisitsByShortUrl($shortUrl, null)->willReturn($list); + $this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledTimes(1); + + $this->assertEquals($list, $this->visitsTracker->info($shortCode)); } }