1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Middleware\ShortUrl; |
6
|
|
|
|
7
|
|
|
use Laminas\Diactoros\Response; |
8
|
|
|
use Laminas\Diactoros\ServerRequestFactory; |
9
|
|
|
use PHPUnit\Framework\Assert; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Prophecy\Argument; |
12
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
15
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; |
16
|
|
|
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DefaultShortCodesLengthMiddleware; |
17
|
|
|
|
18
|
|
|
class DefaultShortCodesLengthMiddlewareTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private DefaultShortCodesLengthMiddleware $middleware; |
21
|
|
|
private ObjectProphecy $handler; |
22
|
|
|
|
23
|
|
|
public function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->handler = $this->prophesize(RequestHandlerInterface::class); |
26
|
|
|
$this->middleware = new DefaultShortCodesLengthMiddleware(8); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
* @dataProvider provideBodies |
32
|
|
|
*/ |
33
|
|
|
public function defaultValueIsInjectedInBodyWhenNotProvided(array $body, int $expectedLength): void |
34
|
|
|
{ |
35
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body); |
36
|
|
|
$handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) use ($expectedLength) { |
37
|
|
|
$parsedBody = $req->getParsedBody(); |
38
|
|
|
Assert::assertArrayHasKey(ShortUrlMetaInputFilter::SHORT_CODE_LENGTH, $parsedBody); |
39
|
|
|
Assert::assertEquals($expectedLength, $parsedBody[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH]); |
40
|
|
|
|
41
|
|
|
return $req; |
42
|
|
|
}))->willReturn(new Response()); |
43
|
|
|
|
44
|
|
|
$this->middleware->process($request, $this->handler->reveal()); |
45
|
|
|
|
46
|
|
|
$handle->shouldHaveBeenCalledOnce(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function provideBodies(): iterable |
50
|
|
|
{ |
51
|
|
|
yield 'value provided' => [[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 6], 6]; |
52
|
|
|
yield 'value not provided' => [[], 8]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|