Skip to content

Commit c53e253

Browse files
committed
[Debug][ExceptionHandler] Add tests for custom handlers
1 parent aece546 commit c53e253

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php

+48-18
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testHeaders()
7676

7777
ob_start();
7878
$handler->sendPhpResponse(new MethodNotAllowedHttpException(['POST']));
79-
$response = ob_get_clean();
79+
ob_get_clean();
8080

8181
$expectedHeaders = [
8282
['HTTP/1.0 405', true, null],
@@ -99,35 +99,65 @@ public function testNestedExceptions()
9999

100100
public function testHandle()
101101
{
102-
$exception = new \Exception('foo');
102+
$handler = new ExceptionHandler(true);
103+
ob_start();
103104

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'));
108106

109-
$handler->handle($exception);
107+
$this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'foo');
108+
}
110109

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';
113116
});
114117

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());
116121
}
117122

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()
119134
{
120-
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
135+
$handler = new ExceptionHandler(true);
136+
$handler->setHandler(function () {
137+
throw new \RuntimeException();
138+
});
121139

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'));
126141

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 () {
128150
$this->fail('OutOfMemoryException should bypass the handler');
129151
});
130152

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);
132162
}
133163
}

0 commit comments

Comments
 (0)