mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-31 19:26:58 -06:00
Migrated NotFoundRedirectHandlerTest to use PHPUnit mocks
This commit is contained in:
parent
a8514a9ae4
commit
230e56370a
@ -42,7 +42,7 @@ class QrCodeActionTest extends TestCase
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
||||
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
||||
$delegate = $this->createMock(RequestHandlerInterface::class);
|
||||
$delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
|
||||
|
@ -69,7 +69,7 @@ class RedirectActionTest extends TestCase
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
||||
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
||||
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
|
||||
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
||||
$this->requestTracker->expects($this->never())->method('trackIfApplicable');
|
||||
|
||||
|
@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
@ -22,31 +20,25 @@ use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
|
||||
class NotFoundRedirectHandlerTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private NotFoundRedirectHandler $middleware;
|
||||
private NotFoundRedirectOptions $redirectOptions;
|
||||
private ObjectProphecy $resolver;
|
||||
private ObjectProphecy $domainService;
|
||||
private ObjectProphecy $next;
|
||||
private MockObject $resolver;
|
||||
private MockObject $domainService;
|
||||
private MockObject $next;
|
||||
private ServerRequestInterface $req;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->redirectOptions = new NotFoundRedirectOptions();
|
||||
$this->resolver = $this->prophesize(NotFoundRedirectResolverInterface::class);
|
||||
$this->domainService = $this->prophesize(DomainServiceInterface::class);
|
||||
$this->resolver = $this->createMock(NotFoundRedirectResolverInterface::class);
|
||||
$this->domainService = $this->createMock(DomainServiceInterface::class);
|
||||
|
||||
$this->middleware = new NotFoundRedirectHandler(
|
||||
$this->redirectOptions,
|
||||
$this->resolver->reveal(),
|
||||
$this->domainService->reveal(),
|
||||
);
|
||||
$this->middleware = new NotFoundRedirectHandler($this->redirectOptions, $this->resolver, $this->domainService);
|
||||
|
||||
$this->next = $this->prophesize(RequestHandlerInterface::class);
|
||||
$this->next = $this->createMock(RequestHandlerInterface::class);
|
||||
$this->req = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
NotFoundType::class,
|
||||
$this->prophesize(NotFoundType::class)->reveal(),
|
||||
$this->createMock(NotFoundType::class),
|
||||
);
|
||||
}
|
||||
|
||||
@ -59,40 +51,49 @@ class NotFoundRedirectHandlerTest extends TestCase
|
||||
$expectedResp = new Response();
|
||||
|
||||
$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);
|
||||
$handle->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideNonRedirectScenarios(): iterable
|
||||
{
|
||||
yield 'no domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
|
||||
$domainService->findByAuthority(Argument::cetera())
|
||||
->willReturn(null)
|
||||
->shouldBeCalledOnce();
|
||||
$resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
Argument::type(NotFoundRedirectOptions::class),
|
||||
Argument::type(UriInterface::class),
|
||||
)->willReturn(null)->shouldBeCalledOnce();
|
||||
yield 'no domain' => [function (
|
||||
MockObject&DomainServiceInterface $domainService,
|
||||
MockObject&NotFoundRedirectResolverInterface $resolver,
|
||||
): void {
|
||||
$domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
|
||||
null,
|
||||
);
|
||||
$resolver->expects($this->once())->method('resolveRedirectResponse')->with(
|
||||
$this->isInstanceOf(NotFoundType::class),
|
||||
$this->isInstanceOf(NotFoundRedirectOptions::class),
|
||||
$this->isInstanceOf(UriInterface::class),
|
||||
)->willReturn(null);
|
||||
}];
|
||||
yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
|
||||
$domainService->findByAuthority(Argument::cetera())
|
||||
->willReturn(Domain::withAuthority(''))
|
||||
->shouldBeCalledOnce();
|
||||
$resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
Argument::type(NotFoundRedirectOptions::class),
|
||||
Argument::type(UriInterface::class),
|
||||
)->willReturn(null)->shouldBeCalledOnce();
|
||||
$resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
Argument::type(Domain::class),
|
||||
Argument::type(UriInterface::class),
|
||||
)->willReturn(null)->shouldBeCalledOnce();
|
||||
yield 'non-redirecting domain' => [function (
|
||||
MockObject&DomainServiceInterface $domainService,
|
||||
MockObject&NotFoundRedirectResolverInterface $resolver,
|
||||
): void {
|
||||
$domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
|
||||
Domain::withAuthority(''),
|
||||
);
|
||||
$resolver->expects($this->exactly(2))->method('resolveRedirectResponse')->withConsecutive(
|
||||
[
|
||||
$this->isInstanceOf(NotFoundType::class),
|
||||
$this->isInstanceOf(Domain::class),
|
||||
$this->isInstanceOf(UriInterface::class),
|
||||
],
|
||||
[
|
||||
$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();
|
||||
|
||||
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn(null);
|
||||
$resolveRedirect = $this->resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
$this->redirectOptions,
|
||||
Argument::type(UriInterface::class),
|
||||
$this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(null);
|
||||
$this->resolver->expects($this->once())->method('resolveRedirectResponse')->with(
|
||||
$this->isInstanceOf(NotFoundType::class),
|
||||
$this->equalTo($this->redirectOptions),
|
||||
$this->isInstanceOf(UriInterface::class),
|
||||
)->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);
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$resolveRedirect->shouldHaveBeenCalledOnce();
|
||||
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@ -122,18 +121,18 @@ class NotFoundRedirectHandlerTest extends TestCase
|
||||
$expectedResp = new Response();
|
||||
$domain = Domain::withAuthority('');
|
||||
|
||||
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn($domain);
|
||||
$resolveRedirect = $this->resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
$this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
|
||||
$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);
|
||||
$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);
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$resolveRedirect->shouldHaveBeenCalledOnce();
|
||||
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user