From 5201ea4516dbacc84187c3be41d21ae283e50801 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 24 May 2022 17:54:44 +0200 Subject: [PATCH] Created tests for short-url-visits commands --- .../Domain/GetDomainVisitsCommandTest.php | 71 +++++++++++++++++++ .../ShortUrl/GetShortUrlVisitsCommandTest.php | 2 - .../Command/Tag/GetTagVisitsCommandTest.php | 71 +++++++++++++++++++ .../Visit/GetNonOrphanVisitsCommandTest.php | 70 ++++++++++++++++++ 4 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php create mode 100644 module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php create mode 100644 module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php diff --git a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php new file mode 100644 index 00000000..f94a2000 --- /dev/null +++ b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php @@ -0,0 +1,71 @@ +visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class); + $this->stringifier = $this->prophesize(ShortUrlStringifierInterface::class); + + $this->commandTester = $this->testerForCommand( + new GetDomainVisitsCommand($this->visitsHelper->reveal(), $this->stringifier->reveal()), + ); + } + + /** @test */ + public function outputIsProperlyGenerated(): void + { + $shortUrl = ShortUrl::createEmpty(); + $visit = Visit::forValidShortUrl($shortUrl, new Visitor('bar', 'foo', '', ''))->locate( + VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')), + ); + $domain = 'doma.in'; + $getVisits = $this->visitsHelper->visitsForDomain($domain, Argument::any())->willReturn( + new Paginator(new ArrayAdapter([$visit])), + ); + $stringify = $this->stringifier->stringify($shortUrl)->willReturn('the_short_url'); + + $this->commandTester->execute(['domain' => $domain]); + $output = $this->commandTester->getDisplay(); + + self::assertEquals( + <<getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url | + +---------+---------------------------+------------+---------+--------+---------------+ + + OUTPUT, + $output, + ); + $getVisits->shouldHaveBeenCalledOnce(); + $stringify->shouldHaveBeenCalledOnce(); + } +} diff --git a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php index d5709da6..076eb9b2 100644 --- a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php @@ -114,8 +114,6 @@ class GetShortUrlVisitsCommandTest extends TestCase $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); - echo $output; - self::assertEquals( <<visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class); + $this->stringifier = $this->prophesize(ShortUrlStringifierInterface::class); + + $this->commandTester = $this->testerForCommand( + new GetTagVisitsCommand($this->visitsHelper->reveal(), $this->stringifier->reveal()), + ); + } + + /** @test */ + public function outputIsProperlyGenerated(): void + { + $shortUrl = ShortUrl::createEmpty(); + $visit = Visit::forValidShortUrl($shortUrl, new Visitor('bar', 'foo', '', ''))->locate( + VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')), + ); + $tag = 'abc123'; + $getVisits = $this->visitsHelper->visitsForTag($tag, Argument::any())->willReturn( + new Paginator(new ArrayAdapter([$visit])), + ); + $stringify = $this->stringifier->stringify($shortUrl)->willReturn('the_short_url'); + + $this->commandTester->execute(['tag' => $tag]); + $output = $this->commandTester->getDisplay(); + + self::assertEquals( + <<getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url | + +---------+---------------------------+------------+---------+--------+---------------+ + + OUTPUT, + $output, + ); + $getVisits->shouldHaveBeenCalledOnce(); + $stringify->shouldHaveBeenCalledOnce(); + } +} diff --git a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php new file mode 100644 index 00000000..d6888bf5 --- /dev/null +++ b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php @@ -0,0 +1,70 @@ +visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class); + $this->stringifier = $this->prophesize(ShortUrlStringifierInterface::class); + + $this->commandTester = $this->testerForCommand( + new GetNonOrphanVisitsCommand($this->visitsHelper->reveal(), $this->stringifier->reveal()), + ); + } + + /** @test */ + public function outputIsProperlyGenerated(): void + { + $shortUrl = ShortUrl::createEmpty(); + $visit = Visit::forValidShortUrl($shortUrl, new Visitor('bar', 'foo', '', ''))->locate( + VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')), + ); + $getVisits = $this->visitsHelper->nonOrphanVisits(Argument::any())->willReturn( + new Paginator(new ArrayAdapter([$visit])), + ); + $stringify = $this->stringifier->stringify($shortUrl)->willReturn('the_short_url'); + + $this->commandTester->execute([]); + $output = $this->commandTester->getDisplay(); + + self::assertEquals( + <<getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url | + +---------+---------------------------+------------+---------+--------+---------------+ + + OUTPUT, + $output, + ); + $getVisits->shouldHaveBeenCalledOnce(); + $stringify->shouldHaveBeenCalledOnce(); + } +}