Completed
Pull Request — master (#213)
by Alejandro
04:40
created

provideData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
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\ShortUrl\CreateShortUrlContentNegotiationMiddleware;
11
use Zend\Diactoros\Response;
12
use Zend\Diactoros\Response\JsonResponse;
13
use Zend\Diactoros\ServerRequestFactory;
14
15
class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
16
{
17
    /**
18
     * @var CreateShortUrlContentNegotiationMiddleware
19
     */
20
    private $middleware;
21
    /**
22
     * @var RequestHandlerInterface
23
     */
24
    private $requestHandler;
25
26
    public function setUp()
27
    {
28
        $this->middleware = new CreateShortUrlContentNegotiationMiddleware();
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
            ['application/json', ['format' => 'txt'], 'text/plain'],
78
        ];
79
    }
80
81
    /**
82
     * @test
83
     * @dataProvider provideTextBodies
84
     * @param array $json
85
     */
86
    public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody)
87
    {
88
        $request = ServerRequestFactory::fromGlobals()->withQueryParams(['format' => 'txt']);
89
90
        $handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
91
            new JsonResponse($json)
92
        );
93
94
        $response = $this->middleware->process($request, $this->requestHandler->reveal());
95
96
        $this->assertEquals($expectedBody, (string) $response->getBody());
97
        $handle->shouldHaveBeenCalled();
98
    }
99
100
    public function provideTextBodies(): array
101
    {
102
        return [
103
            [['shortUrl' => 'foobar'], 'foobar'],
104
            [['error' => 'FOO_BAR'], 'FOO_BAR'],
105
            [[], ''],
106
        ];
107
    }
108
}
109