DefaultShortCodesLengthMiddlewareTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 17
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

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