Migrated DefaultShortCodesLengthMiddlewareTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:55:11 +02:00
parent 23aa7a015c
commit 8f76c3e202

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
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\ShortUrl\Model\Validation\ShortUrlInputFilter; use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
@ -18,14 +16,12 @@ use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DefaultShortCodesLengthMiddleware;
class DefaultShortCodesLengthMiddlewareTest extends TestCase class DefaultShortCodesLengthMiddlewareTest extends TestCase
{ {
use ProphecyTrait;
private DefaultShortCodesLengthMiddleware $middleware; private DefaultShortCodesLengthMiddleware $middleware;
private ObjectProphecy $handler; private MockObject $handler;
protected function setUp(): void protected function setUp(): void
{ {
$this->handler = $this->prophesize(RequestHandlerInterface::class); $this->handler = $this->createMock(RequestHandlerInterface::class);
$this->middleware = new DefaultShortCodesLengthMiddleware(8); $this->middleware = new DefaultShortCodesLengthMiddleware(8);
} }
@ -36,17 +32,17 @@ class DefaultShortCodesLengthMiddlewareTest extends TestCase
public function defaultValueIsInjectedInBodyWhenNotProvided(array $body, int $expectedLength): void public function defaultValueIsInjectedInBodyWhenNotProvided(array $body, int $expectedLength): void
{ {
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body); $request = ServerRequestFactory::fromGlobals()->withParsedBody($body);
$handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) use ($expectedLength) { $this->handler->expects($this->once())->method('handle')->with($this->callback(
function (ServerRequestInterface $req) use ($expectedLength) {
$parsedBody = $req->getParsedBody(); $parsedBody = $req->getParsedBody();
Assert::assertArrayHasKey(ShortUrlInputFilter::SHORT_CODE_LENGTH, $parsedBody); Assert::assertArrayHasKey(ShortUrlInputFilter::SHORT_CODE_LENGTH, $parsedBody);
Assert::assertEquals($expectedLength, $parsedBody[ShortUrlInputFilter::SHORT_CODE_LENGTH]); Assert::assertEquals($expectedLength, $parsedBody[ShortUrlInputFilter::SHORT_CODE_LENGTH]);
return $req; return true;
}))->willReturn(new Response()); },
))->willReturn(new Response());
$this->middleware->process($request, $this->handler->reveal()); $this->middleware->process($request, $this->handler);
$handle->shouldHaveBeenCalledOnce();
} }
public function provideBodies(): iterable public function provideBodies(): iterable