Passed
Push — master ( c13efe...13a161 )
by Paweł
02:57
created

JsonFormatterTest::testFormatsJsonResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gorynych\Tests\Http\Formatter;
6
7
use Gorynych\Http\Formatter\JsonFormatter;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class JsonFormatterTest extends TestCase
13
{
14
    /**
15
     * @dataProvider provideCases
16
     * @param mixed $content
17
     * @param mixed $expectedContent
18
     */
19
    public function testFormatsJsonResponse($content, $expectedContent, int $statusCode, string $contentType): void
20
    {
21
        $response = (new JsonFormatter())->format($content, $statusCode);
22
23
        $this->assertInstanceOf(JsonResponse::class, $response);
24
        $this->assertJsonStringEqualsJsonString(json_encode($expectedContent), $response->getContent());
25
        $this->assertSame($statusCode, $response->getStatusCode());
26
        $this->assertSame($contentType, $response->headers->get('Content-Type'));
27
    }
28
29
    /**
30
     * @return \Generator<array>
31
     */
32
    public function provideCases(): \Generator
33
    {
34
        yield 'successful response' => [
35
            ['foo' => 'bar'],
36
            ['data' => ['foo' => 'bar']],
37
            Response::HTTP_OK,
38
            'application/json',
39
        ];
40
41
        yield 'client error response' => [
42
            ['foo' => 'bar'],
43
            ['errors' => [['foo' => 'bar']]],
44
            Response::HTTP_BAD_REQUEST,
45
            'application/problem+json',
46
        ];
47
48
        yield 'server error response' => [
49
            ['foo' => 'bar'],
50
            ['errors' => [['foo' => 'bar']]],
51
            Response::HTTP_BAD_GATEWAY,
52
            'application/problem+json',
53
        ];
54
    }
55
}
56