@@ -22,40 +22,57 @@ class ExceptionControllerTest extends TestCase
22
22
{
23
23
public function testShowActionCanBeForcedToShowErrorPage ()
24
24
{
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> ' ));
30
26
31
- $ request = Request::create ('whatever ' , 'GET ' );
32
- $ request ->headers ->set ('X-Php-Ob-Level ' , 1 );
27
+ $ request = $ this ->createRequest ('html ' );
33
28
$ request ->attributes ->set ('showException ' , false );
34
29
$ exception = FlattenException::create (new \Exception (), 404 );
35
30
$ controller = new ExceptionController ($ twig , /* "showException" defaults to --> */ true );
36
31
37
32
$ response = $ controller ->showAction ($ request , $ exception , null );
38
33
39
34
$ 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 ());
41
36
}
42
37
43
38
public function testFallbackToHtmlIfNoTemplateForRequestedFormat ()
44
39
{
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> ' ));
50
41
51
- $ request = Request::create ('whatever ' );
52
- $ request ->headers ->set ('X-Php-Ob-Level ' , 1 );
53
- $ request ->setRequestFormat ('txt ' );
42
+ $ request = $ this ->createRequest ('txt ' );
54
43
$ exception = FlattenException::create (new \Exception ());
55
44
$ controller = new ExceptionController ($ twig , false );
56
45
57
- $ response = $ controller ->showAction ($ request , $ exception );
46
+ $ controller ->showAction ($ request , $ exception );
58
47
59
48
$ this ->assertEquals ('html ' , $ request ->getRequestFormat ());
60
49
}
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
+ }
61
78
}
0 commit comments