Passed
Pull Request — master (#653)
by Aleksei
06:27
created

PlainRendererTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 57
dl 0
loc 119
rs 10
c 2
b 1
f 0
wmc 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Framework\Http\ErrorHandler;
6
7
use GuzzleHttp\Psr7\Response;
8
use Laminas\Diactoros\ServerRequest;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ResponseFactoryInterface;
11
use Spiral\Http\ErrorHandler\PlainRenderer;
12
13
/**
14
 * @coversDefaultClass \Spiral\Http\ErrorHandler\PlainRenderer
15
 */
16
class PlainRendererTest extends TestCase
17
{
18
    public function testContentTypeApplicationJson(): void
19
    {
20
        $renderer = new PlainRenderer($this->mockResponseFactory());
21
        $request = new ServerRequest([], [], null, null, 'php://input', [
22
            'Accept' => 'application/json',
23
        ]);
24
25
        $response = $renderer->renderException($request, 400, 'message');
26
        self::assertTrue($response->hasHeader('Content-Type'));
27
        self::assertSame(['application/json; charset=UTF-8'], $response->getHeader('Content-Type'));
28
29
        $stream = $response->getBody();
30
        $stream->rewind();
31
        self::assertJsonStringEqualsJsonString('{"status": 400, "error": "message"}', $stream->getContents());
32
    }
33
34
    public function testNoAcceptHeader(): void
35
    {
36
        $renderer = new PlainRenderer($this->mockResponseFactory());
37
        $request = new ServerRequest([], [], null, null, 'php://input');
38
39
        $response = $renderer->renderException($request, 400, 'message');
40
        $stream = $response->getBody();
41
        $stream->rewind();
42
        self::assertEquals('Error code: 400', $stream->getContents());
43
    }
44
45
    /**
46
     * @dataProvider dataResponseIsJson
47
     */
48
    public function testResponseIsJson(): void
49
    {
50
        $renderer = new PlainRenderer($this->mockResponseFactory());
51
        $request = new ServerRequest([], [], null, null, 'php://input', [
52
            'Accept' => [
53
                'application/json',
54
                'application/json',
55
            ],
56
        ]);
57
58
        $response = $renderer->renderException($request, 400, 'message');
59
        self::assertTrue($response->hasHeader('Content-Type'));
60
        self::assertSame(['application/json; charset=UTF-8'], $response->getHeader('Content-Type'));
61
62
        $stream = $response->getBody();
63
        $stream->rewind();
64
        self::assertJsonStringEqualsJsonString('{"status": 400, "error": "message"}', $stream->getContents());
65
    }
66
67
    public function dataResponseIsJson(): iterable
68
    {
69
        yield [
70
            'application/json',
71
        ];
72
73
        //Client and Server set `Accept` header each
74
        yield [
75
            ['application/json', 'application/json'],
76
        ];
77
78
        yield [
79
            'application/json, text/html;q=0.9, */*;q=0.8',
80
        ];
81
82
        yield [
83
            [
84
                'application/json',
85
                'text/html, application/json;q=0.9, */*;q=0.8',
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * @dataProvider dataResponseIsPlain
92
     */
93
    public function testResponseIsPlain($acceptHeader): void
94
    {
95
        $renderer = new PlainRenderer($this->mockResponseFactory());
96
        $request = new ServerRequest([], [], null, null, 'php://input', [
97
            'Accept' => $acceptHeader,
98
        ]);
99
100
        $response = $renderer->renderException($request, 400, 'message');
101
        $stream = $response->getBody();
102
        $stream->rewind();
103
        self::assertEquals('Error code: 400', $stream->getContents());
104
    }
105
106
    public function dataResponseIsPlain(): iterable
107
    {
108
        //Accept header contains several mime types with `q` values. JSON is not prioritized
109
        yield [
110
            'text/html, application/json;q=0.9, */*;q=0.8',
111
        ];
112
113
        yield [
114
            [
115
                'text/html, application/json;q=0.9, */*;q=0.8',
116
                'application/json',
117
            ],
118
        ];
119
120
        yield [
121
            'text/html, application/json, */*;q=0.9',
122
        ];
123
    }
124
125
    private function mockResponseFactory(): ResponseFactoryInterface
126
    {
127
        $responseFactory = $this->createMock(ResponseFactoryInterface::class);
128
        $responseFactory
129
            ->expects(self::once())
130
            ->method('createResponse')
131
            ->willReturnCallback(static function () {
132
                return new Response();
133
            });
134
        return $responseFactory;
135
    }
136
}
137