From d2e0413a48490d90da2c49e6a605816528ac8a5b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 8 Feb 2021 22:16:15 +0100 Subject: [PATCH] Added NotFoundTrackerMiddlewareTest --- .../src/ErrorHandler/Model/NotFoundType.php | 2 +- .../NotFoundTrackerMiddleware.php | 8 +- .../NotFoundTrackerMiddlewareTest.php | 95 +++++++++++++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php diff --git a/module/Core/src/ErrorHandler/Model/NotFoundType.php b/module/Core/src/ErrorHandler/Model/NotFoundType.php index 7585a3ca..57176e84 100644 --- a/module/Core/src/ErrorHandler/Model/NotFoundType.php +++ b/module/Core/src/ErrorHandler/Model/NotFoundType.php @@ -11,7 +11,7 @@ use Shlinkio\Shlink\Core\Entity\Visit; use function rtrim; -final class NotFoundType +class NotFoundType { private string $type; diff --git a/module/Core/src/ErrorHandler/NotFoundTrackerMiddleware.php b/module/Core/src/ErrorHandler/NotFoundTrackerMiddleware.php index f792dd07..b81e55de 100644 --- a/module/Core/src/ErrorHandler/NotFoundTrackerMiddleware.php +++ b/module/Core/src/ErrorHandler/NotFoundTrackerMiddleware.php @@ -29,13 +29,9 @@ class NotFoundTrackerMiddleware implements MiddlewareInterface if ($notFoundType->isBaseUrl()) { $this->visitsTracker->trackBaseUrlVisit($visitor); - } - - if ($notFoundType->isRegularNotFound()) { + } elseif ($notFoundType->isRegularNotFound()) { $this->visitsTracker->trackRegularNotFoundVisit($visitor); - } - - if ($notFoundType->isInvalidShortUrl()) { + } elseif ($notFoundType->isInvalidShortUrl()) { $this->visitsTracker->trackInvalidShortUrlVisit($visitor); } diff --git a/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php b/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php new file mode 100644 index 00000000..560a2468 --- /dev/null +++ b/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php @@ -0,0 +1,95 @@ +notFoundType = $this->prophesize(NotFoundType::class); + $this->handler = $this->prophesize(RequestHandlerInterface::class); + $this->handler->handle(Argument::cetera())->willReturn(new Response()); + + $this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class); + $this->middleware = new NotFoundTrackerMiddleware($this->visitsTracker->reveal()); + + $this->request = ServerRequestFactory::fromGlobals()->withAttribute( + NotFoundType::class, + $this->notFoundType->reveal(), + ); + } + + /** @test */ + public function baseUrlErrorIsTracked(): void + { + $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(true); + $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false); + $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false); + + $this->middleware->process($this->request, $this->handler->reveal()); + + $isBaseUrl->shouldHaveBeenCalledOnce(); + $isRegularNotFound->shouldNotHaveBeenCalled(); + $isInvalidShortUrl->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function regularNotFoundErrorIsTracked(): void + { + $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false); + $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(true); + $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false); + + $this->middleware->process($this->request, $this->handler->reveal()); + + $isBaseUrl->shouldHaveBeenCalledOnce(); + $isRegularNotFound->shouldHaveBeenCalledOnce(); + $isInvalidShortUrl->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function invalidShortUrlErrorIsTracked(): void + { + $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false); + $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false); + $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(true); + + $this->middleware->process($this->request, $this->handler->reveal()); + + $isBaseUrl->shouldHaveBeenCalledOnce(); + $isRegularNotFound->shouldHaveBeenCalledOnce(); + $isInvalidShortUrl->shouldHaveBeenCalledOnce(); + $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled(); + $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce(); + } +}