Created RequestTracker test

This commit is contained in:
Alejandro Celaya 2021-07-15 17:43:29 +02:00
parent 050f83e3bb
commit 0096a778ac
3 changed files with 147 additions and 84 deletions

View File

@ -4,10 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action;
use Fig\Http\Message\RequestMethodInterface;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
@ -72,14 +70,6 @@ class RedirectActionTest extends TestCase
$track->shouldHaveBeenCalledOnce();
}
// public function provideQueries(): iterable
// {
// yield [[]];
// yield [['foobar' => 'notrack']];
// yield [['foobar' => 'barfoo']];
// yield [['foobar' => null]];
// }
/** @test */
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
{
@ -97,27 +87,4 @@ class RedirectActionTest extends TestCase
$handle->shouldHaveBeenCalledOnce();
}
// /** @test */
// public function trackingIsDisabledWhenRequestIsForwardedFromHead(): void
// {
// $shortCode = 'abc123';
// $shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
// $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn($shortUrl);
// $track = $this->requestTracker->trackIfApplicable(Argument::cetera())->will(function (): void {
// });
// $buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn(
// new Response\RedirectResponse(''),
// );
//
// $request = (new ServerRequest())->withAttribute('shortCode', $shortCode)
// ->withAttribute(
// ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE,
// RequestMethodInterface::METHOD_HEAD,
// );
// $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
//
// $buildResp->shouldHaveBeenCalled();
// $track->shouldNotHaveBeenCalled();
// }
}

View File

@ -49,55 +49,4 @@ class NotFoundTrackerMiddlewareTest extends TestCase
$this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce();
$this->handler->handle($this->request)->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();
// }
}

View File

@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Visit;
use Fig\Http\Message\RequestMethodInterface;
use Laminas\Diactoros\ServerRequestFactory;
use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\Core\Visit\RequestTracker;
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
class RequestTrackerTest extends TestCase
{
use ProphecyTrait;
private const LONG_URL = 'https://domain.com/foo/bar?some=thing';
private RequestTracker $requestTracker;
private ObjectProphecy $visitsTracker;
private ObjectProphecy $notFoundType;
private ServerRequestInterface $request;
protected function setUp(): void
{
$this->notFoundType = $this->prophesize(NotFoundType::class);
$this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
$this->requestTracker = new RequestTracker(
$this->visitsTracker->reveal(),
new TrackingOptions(['disable_track_param' => 'foobar']),
);
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
NotFoundType::class,
$this->notFoundType->reveal(),
);
}
/**
* @test
* @dataProvider provideNonTrackingRequests
*/
public function trackingIsDisabledWhenRequestDoesNotMeetConditions(ServerRequestInterface $request): void
{
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
$this->requestTracker->trackIfApplicable($shortUrl, $request);
$this->visitsTracker->track(Argument::cetera())->shouldNotHaveBeenCalled();
}
public function provideNonTrackingRequests(): iterable
{
yield 'forwarded from head' => [ServerRequestFactory::fromGlobals()->withAttribute(
ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE,
RequestMethodInterface::METHOD_HEAD,
)];
yield 'disable track param' => [ServerRequestFactory::fromGlobals()->withQueryParams(['foobar' => 'foo'])];
yield 'disable track param as null' => [
ServerRequestFactory::fromGlobals()->withQueryParams(['foobar' => null]),
];
}
/** @test */
public function trackingHappensOverShortUrlsWhenRequestMeetsConditions(): void
{
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
$this->requestTracker->trackIfApplicable($shortUrl, $this->request);
$this->visitsTracker->track($shortUrl, 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->requestTracker->trackNotFoundIfApplicable($this->request);
$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->requestTracker->trackNotFoundIfApplicable($this->request);
$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->requestTracker->trackNotFoundIfApplicable($this->request);
$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
* @dataProvider provideNonTrackingRequests
*/
public function notFoundIsNotTrackedIfRequestDoesNotMeetConditions(ServerRequestInterface $request): void
{
$this->requestTracker->trackNotFoundIfApplicable($request);
$this->visitsTracker->trackBaseUrlVisit(Argument::cetera())->shouldNotHaveBeenCalled();
$this->visitsTracker->trackRegularNotFoundVisit(Argument::cetera())->shouldNotHaveBeenCalled();
$this->visitsTracker->trackInvalidShortUrlVisit(Argument::cetera())->shouldNotHaveBeenCalled();
}
}