@@ -85,7 +85,7 @@ public function testHeaders()
85
85
86
86
ob_start ();
87
87
$ handler ->sendPhpResponse (new MethodNotAllowedHttpException (['POST ' ]));
88
- $ response = ob_get_clean ();
88
+ ob_get_clean ();
89
89
90
90
$ expectedHeaders = [
91
91
['HTTP/1.0 405 ' , true , null ],
@@ -106,37 +106,78 @@ public function testNestedExceptions()
106
106
$ this ->assertStringMatchesFormat ('%A<p class="break-long-words trace-message">Foo</p>%A<p class="break-long-words trace-message">Bar</p>%A ' , $ response );
107
107
}
108
108
109
- public function testHandle ()
109
+ /**
110
+ * @dataProvider handleProvider
111
+ */
112
+ public function testHandle (\Throwable $ exception , string $ expectedClass , string $ expectedTitle , string $ expectedMessage )
110
113
{
111
- $ exception = new \Exception ('foo ' );
112
-
113
- $ handler = $ this ->getMockBuilder ('Symfony\Component\ErrorCatcher\ExceptionHandler ' )->setMethods (['sendPhpResponse ' ])->getMock ();
114
- $ handler
115
- ->expects ($ this ->exactly (2 ))
116
- ->method ('sendPhpResponse ' );
114
+ $ handler = new ExceptionHandler (true );
115
+ ob_start ();
117
116
118
117
$ handler ->handle ($ exception );
119
118
120
- $ handler ->setHandler (function ($ e ) use ($ exception ) {
121
- $ this ->assertSame ($ exception , $ e );
119
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), $ expectedClass , $ expectedTitle , $ expectedMessage );
120
+ }
121
+
122
+ public function handleProvider ()
123
+ {
124
+ return [
125
+ [new \Exception ('foo ' ), \Exception::class, 'Exception ' , 'foo ' ],
126
+ [new \Error ('bar ' ), \Error::class, 'Error ' , 'bar ' ],
127
+ ];
128
+ }
129
+
130
+ public function testHandleWithACustomHandlerThatOutputsSomething ()
131
+ {
132
+ $ handler = new ExceptionHandler (true );
133
+ ob_start ();
134
+ $ handler ->setHandler (function () {
135
+ echo 'ccc ' ;
122
136
});
123
137
124
- $ handler ->handle ($ exception );
138
+ $ handler ->handle (new \Exception ());
139
+ ob_end_flush (); // Necessary because of this PHP bug : https://bugs.php.net/bug.php?id=76563
140
+ $ this ->assertSame ('ccc ' , ob_get_clean ());
125
141
}
126
142
127
- public function testHandleOutOfMemoryException ()
143
+ public function testHandleWithACustomHandlerThatOutputsNothing ()
144
+ {
145
+ $ handler = new ExceptionHandler (true );
146
+ $ handler ->setHandler (function () {});
147
+
148
+ $ handler ->handle (new \Exception ('ccc ' ));
149
+
150
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), \Exception::class, 'Exception ' , 'ccc ' );
151
+ }
152
+
153
+ public function testHandleWithACustomHandlerThatFails ()
128
154
{
129
- $ exception = new OutOfMemoryException ('foo ' , 0 , E_ERROR , __FILE__ , __LINE__ );
155
+ $ handler = new ExceptionHandler (true );
156
+ $ handler ->setHandler (function () {
157
+ throw new \RuntimeException ();
158
+ });
159
+
160
+ $ handler ->handle (new \Exception ('ccc ' ));
130
161
131
- $ handler = $ this ->getMockBuilder ('Symfony\Component\ErrorCatcher\ExceptionHandler ' )->setMethods (['sendPhpResponse ' ])->getMock ();
132
- $ handler
133
- ->expects ($ this ->once ())
134
- ->method ('sendPhpResponse ' );
162
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), \Exception::class, 'Exception ' , 'ccc ' );
163
+ }
135
164
136
- $ handler ->setHandler (function ($ e ) {
165
+ public function testHandleOutOfMemoryException ()
166
+ {
167
+ $ handler = new ExceptionHandler (true );
168
+ ob_start ();
169
+ $ handler ->setHandler (function () {
137
170
$ this ->fail ('OutOfMemoryException should bypass the handler ' );
138
171
});
139
172
140
- $ handler ->handle ($ exception );
173
+ $ handler ->handle (new OutOfMemoryException ('foo ' , 0 , E_ERROR , __FILE__ , __LINE__ ));
174
+
175
+ $ this ->assertThatTheExceptionWasOutput (ob_get_clean (), OutOfMemoryException::class, 'OutOfMemoryException ' , 'foo ' );
176
+ }
177
+
178
+ private function assertThatTheExceptionWasOutput (string $ content , string $ expectedClass , string $ expectedTitle , string $ expectedMessage )
179
+ {
180
+ $ this ->assertContains (sprintf ('<span class="exception_title"><abbr title="%s">%s</abbr></span> ' , $ expectedClass , $ expectedTitle ), $ content );
181
+ $ this ->assertContains (sprintf ('<h1 class="break-long-words exception-message">%s</h1> ' , $ expectedMessage ), $ content );
141
182
}
142
183
}
0 commit comments