Completed
Push — develop ( e18187...f7d54a )
by Alejandro
21s queued 12s
created

domainIsDroppedWhenDefaultOneIsProvided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 12
rs 10
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\Prophecy\ObjectProphecy;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware;
16
17
class DropDefaultDomainFromQueryMiddlewareTest extends TestCase
18
{
19
    private DropDefaultDomainFromQueryMiddleware $middleware;
20
    private ObjectProphecy $next;
21
22
    public function setUp(): void
23
    {
24
        $this->next = $this->prophesize(RequestHandlerInterface::class);
25
        $this->middleware = new DropDefaultDomainFromQueryMiddleware('doma.in');
26
    }
27
28
    /**
29
     * @test
30
     * @dataProvider provideQueryParams
31
     */
32
    public function domainIsDroppedWhenDefaultOneIsProvided(array $providedQuery, array $expectedQuery): void
33
    {
34
        $req = ServerRequestFactory::fromGlobals()->withQueryParams($providedQuery);
35
36
        $handle = $this->next->handle(Argument::that(function (ServerRequestInterface $request) use ($expectedQuery) {
37
            Assert::assertEquals($expectedQuery, $request->getQueryParams());
38
            return $request;
39
        }))->willReturn(new Response());
40
41
        $this->middleware->process($req, $this->next->reveal());
42
43
        $handle->shouldHaveBeenCalledOnce();
44
    }
45
46
    public function provideQueryParams(): iterable
47
    {
48
        yield [[], []];
49
        yield [['foo' => 'bar'], ['foo' => 'bar']];
50
        yield [['foo' => 'bar', 'domain' => 'doma.in'], ['foo' => 'bar']];
51
        yield [['foo' => 'bar', 'domain' => 'not_default'], ['foo' => 'bar', 'domain' => 'not_default']];
52
        yield [['domain' => 'doma.in'], []];
53
    }
54
}
55