1414use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1616use Symfony\Component\DependencyInjection\ContainerInterface;
17+ use Symfony\Component\HttpFoundation\JsonResponse;
1718use Symfony\Component\HttpFoundation\Request;
1819use Symfony\Component\HttpFoundation\RequestStack;
1920use Symfony\Component\HttpFoundation\Response;
2021use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
2122use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2223use Symfony\Component\Security\Core\User\User;
24+ use Symfony\Component\Serializer\SerializerInterface;
2325
2426class ControllerTest extends TestCase
2527{
@@ -124,6 +126,85 @@ private function getContainerWithTokenStorage($token = null)
124126
125127 return $container;
126128 }
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+ }
127208}
128209
129210class TestController extends Controller
@@ -137,4 +218,9 @@ public function getUser()
137218 {
138219 return parent::getUser();
139220 }
221+
222+ public function json($data, $status = 200, $headers = array(), $context = array())
223+ {
224+ return parent::json($data, $status, $headers, $context);
225+ }
140226}
0 commit comments