1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ChrisyueAutoJsonResponseBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Chrisyue <http://chrisyue.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Chrisyue\Bundle\AutoJsonResponseBundle\Tests\EventListener; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
15
|
|
|
use Chrisyue\Bundle\AutoJsonResponseBundle\EventListener\AutoJsonResponseListener; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
use Symfony\Component\Serializer\Serializer; |
19
|
|
|
|
20
|
|
|
class AutoJsonResponseListenerTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
public function testOnKernelViewWithNonMasterRequest() |
23
|
|
|
{ |
24
|
|
|
$listener = new AutoJsonResponseListener(); |
25
|
|
|
$listener->onKernelView($this->prophesizeEvent(false)->reveal()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testOnKernelViewWithNonJsonRequest() |
29
|
|
|
{ |
30
|
|
|
$event = $this->prophesizeEvent(); |
31
|
|
|
$request = $this->prophesizeRequest(false); |
32
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
33
|
|
|
|
34
|
|
|
$listener = new AutoJsonResponseListener(); |
35
|
|
|
$listener->onKernelView($event->reveal()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testOnKernelViewWithNullControllerResult() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$event = $this->prophesizeEvent(); |
41
|
|
|
$request = $this->prophesizeRequest(); |
42
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
43
|
|
|
|
44
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn(null); |
45
|
|
|
$event->setResponse(new JsonResponse(null, 204))->shouldBeCalledTimes(1); |
46
|
|
|
|
47
|
|
|
$listener = new AutoJsonResponseListener(); |
48
|
|
|
$listener->onKernelView($event->reveal()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testOnKernelViewWithResponseControllerResult() |
52
|
|
|
{ |
53
|
|
|
$event = $this->prophesizeEvent(); |
54
|
|
|
$request = $this->prophesizeRequest(); |
55
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
56
|
|
|
|
57
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn(new JsonResponse()); |
58
|
|
|
|
59
|
|
|
$listener = new AutoJsonResponseListener(); |
60
|
|
|
$listener->onKernelView($event->reveal()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testOnKernelViewWithNonObjectControllerResult() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$event = $this->prophesizeEvent(); |
66
|
|
|
$request = $this->prophesizeRequest(); |
67
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
68
|
|
|
|
69
|
|
|
$nonObject = ['foo' => 'bar']; |
70
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn($nonObject); |
71
|
|
|
$request->isMethod('POST')->shouldBeCalledTimes(1)->willReturn(false); |
72
|
|
|
|
73
|
|
|
$event->setResponse(new JsonResponse($nonObject))->shouldBeCalledTimes(1); |
74
|
|
|
|
75
|
|
|
$listener = new AutoJsonResponseListener(); |
76
|
|
|
$listener->onKernelView($event->reveal()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testOnKernelViewWithPostRequestAndNonObjectControllerResult() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$event = $this->prophesizeEvent(); |
82
|
|
|
$request = $this->prophesizeRequest(); |
83
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
84
|
|
|
|
85
|
|
|
$nonObject = 'hello world'; |
86
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn($nonObject); |
87
|
|
|
$request->isMethod('POST')->shouldBeCalledTimes(1)->willReturn(true); |
88
|
|
|
|
89
|
|
|
$event->setResponse(new JsonResponse($nonObject, 201))->shouldBeCalledTimes(1); |
90
|
|
|
|
91
|
|
|
$listener = new AutoJsonResponseListener(); |
92
|
|
|
$listener->onKernelView($event->reveal()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @expectedException BadMethodCallException |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function testOnKernelViewWithObjectControllerResultButNoSerializer() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$event = $this->prophesizeEvent(); |
101
|
|
|
$request = $this->prophesizeRequest(); |
102
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
103
|
|
|
|
104
|
|
|
$object = new \stdClass(); |
105
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn($object); |
106
|
|
|
$request->isMethod('POST')->shouldBeCalledTimes(1)->willReturn(false); |
107
|
|
|
|
108
|
|
|
$listener = new AutoJsonResponseListener(); |
109
|
|
|
$listener->onKernelView($event->reveal()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testOnKernelViewWithObjectControllerResult() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$event = $this->prophesizeEvent(); |
115
|
|
|
$request = $this->prophesizeRequest(); |
116
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
117
|
|
|
|
118
|
|
|
$object = new \stdClass(); |
119
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn($object); |
120
|
|
|
$request->isMethod('POST')->shouldBeCalledTimes(1)->willReturn(false); |
121
|
|
|
|
122
|
|
|
$normalized = ['foo' => 'bar']; |
123
|
|
|
$serializer = $this->prophesizeSerializer($object, $normalized); |
124
|
|
|
$listener = new AutoJsonResponseListener($serializer->reveal()); |
125
|
|
|
|
126
|
|
|
$event->setResponse(new JsonResponse($normalized))->shouldBeCalledTimes(1); |
127
|
|
|
|
128
|
|
|
$listener->onKernelView($event->reveal()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
public function testOnKernelViewWithPostRequestAndObjectControllerResult() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$event = $this->prophesizeEvent(); |
134
|
|
|
$request = $this->prophesizeRequest(); |
135
|
|
|
$event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal()); |
136
|
|
|
|
137
|
|
|
$object = new \stdClass(); |
138
|
|
|
$event->getControllerResult()->shouldBeCalledTimes(1)->willReturn($object); |
139
|
|
|
$request->isMethod('POST')->shouldBeCalledTimes(1)->willReturn(true); |
140
|
|
|
|
141
|
|
|
$normalized = ['bar' => 'foo']; |
142
|
|
|
$serializer = $this->prophesizeSerializer($object, $normalized); |
143
|
|
|
$listener = new AutoJsonResponseListener($serializer->reveal()); |
144
|
|
|
|
145
|
|
|
$event->setResponse(new JsonResponse($normalized, 201))->shouldBeCalledTimes(1); |
146
|
|
|
|
147
|
|
|
$listener->onKernelView($event->reveal()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function prophesizeEvent($isMasterRequest = true) |
151
|
|
|
{ |
152
|
|
|
$event = $this->prophesize(GetResponseForControllerResultEvent::class); |
153
|
|
|
$event->isMasterRequest()->shouldBeCalledTimes(1)->willReturn($isMasterRequest); |
154
|
|
|
|
155
|
|
|
return $event; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function prophesizeRequest($isJson = true) |
159
|
|
|
{ |
160
|
|
|
$request = $this->prophesize(Request::class); |
161
|
|
|
$request->getRequestFormat()->shouldBeCalledTimes(1)->willReturn($isJson ? 'json' : '!json'); |
162
|
|
|
|
163
|
|
|
return $request; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function prophesizeSerializer(\stdClass $object, array $normalized) |
167
|
|
|
{ |
168
|
|
|
$serializer = $this->prophesize(Serializer::class); |
169
|
|
|
$serializer->normalize($object)->shouldBeCalledTimes(1)->willReturn($normalized); |
170
|
|
|
|
171
|
|
|
return $serializer; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.