DropDefaultDomainFromRequestMiddlewareTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 19
dl 0
loc 39
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A provideQueryParams() 0 7 1
A setUp() 0 4 1
A domainIsDroppedWhenDefaultOneIsProvided() 0 13 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\Rest\Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware;
17
18
class DropDefaultDomainFromRequestMiddlewareTest extends TestCase
19
{
20
    use ProphecyTrait;
21
22
    private DropDefaultDomainFromRequestMiddleware $middleware;
23
    private ObjectProphecy $next;
24
25
    public function setUp(): void
26
    {
27
        $this->next = $this->prophesize(RequestHandlerInterface::class);
28
        $this->middleware = new DropDefaultDomainFromRequestMiddleware('doma.in');
29
    }
30
31
    /**
32
     * @test
33
     * @dataProvider provideQueryParams
34
     */
35
    public function domainIsDroppedWhenDefaultOneIsProvided(array $providedPayload, array $expectedPayload): void
36
    {
37
        $req = ServerRequestFactory::fromGlobals()->withQueryParams($providedPayload)->withParsedBody($providedPayload);
38
39
        $handle = $this->next->handle(Argument::that(function (ServerRequestInterface $request) use ($expectedPayload) {
40
            Assert::assertEquals($expectedPayload, $request->getQueryParams());
41
            Assert::assertEquals($expectedPayload, $request->getParsedBody());
42
            return $request;
43
        }))->willReturn(new Response());
44
45
        $this->middleware->process($req, $this->next->reveal());
46
47
        $handle->shouldHaveBeenCalledOnce();
48
    }
49
50
    public function provideQueryParams(): iterable
51
    {
52
        yield [[], []];
53
        yield [['foo' => 'bar'], ['foo' => 'bar']];
54
        yield [['foo' => 'bar', 'domain' => 'doma.in'], ['foo' => 'bar']];
55
        yield [['foo' => 'bar', 'domain' => 'not_default'], ['foo' => 'bar', 'domain' => 'not_default']];
56
        yield [['domain' => 'doma.in'], []];
57
    }
58
}
59