13
13
14
14
use Symfony \Component \Debug \ExceptionHandler ;
15
15
use Symfony \Component \Debug \Exception \OutOfMemoryException ;
16
- use Symfony \Component \HttpFoundation \Response ;
17
16
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
18
17
use Symfony \Component \HttpKernel \Exception \MethodNotAllowedHttpException ;
19
18
19
+ require_once __DIR__ .'/HeaderMock.php ' ;
20
+
20
21
class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
21
22
{
23
+ protected function setUp ()
24
+ {
25
+ testHeader ();
26
+ }
27
+
28
+ protected function tearDown ()
29
+ {
30
+ testHeader ();
31
+ }
32
+
22
33
public function testDebug ()
23
34
{
24
35
$ handler = new ExceptionHandler (false );
25
- $ response = $ handler ->createResponse (new \RuntimeException ('Foo ' ));
26
36
27
- $ this ->assertContains ('<h1>Whoops, looks like something went wrong.</h1> ' , $ response ->getContent ());
28
- $ this ->assertNotContains ('<h2 class="block_exception clear_fix"> ' , $ response ->getContent ());
37
+ ob_start ();
38
+ $ handler ->sendPhpResponse (new \RuntimeException ('Foo ' ));
39
+ $ response = ob_get_clean ();
40
+
41
+ $ this ->assertContains ('<h1>Whoops, looks like something went wrong.</h1> ' , $ response );
42
+ $ this ->assertNotContains ('<h2 class="block_exception clear_fix"> ' , $ response );
29
43
30
44
$ handler = new ExceptionHandler (true );
31
- $ response = $ handler ->createResponse (new \RuntimeException ('Foo ' ));
32
45
33
- $ this ->assertContains ('<h1>Whoops, looks like something went wrong.</h1> ' , $ response ->getContent ());
34
- $ this ->assertContains ('<h2 class="block_exception clear_fix"> ' , $ response ->getContent ());
46
+ ob_start ();
47
+ $ handler ->sendPhpResponse (new \RuntimeException ('Foo ' ));
48
+ $ response = ob_get_clean ();
49
+
50
+ $ this ->assertContains ('<h1>Whoops, looks like something went wrong.</h1> ' , $ response );
51
+ $ this ->assertContains ('<h2 class="block_exception clear_fix"> ' , $ response );
35
52
}
36
53
37
54
public function testStatusCode ()
38
55
{
39
- $ handler = new ExceptionHandler (false );
56
+ $ handler = new ExceptionHandler (false , 'iso8859-1 ' );
57
+
58
+ ob_start ();
59
+ $ handler ->sendPhpResponse (new NotFoundHttpException ('Foo ' ));
60
+ $ response = ob_get_clean ();
61
+
62
+ $ this ->assertContains ('Sorry, the page you are looking for could not be found. ' , $ response );
40
63
41
- $ response = $ handler ->createResponse (new \RuntimeException ('Foo ' ));
42
- $ this ->assertEquals ('500 ' , $ response ->getStatusCode ());
43
- $ this ->assertContains ('Whoops, looks like something went wrong. ' , $ response ->getContent ());
64
+ $ expectedHeaders = array (
65
+ array ('HTTP/1.0 404 ' , true , null ),
66
+ array ('Content-Type: text/html; charset=iso8859-1 ' , true , null ),
67
+ );
44
68
45
- $ response = $ handler ->createResponse (new NotFoundHttpException ('Foo ' ));
46
- $ this ->assertEquals ('404 ' , $ response ->getStatusCode
F438
span>());
47
- $ this ->assertContains ('Sorry, the page you are looking for could not be found. ' , $ response ->getContent ());
69
+ $ this ->assertSame ($ expectedHeaders , testHeader ());
48
70
}
49
71
50
72
public function testHeaders ()
51
73
{
52
- $ handler = new ExceptionHandler (false );
74
+ $ handler = new ExceptionHandler (false , 'iso8859-1 ' );
75
+
76
+ ob_start ();
77
+ $ handler ->sendPhpResponse (new MethodNotAllowedHttpException (array ('POST ' )));
78
+ $ response = ob_get_clean ();
53
79
54
- $ response = $ handler ->createResponse (new MethodNotAllowedHttpException (array ('POST ' )));
55
- $ this ->assertEquals ('405 ' , $ response ->getStatusCode ());
56
- $ this ->assertEquals ('POST ' , $ response ->headers ->get ('Allow ' ));
80
+ $ expectedHeaders = array (
81
+ array ('HTTP/1.0 405 ' , true , null ),
82
+ array ('Allow: POST ' , false , null ),
83
+ array ('Content-Type: text/html; charset=iso8859-1 ' , true , null ),
84
+ );
85
+
86
+ $ this ->assertSame ($ expectedHeaders , testHeader ());
57
87
}
58
88
59
89
public function testNestedExceptions ()
60
90
{
61
91
$ handler = new ExceptionHandler (true );
62
- $ response = $ handler ->createResponse (new \RuntimeException ('Foo ' , 0 , new \RuntimeException ('Bar ' )));
92
+ ob_start ();
93
+ $ handler ->sendPhpResponse (new \RuntimeException ('Foo ' , 0 , new \RuntimeException ('Bar ' )));
94
+ $ response = ob_get_clean ();
95
+
96
+ $ this ->assertStringMatchesFormat ('%A<span class="exception_message">Foo</span>%A<span class="exception_message">Bar</span>%A ' , $ response );
63
97
}
64
98
65
99
public function testHandle ()
66
100
{
67
101
$ exception = new \Exception ('foo ' );
68
102
69
- if (class_exists ('Symfony\Component\HttpFoundation\Response ' )) {
70
- $ handler = $ this ->getMock ('Symfony\Component\Debug\ExceptionHandler ' , array ('createResponse ' ));
71
- $ handler
72
- ->expects ($ this ->exactly (2 ))
73
- ->method ('createResponse ' )
74
- ->will ($ this ->returnValue (new Response ()));
75
- } else {
76
- $ handler = $ this ->getMock ('Symfony\Component\Debug\ExceptionHandler ' , array ('sendPhpResponse ' ));
77
- $ handler
78
- ->expects ($ this ->exactly (2 ))
79
- ->method ('sendPhpResponse ' );
80
- }
103
+ $ handler = $ this ->getMock ('Symfony\Component\Debug\ExceptionHandler ' , array ('sendPhpResponse ' ));
104
+ $ handler
105
+ ->expects ($ this ->exactly (2 ))
106
+ ->method ('sendPhpResponse ' );
81
107
82
108
$ handler ->handle ($ exception );
83
109
@@ -93,18 +119,10 @@ public function testHandleOutOfMemoryException()
93
119
{
94
120
$ exception = new OutOfMemoryException ('foo ' , 0 , E_ERROR , __FILE__ , __LINE__ );
95
121
96
- if (class_exists ('Symfony\Component\HttpFoundation\Response ' )) {
97
- $ handler = $ this ->getMock ('Symfony\Component\Debug\ExceptionHandler ' , array ('createResponse ' ));
98
- $ handler
99
- ->expects ($ this ->once ())
100
- ->method ('createResponse ' )
101
- ->will ($ this ->returnValue (new Response ()));
102
- } else {
103
- $ handler = $ this ->getMock ('Symfony\Component\Debug\ExceptionHandler ' , array ('sendPhpResponse ' ));
104
- $ handler
105
- ->expects ($ this ->once ())
106
- ->method ('sendPhpResponse ' );
107
- }
122
+ $ handler = $ this ->getMock ('Symfony\Component\Debug\ExceptionHandler ' , array ('sendPhpResponse ' ));
123
+ $ handler
124
+ ->expects ($ this ->once ())
125
+ ->method ('sendPhpResponse ' );
108
126
109
127
$ that = $ this ;
110
128
$ handler ->setHandler (function ($ e ) use ($ that ) {
0 commit comments