mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Merge pull request #1581 from acelaya-forks/feature/phpunit-mocks
Feature/phpunit mocks
This commit is contained in:
commit
92ddd2eebe
@ -5,10 +5,8 @@ declare(strict_types=1);
|
|||||||
namespace ShlinkioTest\Shlink\Core\Action;
|
namespace ShlinkioTest\Shlink\Core\Action;
|
||||||
|
|
||||||
use Laminas\Diactoros\ServerRequest;
|
use Laminas\Diactoros\ServerRequest;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Shlinkio\Shlink\Common\Response\PixelResponse;
|
use Shlinkio\Shlink\Common\Response\PixelResponse;
|
||||||
use Shlinkio\Shlink\Core\Action\PixelAction;
|
use Shlinkio\Shlink\Core\Action\PixelAction;
|
||||||
@ -19,32 +17,29 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
|||||||
|
|
||||||
class PixelActionTest extends TestCase
|
class PixelActionTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private PixelAction $action;
|
private PixelAction $action;
|
||||||
private ObjectProphecy $urlResolver;
|
private MockObject $urlResolver;
|
||||||
private ObjectProphecy $requestTracker;
|
private MockObject $requestTracker;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
||||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
|
||||||
|
|
||||||
$this->action = new PixelAction($this->urlResolver->reveal(), $this->requestTracker->reveal());
|
$this->action = new PixelAction($this->urlResolver, $this->requestTracker);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function imageIsReturned(): void
|
public function imageIsReturned(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlResolver->resolveEnabledShortUrl(
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'))
|
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'));
|
||||||
->shouldBeCalledOnce();
|
$this->requestTracker->expects($this->once())->method('trackIfApplicable')->withAnyParameters();
|
||||||
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldBeCalledOnce();
|
|
||||||
|
|
||||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||||
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
|
||||||
|
|
||||||
self::assertInstanceOf(PixelResponse::class, $response);
|
self::assertInstanceOf(PixelResponse::class, $response);
|
||||||
self::assertEquals(200, $response->getStatusCode());
|
self::assertEquals(200, $response->getStatusCode());
|
||||||
|
@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Core\Action;
|
|||||||
use Laminas\Diactoros\Response;
|
use Laminas\Diactoros\Response;
|
||||||
use Laminas\Diactoros\ServerRequest;
|
use Laminas\Diactoros\ServerRequest;
|
||||||
use Laminas\Diactoros\ServerRequestFactory;
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Psr\Log\NullLogger;
|
use Psr\Log\NullLogger;
|
||||||
@ -29,50 +27,43 @@ use function imagecreatefromstring;
|
|||||||
|
|
||||||
class QrCodeActionTest extends TestCase
|
class QrCodeActionTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private const WHITE = 0xFFFFFF;
|
private const WHITE = 0xFFFFFF;
|
||||||
private const BLACK = 0x0;
|
private const BLACK = 0x0;
|
||||||
|
|
||||||
private ObjectProphecy $urlResolver;
|
private MockObject $urlResolver;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
->willThrow(ShortUrlNotFoundException::class)
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
->shouldBeCalledOnce();
|
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||||
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
$delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
|
||||||
|
|
||||||
$this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
|
$this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate);
|
||||||
|
|
||||||
$process->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
->willReturn(ShortUrl::createEmpty())
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
->shouldBeCalledOnce();
|
)->willReturn(ShortUrl::createEmpty());
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||||
|
$delegate->expects($this->never())->method('handle');
|
||||||
|
|
||||||
$resp = $this->action()->process(
|
$resp = $this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate);
|
||||||
(new ServerRequest())->withAttribute('shortCode', $shortCode),
|
|
||||||
$delegate->reveal(),
|
|
||||||
);
|
|
||||||
|
|
||||||
self::assertInstanceOf(QrCodeResponse::class, $resp);
|
self::assertInstanceOf(QrCodeResponse::class, $resp);
|
||||||
self::assertEquals(200, $resp->getStatusCode());
|
self::assertEquals(200, $resp->getStatusCode());
|
||||||
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,13 +76,13 @@ class QrCodeActionTest extends TestCase
|
|||||||
string $expectedContentType,
|
string $expectedContentType,
|
||||||
): void {
|
): void {
|
||||||
$code = 'abc123';
|
$code = 'abc123';
|
||||||
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
|
$this->urlResolver->method('resolveEnabledShortUrl')->with(
|
||||||
ShortUrl::createEmpty(),
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($code, '')),
|
||||||
);
|
)->willReturn(ShortUrl::createEmpty());
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||||
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
|
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
|
||||||
|
|
||||||
$resp = $this->action(new QrCodeOptions(format: $defaultFormat))->process($req, $delegate->reveal());
|
$resp = $this->action(new QrCodeOptions(format: $defaultFormat))->process($req, $delegate);
|
||||||
|
|
||||||
self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
|
self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
|
||||||
}
|
}
|
||||||
@ -118,12 +109,12 @@ class QrCodeActionTest extends TestCase
|
|||||||
int $expectedSize,
|
int $expectedSize,
|
||||||
): void {
|
): void {
|
||||||
$code = 'abc123';
|
$code = 'abc123';
|
||||||
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
|
$this->urlResolver->method('resolveEnabledShortUrl')->with(
|
||||||
ShortUrl::createEmpty(),
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($code, '')),
|
||||||
);
|
)->willReturn(ShortUrl::createEmpty());
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||||
|
|
||||||
$resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $delegate->reveal());
|
$resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $delegate);
|
||||||
[$size] = getimagesizefromstring($resp->getBody()->__toString());
|
[$size] = getimagesizefromstring($resp->getBody()->__toString());
|
||||||
|
|
||||||
self::assertEquals($expectedSize, $size);
|
self::assertEquals($expectedSize, $size);
|
||||||
@ -209,12 +200,12 @@ class QrCodeActionTest extends TestCase
|
|||||||
->withQueryParams(['size' => 250, 'roundBlockSize' => $roundBlockSize])
|
->withQueryParams(['size' => 250, 'roundBlockSize' => $roundBlockSize])
|
||||||
->withAttribute('shortCode', $code);
|
->withAttribute('shortCode', $code);
|
||||||
|
|
||||||
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
|
$this->urlResolver->method('resolveEnabledShortUrl')->with(
|
||||||
ShortUrl::withLongUrl('https://shlink.io'),
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($code, '')),
|
||||||
);
|
)->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||||
|
|
||||||
$resp = $this->action($defaultOptions)->process($req, $delegate->reveal());
|
$resp = $this->action($defaultOptions)->process($req, $delegate);
|
||||||
$image = imagecreatefromstring($resp->getBody()->__toString());
|
$image = imagecreatefromstring($resp->getBody()->__toString());
|
||||||
$color = imagecolorat($image, 1, 1);
|
$color = imagecolorat($image, 1, 1);
|
||||||
|
|
||||||
@ -246,7 +237,7 @@ class QrCodeActionTest extends TestCase
|
|||||||
public function action(?QrCodeOptions $options = null): QrCodeAction
|
public function action(?QrCodeOptions $options = null): QrCodeAction
|
||||||
{
|
{
|
||||||
return new QrCodeAction(
|
return new QrCodeAction(
|
||||||
$this->urlResolver->reveal(),
|
$this->urlResolver,
|
||||||
new ShortUrlStringifier(['domain' => 'doma.in']),
|
new ShortUrlStringifier(['domain' => 'doma.in']),
|
||||||
new NullLogger(),
|
new NullLogger(),
|
||||||
$options ?? new QrCodeOptions(),
|
$options ?? new QrCodeOptions(),
|
||||||
|
@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Core\Action;
|
|||||||
|
|
||||||
use Laminas\Diactoros\Response;
|
use Laminas\Diactoros\Response;
|
||||||
use Laminas\Diactoros\ServerRequest;
|
use Laminas\Diactoros\ServerRequest;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
@ -22,29 +20,27 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
|||||||
|
|
||||||
class RedirectActionTest extends TestCase
|
class RedirectActionTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private const LONG_URL = 'https://domain.com/foo/bar?some=thing';
|
private const LONG_URL = 'https://domain.com/foo/bar?some=thing';
|
||||||
|
|
||||||
private RedirectAction $action;
|
private RedirectAction $action;
|
||||||
private ObjectProphecy $urlResolver;
|
private MockObject $urlResolver;
|
||||||
private ObjectProphecy $requestTracker;
|
private MockObject $requestTracker;
|
||||||
private ObjectProphecy $redirectRespHelper;
|
private MockObject $redirectRespHelper;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
||||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
|
||||||
$this->redirectRespHelper = $this->prophesize(RedirectResponseHelperInterface::class);
|
$this->redirectRespHelper = $this->createMock(RedirectResponseHelperInterface::class);
|
||||||
|
|
||||||
$redirectBuilder = $this->prophesize(ShortUrlRedirectionBuilderInterface::class);
|
$redirectBuilder = $this->createMock(ShortUrlRedirectionBuilderInterface::class);
|
||||||
$redirectBuilder->buildShortUrlRedirect(Argument::cetera())->willReturn(self::LONG_URL);
|
$redirectBuilder->method('buildShortUrlRedirect')->withAnyParameters()->willReturn(self::LONG_URL);
|
||||||
|
|
||||||
$this->action = new RedirectAction(
|
$this->action = new RedirectAction(
|
||||||
$this->urlResolver->reveal(),
|
$this->urlResolver,
|
||||||
$this->requestTracker->reveal(),
|
$this->requestTracker,
|
||||||
$redirectBuilder->reveal(),
|
$redirectBuilder,
|
||||||
$this->redirectRespHelper->reveal(),
|
$this->redirectRespHelper,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,38 +49,34 @@ class RedirectActionTest extends TestCase
|
|||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
||||||
$shortCodeToUrl = $this->urlResolver->resolveEnabledShortUrl(
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
)->willReturn($shortUrl);
|
)->willReturn($shortUrl);
|
||||||
$track = $this->requestTracker->trackIfApplicable(Argument::cetera())->will(function (): void {
|
$this->requestTracker->expects($this->once())->method('trackIfApplicable');
|
||||||
});
|
|
||||||
$expectedResp = new Response\RedirectResponse(self::LONG_URL);
|
$expectedResp = new Response\RedirectResponse(self::LONG_URL);
|
||||||
$buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn($expectedResp);
|
$this->redirectRespHelper->expects($this->once())->method('buildRedirectResponse')->with(
|
||||||
|
$this->equalTo(self::LONG_URL),
|
||||||
|
)->willReturn($expectedResp);
|
||||||
|
|
||||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||||
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
|
||||||
|
|
||||||
self::assertSame($expectedResp, $response);
|
self::assertSame($expectedResp, $response);
|
||||||
$buildResp->shouldHaveBeenCalledOnce();
|
|
||||||
$shortCodeToUrl->shouldHaveBeenCalledOnce();
|
|
||||||
$track->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
|
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
->willThrow(ShortUrlNotFoundException::class)
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
->shouldBeCalledOnce();
|
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
||||||
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldNotBeCalled();
|
$this->requestTracker->expects($this->never())->method('trackIfApplicable');
|
||||||
|
|
||||||
$handler = $this->prophesize(RequestHandlerInterface::class);
|
$handler = $this->createMock(RequestHandlerInterface::class);
|
||||||
$handle = $handler->handle(Argument::any())->willReturn(new Response());
|
$handler->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
|
||||||
|
|
||||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||||
$this->action->process($request, $handler->reveal());
|
$this->action->process($request, $handler);
|
||||||
|
|
||||||
$handle->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,8 @@ use Laminas\Diactoros\ServerRequestFactory;
|
|||||||
use Laminas\Diactoros\Uri;
|
use Laminas\Diactoros\Uri;
|
||||||
use Mezzio\Router\Route;
|
use Mezzio\Router\Route;
|
||||||
use Mezzio\Router\RouteResult;
|
use Mezzio\Router\RouteResult;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
use Psr\Http\Server\MiddlewareInterface;
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
@ -25,15 +23,13 @@ use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
|||||||
|
|
||||||
class NotFoundRedirectResolverTest extends TestCase
|
class NotFoundRedirectResolverTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private NotFoundRedirectResolver $resolver;
|
private NotFoundRedirectResolver $resolver;
|
||||||
private ObjectProphecy $helper;
|
private MockObject $helper;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->helper = $this->prophesize(RedirectResponseHelperInterface::class);
|
$this->helper = $this->createMock(RedirectResponseHelperInterface::class);
|
||||||
$this->resolver = new NotFoundRedirectResolver($this->helper->reveal(), new NullLogger());
|
$this->resolver = new NotFoundRedirectResolver($this->helper, new NullLogger());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,12 +43,13 @@ class NotFoundRedirectResolverTest extends TestCase
|
|||||||
string $expectedRedirectTo,
|
string $expectedRedirectTo,
|
||||||
): void {
|
): void {
|
||||||
$expectedResp = new Response();
|
$expectedResp = new Response();
|
||||||
$buildResp = $this->helper->buildRedirectResponse($expectedRedirectTo)->willReturn($expectedResp);
|
$this->helper->expects($this->once())->method('buildRedirectResponse')->with(
|
||||||
|
$this->equalTo($expectedRedirectTo),
|
||||||
|
)->willReturn($expectedResp);
|
||||||
|
|
||||||
$resp = $this->resolver->resolveRedirectResponse($notFoundType, $redirectConfig, $uri);
|
$resp = $this->resolver->resolveRedirectResponse($notFoundType, $redirectConfig, $uri);
|
||||||
|
|
||||||
self::assertSame($expectedResp, $resp);
|
self::assertSame($expectedResp, $resp);
|
||||||
$buildResp->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideRedirects(): iterable
|
public function provideRedirects(): iterable
|
||||||
@ -119,11 +116,11 @@ class NotFoundRedirectResolverTest extends TestCase
|
|||||||
public function noResponseIsReturnedIfNoConditionsMatch(): void
|
public function noResponseIsReturnedIfNoConditionsMatch(): void
|
||||||
{
|
{
|
||||||
$notFoundType = $this->notFoundType($this->requestForRoute('foo'));
|
$notFoundType = $this->notFoundType($this->requestForRoute('foo'));
|
||||||
|
$this->helper->expects($this->never())->method('buildRedirectResponse');
|
||||||
|
|
||||||
$result = $this->resolver->resolveRedirectResponse($notFoundType, new NotFoundRedirectOptions(), new Uri());
|
$result = $this->resolver->resolveRedirectResponse($notFoundType, new NotFoundRedirectOptions(), new Uri());
|
||||||
|
|
||||||
self::assertNull($result);
|
self::assertNull($result);
|
||||||
$this->helper->buildRedirectResponse(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function notFoundType(ServerRequestInterface $req): NotFoundType
|
private function notFoundType(ServerRequestInterface $req): NotFoundType
|
||||||
@ -139,7 +136,7 @@ class NotFoundRedirectResolverTest extends TestCase
|
|||||||
RouteResult::fromRoute(
|
RouteResult::fromRoute(
|
||||||
new Route(
|
new Route(
|
||||||
'',
|
'',
|
||||||
$this->prophesize(MiddlewareInterface::class)->reveal(),
|
$this->createMock(MiddlewareInterface::class),
|
||||||
['GET'],
|
['GET'],
|
||||||
$routeName,
|
$routeName,
|
||||||
),
|
),
|
||||||
|
@ -5,39 +5,35 @@ declare(strict_types=1);
|
|||||||
namespace ShlinkioTest\Shlink\Core\Crawling;
|
namespace ShlinkioTest\Shlink\Core\Crawling;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Shlinkio\Shlink\Core\Crawling\CrawlingHelper;
|
use Shlinkio\Shlink\Core\Crawling\CrawlingHelper;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
||||||
|
|
||||||
class CrawlingHelperTest extends TestCase
|
class CrawlingHelperTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private CrawlingHelper $helper;
|
private CrawlingHelper $helper;
|
||||||
private ObjectProphecy $em;
|
private MockObject $em;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||||
$this->helper = new CrawlingHelper($this->em->reveal());
|
$this->helper = new CrawlingHelper($this->em);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function listCrawlableShortCodesDelegatesIntoRepository(): void
|
public function listCrawlableShortCodesDelegatesIntoRepository(): void
|
||||||
{
|
{
|
||||||
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
$repo = $this->createMock(ShortUrlRepositoryInterface::class);
|
||||||
$findCrawlableShortCodes = $repo->findCrawlableShortCodes()->willReturn([]);
|
$repo->expects($this->once())->method('findCrawlableShortCodes')->willReturn([]);
|
||||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
|
||||||
|
$repo,
|
||||||
|
);
|
||||||
|
|
||||||
$result = $this->helper->listCrawlableShortCodes();
|
$result = $this->helper->listCrawlableShortCodes();
|
||||||
foreach ($result as $shortCode) {
|
foreach ($result as $shortCode) {
|
||||||
// Result is a generator and therefore, it needs to be iterated
|
// $result is a generator and therefore, it needs to be iterated
|
||||||
}
|
}
|
||||||
|
|
||||||
$findCrawlableShortCodes->shouldHaveBeenCalledOnce();
|
|
||||||
$getRepo->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,8 @@ declare(strict_types=1);
|
|||||||
namespace ShlinkioTest\Shlink\Core\Domain;
|
namespace ShlinkioTest\Shlink\Core\Domain;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig;
|
use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig;
|
||||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||||
use Shlinkio\Shlink\Core\Domain\DomainService;
|
use Shlinkio\Shlink\Core\Domain\DomainService;
|
||||||
@ -22,15 +20,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|||||||
|
|
||||||
class DomainServiceTest extends TestCase
|
class DomainServiceTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private DomainService $domainService;
|
private DomainService $domainService;
|
||||||
private ObjectProphecy $em;
|
private MockObject $em;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||||
$this->domainService = new DomainService($this->em->reveal(), 'default.com');
|
$this->domainService = new DomainService($this->em, 'default.com');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,15 +35,15 @@ class DomainServiceTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void
|
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void
|
||||||
{
|
{
|
||||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
||||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
$repo->expects($this->once())->method('findDomains')->with($this->equalTo($apiKey))->willReturn($domains);
|
||||||
$findDomains = $repo->findDomains($apiKey)->willReturn($domains);
|
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
|
||||||
|
$repo,
|
||||||
|
);
|
||||||
|
|
||||||
$result = $this->domainService->listDomains($apiKey);
|
$result = $this->domainService->listDomains($apiKey);
|
||||||
|
|
||||||
self::assertEquals($expectedResult, $result);
|
self::assertEquals($expectedResult, $result);
|
||||||
$getRepo->shouldHaveBeenCalledOnce();
|
|
||||||
$findDomains->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideExcludedDomains(): iterable
|
public function provideExcludedDomains(): iterable
|
||||||
@ -109,10 +105,12 @@ class DomainServiceTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function getDomainThrowsExceptionWhenDomainIsNotFound(): void
|
public function getDomainThrowsExceptionWhenDomainIsNotFound(): void
|
||||||
{
|
{
|
||||||
$find = $this->em->find(Domain::class, '123')->willReturn(null);
|
$this->em->expects($this->once())->method('find')->with(
|
||||||
|
$this->equalTo(Domain::class),
|
||||||
|
$this->equalTo('123'),
|
||||||
|
)->willReturn(null);
|
||||||
|
|
||||||
$this->expectException(DomainNotFoundException::class);
|
$this->expectException(DomainNotFoundException::class);
|
||||||
$find->shouldBeCalledOnce();
|
|
||||||
|
|
||||||
$this->domainService->getDomain('123');
|
$this->domainService->getDomain('123');
|
||||||
}
|
}
|
||||||
@ -121,12 +119,14 @@ class DomainServiceTest extends TestCase
|
|||||||
public function getDomainReturnsEntityWhenFound(): void
|
public function getDomainReturnsEntityWhenFound(): void
|
||||||
{
|
{
|
||||||
$domain = Domain::withAuthority('');
|
$domain = Domain::withAuthority('');
|
||||||
$find = $this->em->find(Domain::class, '123')->willReturn($domain);
|
$this->em->expects($this->once())->method('find')->with(
|
||||||
|
$this->equalTo(Domain::class),
|
||||||
|
$this->equalTo('123'),
|
||||||
|
)->willReturn($domain);
|
||||||
|
|
||||||
$result = $this->domainService->getDomain('123');
|
$result = $this->domainService->getDomain('123');
|
||||||
|
|
||||||
self::assertSame($domain, $result);
|
self::assertSame($domain, $result);
|
||||||
$find->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -136,20 +136,23 @@ class DomainServiceTest extends TestCase
|
|||||||
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
||||||
{
|
{
|
||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
||||||
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain);
|
$repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
|
||||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
$foundDomain,
|
||||||
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
|
);
|
||||||
$flush = $this->em->flush();
|
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
|
||||||
|
$repo,
|
||||||
|
);
|
||||||
|
$this->em->expects($this->once())->method('persist')->with(
|
||||||
|
$foundDomain !== null ? $this->equalTo($foundDomain) : $this->isInstanceOf(Domain::class),
|
||||||
|
);
|
||||||
|
$this->em->expects($this->once())->method('flush');
|
||||||
|
|
||||||
$result = $this->domainService->getOrCreate($authority, $apiKey);
|
$result = $this->domainService->getOrCreate($authority, $apiKey);
|
||||||
|
|
||||||
if ($foundDomain !== null) {
|
if ($foundDomain !== null) {
|
||||||
self::assertSame($result, $foundDomain);
|
self::assertSame($result, $foundDomain);
|
||||||
}
|
}
|
||||||
$getRepo->shouldHaveBeenCalledOnce();
|
|
||||||
$persist->shouldHaveBeenCalledOnce();
|
|
||||||
$flush->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@ -158,14 +161,17 @@ class DomainServiceTest extends TestCase
|
|||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$domain = Domain::withAuthority($authority)->setId('1');
|
$domain = Domain::withAuthority($authority)->setId('1');
|
||||||
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
||||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
||||||
$repo->findOneByAuthority($authority, $apiKey)->willReturn(null);
|
$repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
|
||||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
null,
|
||||||
|
);
|
||||||
|
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
|
||||||
|
$repo,
|
||||||
|
);
|
||||||
|
$this->em->expects($this->never())->method('persist');
|
||||||
|
$this->em->expects($this->never())->method('flush');
|
||||||
|
|
||||||
$this->expectException(DomainNotFoundException::class);
|
$this->expectException(DomainNotFoundException::class);
|
||||||
$getRepo->shouldBeCalledOnce();
|
|
||||||
$this->em->persist(Argument::cetera())->shouldNotBeCalled();
|
|
||||||
$this->em->flush()->shouldNotBeCalled();
|
|
||||||
|
|
||||||
$this->domainService->getOrCreate($authority, $apiKey);
|
$this->domainService->getOrCreate($authority, $apiKey);
|
||||||
}
|
}
|
||||||
@ -177,11 +183,17 @@ class DomainServiceTest extends TestCase
|
|||||||
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
||||||
{
|
{
|
||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
||||||
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain);
|
$repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
|
||||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
$foundDomain,
|
||||||
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
|
);
|
||||||
$flush = $this->em->flush();
|
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
|
||||||
|
$repo,
|
||||||
|
);
|
||||||
|
$this->em->expects($this->once())->method('persist')->with(
|
||||||
|
$foundDomain !== null ? $this->equalTo($foundDomain) : $this->isInstanceOf(Domain::class),
|
||||||
|
);
|
||||||
|
$this->em->expects($this->once())->method('flush');
|
||||||
|
|
||||||
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
|
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
|
||||||
'foo.com',
|
'foo.com',
|
||||||
@ -195,9 +207,6 @@ class DomainServiceTest extends TestCase
|
|||||||
self::assertEquals('foo.com', $result->baseUrlRedirect());
|
self::assertEquals('foo.com', $result->baseUrlRedirect());
|
||||||
self::assertEquals('bar.com', $result->regular404Redirect());
|
self::assertEquals('bar.com', $result->regular404Redirect());
|
||||||
self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
|
self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
|
||||||
$getRepo->shouldHaveBeenCalledOnce();
|
|
||||||
$persist->shouldHaveBeenCalledOnce();
|
|
||||||
$flush->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideFoundDomains(): iterable
|
public function provideFoundDomains(): iterable
|
||||||
|
@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
|||||||
|
|
||||||
use Laminas\Diactoros\Response;
|
use Laminas\Diactoros\Response;
|
||||||
use Laminas\Diactoros\ServerRequestFactory;
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
@ -22,31 +20,25 @@ use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
|||||||
|
|
||||||
class NotFoundRedirectHandlerTest extends TestCase
|
class NotFoundRedirectHandlerTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private NotFoundRedirectHandler $middleware;
|
private NotFoundRedirectHandler $middleware;
|
||||||
private NotFoundRedirectOptions $redirectOptions;
|
private NotFoundRedirectOptions $redirectOptions;
|
||||||
private ObjectProphecy $resolver;
|
private MockObject $resolver;
|
||||||
private ObjectProphecy $domainService;
|
private MockObject $domainService;
|
||||||
private ObjectProphecy $next;
|
private MockObject $next;
|
||||||
private ServerRequestInterface $req;
|
private ServerRequestInterface $req;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->redirectOptions = new NotFoundRedirectOptions();
|
$this->redirectOptions = new NotFoundRedirectOptions();
|
||||||
$this->resolver = $this->prophesize(NotFoundRedirectResolverInterface::class);
|
$this->resolver = $this->createMock(NotFoundRedirectResolverInterface::class);
|
||||||
$this->domainService = $this->prophesize(DomainServiceInterface::class);
|
$this->domainService = $this->createMock(DomainServiceInterface::class);
|
||||||
|
|
||||||
$this->middleware = new NotFoundRedirectHandler(
|
$this->middleware = new NotFoundRedirectHandler($this->redirectOptions, $this->resolver, $this->domainService);
|
||||||
$this->redirectOptions,
|
|
||||||
$this->resolver->reveal(),
|
|
||||||
$this->domainService->reveal(),
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->next = $this->prophesize(RequestHandlerInterface::class);
|
$this->next = $this->createMock(RequestHandlerInterface::class);
|
||||||
$this->req = ServerRequestFactory::fromGlobals()->withAttribute(
|
$this->req = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||||
NotFoundType::class,
|
NotFoundType::class,
|
||||||
$this->prophesize(NotFoundType::class)->reveal(),
|
$this->createMock(NotFoundType::class),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,40 +51,49 @@ class NotFoundRedirectHandlerTest extends TestCase
|
|||||||
$expectedResp = new Response();
|
$expectedResp = new Response();
|
||||||
|
|
||||||
$setUp($this->domainService, $this->resolver);
|
$setUp($this->domainService, $this->resolver);
|
||||||
$handle = $this->next->handle($this->req)->willReturn($expectedResp);
|
$this->next->expects($this->once())->method('handle')->with($this->equalTo($this->req))->willReturn(
|
||||||
|
$expectedResp,
|
||||||
|
);
|
||||||
|
|
||||||
$result = $this->middleware->process($this->req, $this->next->reveal());
|
$result = $this->middleware->process($this->req, $this->next);
|
||||||
|
|
||||||
self::assertSame($expectedResp, $result);
|
self::assertSame($expectedResp, $result);
|
||||||
$handle->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideNonRedirectScenarios(): iterable
|
public function provideNonRedirectScenarios(): iterable
|
||||||
{
|
{
|
||||||
yield 'no domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
|
yield 'no domain' => [function (
|
||||||
$domainService->findByAuthority(Argument::cetera())
|
MockObject&DomainServiceInterface $domainService,
|
||||||
->willReturn(null)
|
MockObject&NotFoundRedirectResolverInterface $resolver,
|
||||||
->shouldBeCalledOnce();
|
): void {
|
||||||
$resolver->resolveRedirectResponse(
|
$domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
|
||||||
Argument::type(NotFoundType::class),
|
null,
|
||||||
Argument::type(NotFoundRedirectOptions::class),
|
);
|
||||||
Argument::type(UriInterface::class),
|
$resolver->expects($this->once())->method('resolveRedirectResponse')->with(
|
||||||
)->willReturn(null)->shouldBeCalledOnce();
|
$this->isInstanceOf(NotFoundType::class),
|
||||||
|
$this->isInstanceOf(NotFoundRedirectOptions::class),
|
||||||
|
$this->isInstanceOf(UriInterface::class),
|
||||||
|
)->willReturn(null);
|
||||||
}];
|
}];
|
||||||
yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
|
yield 'non-redirecting domain' => [function (
|
||||||
$domainService->findByAuthority(Argument::cetera())
|
MockObject&DomainServiceInterface $domainService,
|
||||||
->willReturn(Domain::withAuthority(''))
|
MockObject&NotFoundRedirectResolverInterface $resolver,
|
||||||
->shouldBeCalledOnce();
|
): void {
|
||||||
$resolver->resolveRedirectResponse(
|
$domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
|
||||||
Argument::type(NotFoundType::class),
|
Domain::withAuthority(''),
|
||||||
Argument::type(NotFoundRedirectOptions::class),
|
);
|
||||||
Argument::type(UriInterface::class),
|
$resolver->expects($this->exactly(2))->method('resolveRedirectResponse')->withConsecutive(
|
||||||
)->willReturn(null)->shouldBeCalledOnce();
|
[
|
||||||
$resolver->resolveRedirectResponse(
|
$this->isInstanceOf(NotFoundType::class),
|
||||||
Argument::type(NotFoundType::class),
|
$this->isInstanceOf(Domain::class),
|
||||||
Argument::type(Domain::class),
|
$this->isInstanceOf(UriInterface::class),
|
||||||
Argument::type(UriInterface::class),
|
],
|
||||||
)->willReturn(null)->shouldBeCalledOnce();
|
[
|
||||||
|
$this->isInstanceOf(NotFoundType::class),
|
||||||
|
$this->isInstanceOf(NotFoundRedirectOptions::class),
|
||||||
|
$this->isInstanceOf(UriInterface::class),
|
||||||
|
],
|
||||||
|
)->willReturn(null);
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,19 +102,17 @@ class NotFoundRedirectHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$expectedResp = new Response();
|
$expectedResp = new Response();
|
||||||
|
|
||||||
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn(null);
|
$this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(null);
|
||||||
$resolveRedirect = $this->resolver->resolveRedirectResponse(
|
$this->resolver->expects($this->once())->method('resolveRedirectResponse')->with(
|
||||||
Argument::type(NotFoundType::class),
|
$this->isInstanceOf(NotFoundType::class),
|
||||||
$this->redirectOptions,
|
$this->equalTo($this->redirectOptions),
|
||||||
Argument::type(UriInterface::class),
|
$this->isInstanceOf(UriInterface::class),
|
||||||
)->willReturn($expectedResp);
|
)->willReturn($expectedResp);
|
||||||
|
$this->next->expects($this->never())->method('handle');
|
||||||
|
|
||||||
$result = $this->middleware->process($this->req, $this->next->reveal());
|
$result = $this->middleware->process($this->req, $this->next);
|
||||||
|
|
||||||
self::assertSame($expectedResp, $result);
|
self::assertSame($expectedResp, $result);
|
||||||
$findDomain->shouldHaveBeenCalledOnce();
|
|
||||||
$resolveRedirect->shouldHaveBeenCalledOnce();
|
|
||||||
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@ -122,18 +121,18 @@ class NotFoundRedirectHandlerTest extends TestCase
|
|||||||
$expectedResp = new Response();
|
$expectedResp = new Response();
|
||||||
$domain = Domain::withAuthority('');
|
$domain = Domain::withAuthority('');
|
||||||
|
|
||||||
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn($domain);
|
$this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
|
||||||
$resolveRedirect = $this->resolver->resolveRedirectResponse(
|
|
||||||
Argument::type(NotFoundType::class),
|
|
||||||
$domain,
|
$domain,
|
||||||
Argument::type(UriInterface::class),
|
);
|
||||||
|
$this->resolver->expects($this->once())->method('resolveRedirectResponse')->with(
|
||||||
|
$this->isInstanceOf(NotFoundType::class),
|
||||||
|
$this->equalTo($domain),
|
||||||
|
$this->isInstanceOf(UriInterface::class),
|
||||||
)->willReturn($expectedResp);
|
)->willReturn($expectedResp);
|
||||||
|
$this->next->expects($this->never())->method('handle');
|
||||||
|
|
||||||
$result = $this->middleware->process($this->req, $this->next->reveal());
|
$result = $this->middleware->process($this->req, $this->next);
|
||||||
|
|
||||||
self::assertSame($expectedResp, $result);
|
self::assertSame($expectedResp, $result);
|
||||||
$findDomain->shouldHaveBeenCalledOnce();
|
|
||||||
$resolveRedirect->shouldHaveBeenCalledOnce();
|
|
||||||
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ class NotFoundTemplateHandlerTest extends TestCase
|
|||||||
RouteResult::fromRoute(
|
RouteResult::fromRoute(
|
||||||
new Route(
|
new Route(
|
||||||
'',
|
'',
|
||||||
$this->prophesize(MiddlewareInterface::class)->reveal(),
|
$this->createMock(MiddlewareInterface::class),
|
||||||
['GET'],
|
['GET'],
|
||||||
RedirectAction::class,
|
RedirectAction::class,
|
||||||
),
|
),
|
||||||
|
@ -4,12 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
||||||
|
|
||||||
use Laminas\Diactoros\Response;
|
|
||||||
use Laminas\Diactoros\ServerRequestFactory;
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||||
@ -18,35 +15,31 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
|||||||
|
|
||||||
class NotFoundTrackerMiddlewareTest extends TestCase
|
class NotFoundTrackerMiddlewareTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private NotFoundTrackerMiddleware $middleware;
|
private NotFoundTrackerMiddleware $middleware;
|
||||||
private ServerRequestInterface $request;
|
private ServerRequestInterface $request;
|
||||||
private ObjectProphecy $requestTracker;
|
private MockObject $handler;
|
||||||
private ObjectProphecy $notFoundType;
|
private MockObject $requestTracker;
|
||||||
private ObjectProphecy $handler;
|
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->notFoundType = $this->prophesize(NotFoundType::class);
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
||||||
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
|
||||||
$this->handler->handle(Argument::cetera())->willReturn(new Response());
|
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker);
|
||||||
|
|
||||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
|
||||||
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker->reveal());
|
|
||||||
|
|
||||||
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
|
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||||
NotFoundType::class,
|
NotFoundType::class,
|
||||||
$this->notFoundType->reveal(),
|
$this->createMock(NotFoundType::class),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function delegatesIntoRequestTracker(): void
|
public function delegatesIntoRequestTracker(): void
|
||||||
{
|
{
|
||||||
$this->middleware->process($this->request, $this->handler->reveal());
|
$this->handler->expects($this->once())->method('handle')->with($this->equalTo($this->request));
|
||||||
|
$this->requestTracker->expects($this->once())->method('trackNotFoundIfApplicable')->with(
|
||||||
|
$this->equalTo($this->request),
|
||||||
|
);
|
||||||
|
|
||||||
$this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce();
|
$this->middleware->process($this->request, $this->handler);
|
||||||
$this->handler->handle($this->request)->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
|||||||
use Laminas\Diactoros\Response;
|
use Laminas\Diactoros\Response;
|
||||||
use Laminas\Diactoros\ServerRequestFactory;
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
use PHPUnit\Framework\Assert;
|
use PHPUnit\Framework\Assert;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||||
@ -18,30 +16,28 @@ use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTypeResolverMiddleware;
|
|||||||
|
|
||||||
class NotFoundTypeResolverMiddlewareTest extends TestCase
|
class NotFoundTypeResolverMiddlewareTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private NotFoundTypeResolverMiddleware $middleware;
|
private NotFoundTypeResolverMiddleware $middleware;
|
||||||
private ObjectProphecy $handler;
|
private MockObject $handler;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->middleware = new NotFoundTypeResolverMiddleware('');
|
$this->middleware = new NotFoundTypeResolverMiddleware('');
|
||||||
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function notFoundTypeIsAddedToRequest(): void
|
public function notFoundTypeIsAddedToRequest(): void
|
||||||
{
|
{
|
||||||
$request = ServerRequestFactory::fromGlobals();
|
$request = ServerRequestFactory::fromGlobals();
|
||||||
$handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) {
|
$this->handler->expects($this->once())->method('handle')->with(
|
||||||
Assert::assertArrayHasKey(NotFoundType::class, $req->getAttributes());
|
$this->callback(function (ServerRequestInterface $req): bool {
|
||||||
|
Assert::assertArrayHasKey(NotFoundType::class, $req->getAttributes());
|
||||||
|
return true;
|
||||||
|
}),
|
||||||
|
)->willReturn(new Response());
|
||||||
|
|
||||||
return true;
|
$this->middleware->process($request, $this->handler);
|
||||||
}))->willReturn(new Response());
|
|
||||||
|
|
||||||
$this->middleware->process($request, $this->handler->reveal());
|
|
||||||
|
|
||||||
self::assertArrayNotHasKey(NotFoundType::class, $request->getAttributes());
|
self::assertArrayNotHasKey(NotFoundType::class, $request->getAttributes());
|
||||||
$handle->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user