defaultValueIsInjectedInBodyWhenNotProvided()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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