14
14
use Symfony \Bundle \FrameworkBundle \Tests \TestCase ;
15
15
use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
16
16
use Symfony \Component \DependencyInjection \ContainerInterface ;
17
+ use Symfony \Component \HttpFoundation \JsonResponse ;
17
18
use Symfony \Component \HttpFoundation \Request ;
18
19
use Symfony \Component \HttpFoundation \RequestStack ;
19
20
use Symfony \Component \HttpFoundation \Response ;
20
21
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
21
22
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
22
23
use Symfony \Component \Security \Core \User \User ;
24
+ use Symfony \Component \Serializer \SerializerInterface ;
23
25
24
26
class ControllerTest extends TestCase
25
27
{
@@ -124,6 +126,85 @@ private function getContainerWithTokenStorage($token = null)
124
126
125
127
return $ container ;
126
128
}
129
+
130
+ public function testJson ()
131
+ {
132
+ $ container = $ this ->getMock (ContainerInterface::class);
133
+ $ container
134
+ ->expects ($ this ->once ())
135
+ ->method ('has ' )
136
+ ->with ('serializer ' )
137
+ ->will ($ this ->returnValue (false ));
138
+
139
+ $ controller = new TestController ();
140
+ $ controller ->setContainer ($ container );
141
+
142
+ $ response = $ controller ->json (array ());
143
+ $ this ->assertInstanceOf (JsonResponse::class, $ response );
144
+ $ this ->assertEquals ('[] ' , $ response ->getContent ());
145
+ }
146
+
147
+ public function testJsonWithSerializer ()
148
+ {
149
+ $ container = $ this ->getMock (ContainerInterface::class);
150
+ $ container
151
+ ->expects ($ this ->once ())
152
+ ->method ('has ' )
153
+ ->with ('serializer ' )
154
+ ->will ($ this ->returnValue (true ));
155
+
156
+ $ serializer = $ this ->getMock (SerializerInterface::class);
157
+ $ serializer
158
+ ->expects ($ this ->once ())
159
+ ->method ('serialize ' )
160
+ ->with (array (), 'json ' , array ('json_encode_options ' => JsonResponse::DEFAULT_ENCODING_OPTIONS ))
161
+ ->will ($ this ->returnValue ('[] ' ));
162
+
163
+ $ container
164
+ ->expects ($ this ->once ())
165
+ ->method ('get ' )
166
+ ->with ('serializer ' )
167
+ ->will ($ this ->returnValue ($ serializer ));
168
+
169
+ $ controller = new TestController ();
170
+ $ controller ->setContainer ($ container );
171
+
172
+ $ response = $ controller ->json (array ());
173
+ $ this ->assertInstanceOf (JsonResponse::class, $ response );
174
+ $ this ->assertEquals ('[] ' , $ response ->getContent ());
175
+ }
176
+
177
+ public function testJsonWithSerializerContextOverride ()
178
+ {
179
+ $ container = $ this ->getMock (ContainerInterface::class);
180
+ $ container
181
+ ->expects ($ this ->once ())
182
+ ->method ('has ' )
183
+ ->with ('serializer ' )
184
+ ->will ($ this ->returnValue (true ));
185
+
186
+ $ serializer = $ this ->getMock (SerializerInterface::class);
187
+ $ serializer
188
+ ->expects ($ this ->once ())
189
+ ->method ('serialize ' )
190
+ ->with (array (), 'json ' , array ('json_encode_options ' => 0 , 'other ' => 'context ' ))
191
+ ->will ($ this ->returnValue ('[] ' ));
192
+
193
+ $ container
194
+ ->expects ($ this ->once ())
195
+ ->method ('get ' )
196
+ ->with ('serializer ' )
197
+ ->will ($ this ->returnValue ($ serializer ));
198
+
199
+ $ controller = new TestController ();
200
+ $ controller ->setContainer ($ container );
201
+
202
+ $ response = $ controller ->json (array (), 200 , array (), array ('json_encode_options ' => 0 , 'other ' => 'context ' ));
203
+ $ this ->assertInstanceOf (JsonResponse::class, $ response );
204
+ $ this ->assertEquals ('[] ' , $ response ->getContent ());
205
+ $ response ->setEncodingOptions (JSON_FORCE_OBJECT );
206
+ $ this ->assertEquals ('{} ' , $ response ->getContent ());
207
+ }
127
208
}
128
209
129
210
class TestController extends Controller
@@ -137,4 +218,9 @@ public function getUser()
137
218
{
138
219
return parent ::getUser ();
139
220
}
221
+
222
+ public function json ($ data , $ status = 200 , $ headers = array (), $ context = array ())
223
+ {
224
+ return parent ::json ($ data , $ status , $ headers , $ context );
225
+ }
140
226
}
0 commit comments