Completed
Pull Request — master (#148)
by Alejandro
03:40
created

provideData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortCode;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
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;
12
use Zend\Diactoros\Response\JsonResponse;
13
use Zend\Diactoros\ServerRequestFactory;
14
15
class CreateShortCodeContentNegotiationMiddlewareTest extends TestCase
16
{
17
    /**
18
     * @var CreateShortCodeContentNegotiationMiddleware
19
     */
20
    private $middleware;
21
    /**
22
     * @var RequestHandlerInterface
23
     */
24
    private $requestHandler;
25
26
    public function setUp()
27
    {
28
        $this->middleware = new CreateShortCodeContentNegotiationMiddleware();
29
        $this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function whenNoJsonResponseIsReturnedNoFurtherOperationsArePerformed()
36
    {
37
        $expectedResp = new Response();
38
        $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn($expectedResp);
39
40
        $resp = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->requestHandler->reveal());
41
42
        $this->assertSame($expectedResp, $resp);
43
    }
44
45
    /**
46
     * @test
47
     * @dataProvider provideData
48
     * @param array $query
49
     */
50
    public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType)
51
    {
52
        $request = ServerRequestFactory::fromGlobals()->withQueryParams($query);
53
        if ($accept !== null) {
54
            $request = $request->withHeader('Accept', $accept);
55
        }
56
57
        $handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
58
            new JsonResponse(['shortUrl' => 'http://doma.in/foo'])
59
        );
60
61
        $response = $this->middleware->process($request, $this->requestHandler->reveal());
62
63
        $this->assertEquals($expectedContentType, $response->getHeaderLine('Content-type'));
64
        $handle->shouldHaveBeenCalled();
65
    }
66
67
    public function provideData(): array
68
    {
69
        return [
70
            [null, [], 'application/json'],
71
            [null, ['format' => 'json'], 'application/json'],
72
            [null, ['format' => 'invalid'], 'application/json'],
73
            [null, ['format' => 'txt'], 'text/plain'],
74
            ['application/json', [], 'application/json'],
75
            ['application/xml', [], 'application/json'],
76
            ['text/plain', [], 'text/plain'],
77
        ];
78
    }
79
80
    /**
81
     * @test
82
     * @dataProvider provideTextBodies
83
     * @param array $json
84
     */
85
    public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody)
86
    {
87
        $request = ServerRequestFactory::fromGlobals()->withQueryParams(['format' => 'txt']);
88
89
        $handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
90
            new JsonResponse($json)
91
        );
92
93
        $response = $this->middleware->process($request, $this->requestHandler->reveal());
94
95
        $this->assertEquals($expectedBody, (string) $response->getBody());
96
        $handle->shouldHaveBeenCalled();
97
    }
98
99
    public function provideTextBodies(): array
100
    {
101
        return [
102
            [['shortUrl' => 'foobar'], 'foobar'],
103
            [['error' => 'FOO_BAR'], 'FOO_BAR'],
104
            [[], ''],
105
        ];
106
    }
107
}
108