@@ -22,40 +22,57 @@ class ExceptionControllerTest extends TestCase
2222{
2323 public function testShowActionCanBeForcedToShowErrorPage ()
2424 {
25- $ twig = new Environment (
26- new ArrayLoader (array (
27- 'TwigBundle:Exception:error404.html.twig ' => 'ok ' ,
28- ))
29- );
25+ $ twig = $ this ->createTwigEnv (array ('TwigBundle:Exception:error404.html.twig ' => '<html>not found</html> ' ));
3026
31- $ request = Request::create ('whatever ' , 'GET ' );
32- $ request ->headers ->set ('X-Php-Ob-Level ' , 1 );
27+ $ request = $ this ->createRequest ('html ' );
3328 $ request ->attributes ->set ('showException ' , false );
3429 $ exception = FlattenException::create (new \Exception (), 404 );
3530 $ controller = new ExceptionController ($ twig , /* "showException" defaults to --> */ true );
3631
3732 $ response = $ controller ->showAction ($ request , $ exception , null );
3833
3934 $ this ->assertEquals (200 , $ response ->getStatusCode ()); // successful request
40- $ this ->assertEquals ('ok ' , $ response ->getContent ()); // content of the error404.html template
35+ $ this ->assertEquals ('<html>not found</html> ' , $ response ->getContent ());
4136 }
4237
4338 public function testFallbackToHtmlIfNoTemplateForRequestedFormat ()
4439 {
45- $ twig = new Environment (
46- new ArrayLoader (array (
47- 'TwigBundle:Exception:error.html.twig ' => 'html ' ,
48- ))
49- );
40+ $ twig = $ this ->createTwigEnv (array ('TwigBundle:Exception:error.html.twig ' => '<html></html> ' ));
5041
51- $ request = Request::create ('whatever ' );
52- $ request ->headers ->set ('X-Php-Ob-Level ' , 1 );
53- $ request ->setRequestFormat ('txt ' );
42+ $ request = $ this ->createRequest ('txt ' );
5443 $ exception = FlattenException::create (new \Exception ());
5544 $ controller = new ExceptionController ($ twig , false );
5645
57- $ response = $ controller ->showAction ($ request , $ exception );
46+ $ controller ->showAction ($ request , $ exception );
5847
5948 $ this ->assertEquals ('html ' , $ request ->getRequestFormat ());
6049 }
50+
51+ public function testResponseHasRequestedMimeType ()
52+ {
53+ $ twig = $ this ->createTwigEnv (array ('TwigBundle:Exception:error.json.twig ' => '{} ' ));
54+
55+ $ request = $ this ->createRequest ('json ' );
56+ $ exception = FlattenException::create (new \Exception ());
57+ $ controller = new ExceptionController ($ twig , false );
58+
59+ $ response = $ controller ->showAction ($ request , $ exception );
60+
61+ $ this ->assertEquals ('json ' , $ request ->getRequestFormat ());
62+ $ this ->assertEquals ($ request ->getMimeType ('json ' ), $ response ->headers ->get ('Content-Type ' ));
63+ }
64+
65+ private function createRequest ($ requestFormat )
66+ {
67+ $ request = Request::create ('whatever ' );
68+ $ request ->headers ->set ('X-Php-Ob-Level ' , 1 );
69+ $ request ->setRequestFormat ($ requestFormat );
70+
71+ return $ request ;
72+ }
73+
74+ private function createTwigEnv (array $ templates )
75+ {
76+ return new Environment (new ArrayLoader ($ templates ));
77+ }
6178}
0 commit comments