8000 [Debug] Deprecate ExceptionHandler::createResponse · symfony/symfony@e505e1e · GitHub
[go: up one dir, main page]

Skip to content

Commit e505e1e

Browse files
[Debug] Deprecate ExceptionHandler::createResponse
1 parent 2655072 commit e505e1e

File tree

3 files changed

+111
-46
lines changed

3 files changed

+111
-46
lines changed

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,23 @@ public function handle(\Exception $exception)
153153
* it will fallback to plain PHP functions.
154154
*
155155
* @param \Exception $exception An \Exception instance
156-
*
157-
* @see sendPhpResponse()
158-
* @see createResponse()
159156
*/
160157
private function failSafeHandle(\Exception $exception)
161158
{
162-
if (class_exists('Symfony\Component\HttpFoundation\Response', false)) {
159+
if (class_exists('Symfony\Component\HttpFoundation\Response', false)
160+
&& __CLASS__ !== get_class($this)
161+
&& ($reflector = new \ReflectionMethod($this, 'createResponse'))
162+
&& __CLASS__ !== $reflector->class
163+
) {
163164
$response = $this->createResponse($exception);
164165
$response->sendHeaders();
165166
$response->sendContent();
166-
} else {
167-
$this->sendPhpResponse($exception);
167+
@trigger_error(sprintf("The %s::createResponse method is deprecated since 2.8 and won't be called anymore when handling an exception in 3.0.", $reflector->class), E_USER_DEPRECATED);
168+
169+
return;
168170
}
171+
172+
$this->sendPhpResponse($exception);
169173
}
170174

171175
/**
@@ -199,9 +203,13 @@ public function sendPhpResponse($exception)
199203
* @param \Exception|FlattenException $exception An \Exception instance
200204
*
201205
* @return Response A Response instance
206+
*
207+
* @deprecated since 2.8, to be removed in 3.0.
202208
*/
203209
public function createResponse($exception)
204210
{
211+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
212+
205213
if (!$exception instanceof FlattenException) {
206214
$exception = FlattenException::create($exception);
207215
}

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

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,94 @@
1717
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1818
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1919

20+
require_once __DIR__.'/HeaderMock.php';
21+
2022
class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
2123
{
24+
protected function setUp()
25+
{
26+
testHeader();
27+
}
28+
29+
protected function tearDown()
30+
{
31+
testHeader();
32+
}
33+
2234
public function testDebug()
2335
{
2436
$handler = new ExceptionHandler(false);
25-
$response = $handler->createResponse(new \RuntimeException('Foo'));
2637

27-
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
28-
$this->assertNotContains('<h2 class="block_exception clear_fix">', $response->getContent());
38+
ob_start();
39+
$handler->sendPhpResponse(new \RuntimeException('Foo'));
40+
$response = ob_get_clean();
41+
42+
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
43+
$this->assertNotContains('<h2 class="block_exception clear_fix">', $response);
2944

3045
$handler = new ExceptionHandler(true);
31-
$response = $handler->createResponse(new \RuntimeException('Foo'));
3246

33-
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
34-
$this->assertContains('<h2 class="block_exception clear_fix">', $response->getContent());
47+
ob_start();
48+
$handler->sendPhpResponse(new \RuntimeException('Foo'));
49+
$response = ob_get_clean();
50+
51+
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
52+
$this->assertContains('<h2 class="block_exception clear_fix">', $response);
3553
}
3654

3755
public function testStatusCode()
3856
{
3957
$handler = new ExceptionHandler(false);
4058

41-
$response = $handler->createResponse(new \RuntimeException('Foo'));
42-
$this->assertEquals('500', $response->getStatusCode());
43-
$this->assertContains('Whoops, looks like something went wrong.', $response->getContent());
59+
ob_start();
60+
$handler->sendPhpResponse(new NotFoundHttpException('Foo'));
61+
$response = ob_get_clean();
62+
63+
$this->assertContains('Sorry, the page you are looking for could not be found.', $response);
64+
65+
$expectedHeaders = array(
66+
array('HTTP/1.0 404', true, null),
67+
array('Content-Type: text/html; charset=UTF-8', true, null),
68+
);
4469

45-
$response = $handler->createResponse(new NotFoundHttpException('Foo'));
46-
$this->assertEquals('404', $response->getStatusCode());
47-
$this->assertContains('Sorry, the page you are looking for could not be found.', $response->getContent());
70+
$this->assertSame($expectedHeaders, testHeader());
4871
}
4972

5073
public function testHeaders()
5174
{
5275
$handler = new ExceptionHandler(false);
5376

54-
$response = $handler->createResponse(new MethodNotAllowedHttpException(array('POST')));
55-
$this->assertEquals('405', $response->getStatusCode());
56-
$this->assertEquals('POST', $response->headers->get('Allow'));
77+
ob_start();
78+
$handler->sendPhpResponse(new MethodNotAllowedHttpException(array('POST')));
79+
$response = ob_get_clean();
80+
81+
$expectedHeaders = array(
82+
array('HTTP/1.0 405', true, null),
83+
array('Allow: POST', false, null),
84+
array('Content-Type: text/html; charset=UTF-8', true, null),
85+
);
86+
87+
$this->assertSame($expectedHeaders, testHeader());
5788
}
5889

5990
public function testNestedExceptions()
6091
{
6192
$handler = new ExceptionHandler(true);
62-
$response = $handler->createResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
93+
ob_start();
94+
$handler->sendPhpResponse(new \Run 10000 timeException('Foo', 0, new \RuntimeException('Bar')));
95+
$response = ob_get_clean();
96+
97+
$this->assertStringMatchesFormat('%A<span class="exception_message">Foo</span>%A<span class="exception_message">Bar</span>%A', $response);
6398
}
6499

65100
public function testHandle()
66101
{
67102
$exception = new \Exception('foo');
68103

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-
}
104+
$handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
105+
$handler
106+
->expects($this->exactly(2))
107+
->method('sendPhpResponse');
81108

82109
$handler->handle($exception);
83110

@@ -93,18 +120,10 @@ public function testHandleOutOfMemoryException()
93120
{
94121
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
95122

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-
}
123+
$handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
124+
$handler
125+
->expects($this->once())
126+
->method('sendPhpResponse');
108127

109128
$that = $this;
110129
$handler->setHandler(function ($e) use ($that) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Debug;
13+
14+
function headers_sent()
15+
{
16+
return false;
17+
}
18+
19+
function header($str, $replace = true, $status = null)
20+
{
21+
Tests\testHeader($str, $replace, $status);
22+
}
23+
24+
namespace Symfony\Component\Debug\Tests;
25+
26+
function testHeader()
27+
{
28+
static $headers = array();
29+
30+
if (!$h = func_get_args()) {
31+
$h = $headers;
32+
$headers = array();
33+
34+
return $h;
35+
}
36+
37+
$headers[] = func_get_args();
38+
}

0 commit comments

Comments
 (0)
0