mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Migrated NotFoundRedirectHandlerTest to use PHPUnit mocks
This commit is contained in:
@@ -42,7 +42,7 @@ class QrCodeActionTest extends TestCase
|
|||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
||||||
$delegate = $this->createMock(RequestHandlerInterface::class);
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||||
$delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
|
$delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class RedirectActionTest extends TestCase
|
|||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||||
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||||
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
||||||
$this->requestTracker->expects($this->never())->method('trackIfApplicable');
|
$this->requestTracker->expects($this->never())->method('trackIfApplicable');
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user