Wrapped logic to track requests to a new RequestTracker service

This commit is contained in:
Alejandro Celaya
2021-07-15 17:23:09 +02:00
parent 32f7b4fbf6
commit 050f83e3bb
10 changed files with 194 additions and 163 deletions

View File

@@ -14,8 +14,7 @@ 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;
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class NotFoundTrackerMiddlewareTest extends TestCase
{
@@ -23,7 +22,7 @@ class NotFoundTrackerMiddlewareTest extends TestCase
private NotFoundTrackerMiddleware $middleware;
private ServerRequestInterface $request;
private ObjectProphecy $visitsTracker;
private ObjectProphecy $requestTracker;
private ObjectProphecy $notFoundType;
private ObjectProphecy $handler;
@@ -33,8 +32,8 @@ class NotFoundTrackerMiddlewareTest extends TestCase
$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->requestTracker = $this->prophesize(RequestTrackerInterface::class);
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker->reveal());
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
NotFoundType::class,
@@ -43,53 +42,62 @@ class NotFoundTrackerMiddlewareTest extends TestCase
}
/** @test */
public function baseUrlErrorIsTracked(): void
public function delegatesIntoRequestTracker(): 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();
$this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce();
$this->handler->handle($this->request)->shouldHaveBeenCalledOnce();
}
/** @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();
}
// /** @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();
// }
}