mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-22 23:23:42 -06:00
Migrated RedirectActionTest to use PHPUnit mocks
This commit is contained in:
parent
cd4b632d75
commit
a8f8297131
@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user