@@ -76,7 +76,7 @@ public function testHeaders()
76
76
77
77
ob_start ();
78
78
$ handler ->sendPhpResponse (new MethodNotAllowedHttpException (['POST ' ]));
79
- $ response = ob_get_clean ();
79
+ ob_get_clean ();
80
80
81
81
$ expectedHeaders = [
82
82
['HTTP/1.0 405 ' , true , null ],
@@ -99,35 +99,65 @@ public function testNestedExceptions()
99
99
100
100
public function testHandle ()
101
101
{
102
- $ exception = new \Exception ('foo ' );
102
+ $ handler = new ExceptionHandler (true );
103
+ ob_start ();
103
104
104
- $ handler = $ this ->getMockBuilder ('Symfony\Component\Debug\ExceptionHandler ' )->setMethods (['sendPhpResponse ' ])->getMock ();
105
- $ handler
106
- ->expects ($ this ->exactly (2 ))
107
- ->method ('sendPhpResponse ' );
105
+ $ handler ->handle (new \Exception ('foo ' ));
108
106
109
- $ handler ->handle ($ exception );
107
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), \Exception::class, 'Exception ' , 'foo ' );
108
+ }
110
109
111
- $ handler ->setHandler (function ($ e ) use ($ exception ) {
112
- $ this ->assertSame ($ exception , $ e );
110
+ public function testHandleWithACustomHandlerThatOutputsSomething ()
111
+ {
112
+ $ handler = new ExceptionHandler (true );
113
+ ob_start ();
114
+ $ handler ->setHandler (function () {
115
+ echo 'ccc ' ;
113
116
});
114
117
115
- $ handler ->handle ($ exception );
118
+ $ handler ->handle (new \Exception ());
119
+ ob_end_flush (); // Necessary because of this PHP bug : https://bugs.php.net/bug.php?id=76563
120
+ $ this ->assertSame ('ccc ' , ob_get_clean ());
116
121
}
117
122
118
- public function testHandleOutOfMemoryException ()
123
+ public function testHandleWithACustomHandlerThatOutputsNothing ()
124
+ {
125
+ $ handler = new ExceptionHandler (true );
126
+ $ handler ->setHandler (function () {});
127
+
128
+ $ handler ->handle (new \Exception ('ccc ' ));
129
+
130
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), \Exception::class, 'Exception ' , 'ccc ' );
131
+ }
132
+
133
+ public function testHandleWithACustomHandlerThatFails ()
119
134
{
120
- $ exception = new OutOfMemoryException ('foo ' , 0 , E_ERROR , __FILE__ , __LINE__ );
135
+ $ handler = new ExceptionHandler (true );
136
+ $ handler ->setHandler (function () {
137
+ throw new \RuntimeException ();
138
+ });
121
139
122
- $ handler = $ this ->getMockBuilder ('Symfony\Component\Debug\ExceptionHandler ' )->setMethods (['sendPhpResponse ' ])->getMock ();
123
- $ handler
124
- ->expects ($ this ->once ())
125
- ->method ('sendPhpResponse ' );
140
+ $ handler ->handle (new \Exception ('ccc ' ));
126
141
127
- $ handler ->setHandler (function ($ e ) {
142
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), \Exception::class, 'Exception ' , 'ccc ' );
143
+ }
144
+
145
+ public function testHandleOutOfMemoryException ()
146
+ {
147
+ $ handler = new ExceptionHandler (true );
148
+ ob_start ();
149
+ $ handler ->setHandler (function () {
128
150
$ this ->fail ('OutOfMemoryException should bypass the handler ' );
129
151
});
130
152
131
- $ handler ->handle ($ exception );
153
+ $ handler ->handle (new OutOfMemoryException ('foo ' , 0 , E_ERROR , __FILE__ , __LINE__ ));
154
+
155
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), OutOfMemoryException::class, 'OutOfMemoryException ' , 'foo ' );
156
+ }
157
+
158
+ private function assertThatTheExceptionWasOutput ($ content , $ expectedClass , $ expectedTitle , $ expectedMessage )
159
+ {
160
+ $ this ->assertContains (sprintf ('<span class="exception_title"><abbr title="%s">%s</abbr></span> ' , $ expectedClass , $ expectedTitle ), $ content );
161
+ $ this ->assertContains (sprintf ('<p class="break-long-words trace-message">%s</p> ' , $ expectedMessage ), $ content );
132
162
}
133
163
}
0 commit comments