Completed
Pull Request — master (#148)
by Alejandro
04:16
created

CreateShortCodeContentNegotiationMiddlewareTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ handle() 0 4 1
A setUp() 0 13 1
A properResponseIsReturned() 0 11 2
A provideData() 0 12 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortCode;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Shlinkio\Shlink\Rest\Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware;
11
use Zend\Diactoros\Response\JsonResponse;
12
use Zend\Diactoros\ServerRequestFactory;
13
14
class CreateShortCodeContentNegotiationMiddlewareTest extends TestCase
15
{
16
    /**
17
     * @var CreateShortCodeContentNegotiationMiddleware
18
     */
19
    private $middleware;
20
    /**
21
     * @var RequestHandlerInterface
22
     */
23
    private $requestHandler;
24
25
    public function setUp()
26
    {
27
        $this->middleware = new CreateShortCodeContentNegotiationMiddleware();
28
        $this->requestHandler = new class implements RequestHandlerInterface {
29
            /**
30
             * Handle the request and return a response.
31
             */
32
            public function handle(ServerRequestInterface $request): ResponseInterface
33
            {
34
                return new JsonResponse(['shortUrl' => 'http://doma.in/foo']);
35
            }
36
        };
37
    }
38
39
    /**
40
     * @test
41
     * @dataProvider provideData
42
     * @param array $query
43
     */
44
    public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType)
45
    {
46
        $request = ServerRequestFactory::fromGlobals()->withQueryParams($query);
47
        if ($accept !== null) {
48
            $request = $request->withHeader('Accept', $accept);
49
        }
50
51
        $response = $this->middleware->process($request, $this->requestHandler);
52
53
        $this->assertEquals($expectedContentType, $response->getHeaderLine('Content-type'));
54
    }
55
56
    public function provideData(): array
57
    {
58
        return [
59
            [null, [], 'application/json'],
60
            [null, ['format' => 'json'], 'application/json'],
61
            [null, ['format' => 'invalid'], 'application/json'],
62
            [null, ['format' => 'txt'], 'text/plain'],
63
            ['application/json', [], 'application/json'],
64
            ['application/xml', [], 'application/json'],
65
            ['text/plain', [], 'text/plain'],
66
        ];
67
    }
68
}
69