RequestTransformerListenerTest::createRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace NW\JsonRequestBundle\Tests\EventListener;
4
5
use NW\JsonRequestBundle\Tests\TestCase;
6
use NW\JsonRequestBundle\EventListener\RequestTransformerListener;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpKernel\Event\RequestEvent;
9
10
class RequestTransformerListenerTest extends TestCase
11
{
12
    /**
13
     * @var RequestTransformerListener
14
     */
15
    private $listener;
16
17
    public function setUp(): void
18
    {
19
        $this->listener = new RequestTransformerListener();
20
    }
21
22
    /**
23
     * @param string $contentType
24
     *
25
     * @dataProvider jsonContentTypes
26
     */
27
    public function testTransformRequest(string $contentType): void
28
    {
29
        $data = ['foo' => 'bar'];
30
        $request = $this->createRequest($contentType, \json_encode($data));
31
        $event = $this->createGetResponseEventMock($request);
32
33
        $this->listener->onKernelRequest($event);
34
35
        $this->assertEquals($data, $event->getRequest()->request->all());
36
        $this->assertNull($event->getResponse());
37
    }
38
39
    /**
40
     * @param string $contentType
41
     *
42
     * @dataProvider jsonContentTypes
43
     */
44
    public function testDoNotTransformRequestContainingScalarJsonValue(string $contentType): void
45
    {
46
        $content = json_encode('foo');
47
        $request = $this->createRequest($contentType, $content);
48
        $event = $this->createGetResponseEventMock($request);
49
50
        $this->listener->onKernelRequest($event);
51
52
        $this->assertEquals([], $event->getRequest()->request->all());
53
        $this->assertEquals($content, $event->getRequest()->getContent());
54
    }
55
56
    public function testBadRequestResponse(): void
57
    {
58
        $request = $this->createRequest('application/json', '{maaan}');
59
        $event = $this->createGetResponseEventMock($request);
60
61
        $this->listener->onKernelRequest($event);
62
63
        $this->assertEquals(400, $event->getResponse()->getStatusCode());
64
    }
65
66
    /**
67
     * @param string $contentType
68
     *
69
     * @dataProvider notJsonContentTypes
70
     */
71
    public function testNotTransformOtherContentType(string $contentType): void
72
    {
73
        $request = $this->createRequest($contentType, 'some=body');
74
        $event = $this->createGetResponseEventMock($request);
75
76
        $this->listener->onKernelRequest($event);
77
78
        $this->assertEquals($request, $event->getRequest());
79
        $this->assertNull($event->getResponse());
80
    }
81
82
    public function testNotReplaceRequestData(): void
83
    {
84
        $request = $this->createRequest('application/json', '');
85
        $event = $this->createGetResponseEventMock($request);
86
87
        $this->listener->onKernelRequest($event);
88
89
        $this->assertEquals($request, $event->getRequest());
90
        $this->assertNull($event->getResponse());
91
    }
92
93
    public function testNotReplaceRequestDataIfNullContent(): void
94
    {
95
        $request = $this->createRequest('application/json', 'null');
96
        $event = $this->createGetResponseEventMock($request);
97
98
        $this->listener->onKernelRequest($event);
99
100
        $this->assertEquals($request, $event->getRequest());
101
        $this->assertNull($event->getResponse());
102
    }
103
104
    private function createGetResponseEventMock(Request $request): RequestEvent
105
    {
106
        $event = $this
107
            ->getMockBuilder(RequestEvent::class)
108
            ->disableOriginalConstructor()
109
            ->setMethods(['getRequest'])
110
            ->getMock();
111
112
        $event->expects($this->any())->method('getRequest')->will($this->returnValue($request));
113
114
        return $event;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $event returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Symfony\Component\HttpKernel\Event\RequestEvent.
Loading history...
115
    }
116
117
    private function createRequest(string $contentType, string $content): Request
118
    {
119
        $request = new Request([], [], [], [], [], [], $content);
120
        $request->headers->set('CONTENT_TYPE', $contentType);
121
122
        return $request;
123
    }
124
125
    public function jsonContentTypes(): array
126
    {
127
        return [
128
            ['application/json'],
129
            ['application/x-json'],
130
        ];
131
    }
132
133
    public function notJsonContentTypes(): array
134
    {
135
        return [
136
            ['application/x-www-form-urlencoded'],
137
            ['text/html'],
138
            ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
139
        ];
140
    }
141
}
142