Added NotFoundTrackerMiddlewareTest

This commit is contained in:
Alejandro Celaya 2021-02-08 22:16:15 +01:00
parent 0e165bc7e0
commit d2e0413a48
3 changed files with 98 additions and 7 deletions

View File

@ -11,7 +11,7 @@ use Shlinkio\Shlink\Core\Entity\Visit;
use function rtrim;
final class NotFoundType
class NotFoundType
{
private string $type;

View File

@ -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);
}

View File

@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTrackerMiddleware;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
class NotFoundTrackerMiddlewareTest extends TestCase
{
use ProphecyTrait;
private NotFoundTrackerMiddleware $middleware;
private ServerRequestInterface $request;
private ObjectProphecy $visitsTracker;
private ObjectProphecy $notFoundType;
private ObjectProphecy $handler;
protected function setUp(): void
{
$this->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();
}
}