8000 minor #15417 [Debug] cleanup ExceptionHandlerTest (nicolas-grekas) · symfony/symfony@9058904 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9058904

Browse files
committed
minor #15417 [Debug] cleanup ExceptionHandlerTest (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Debug] cleanup ExceptionHandlerTest | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 51bacc6 [Debug] cleanup ExceptionHandlerTest
2 parents 15d6b8a + 51bacc6 commit 9058904

File tree

4 files changed

+110
-56
lines changed

4 files changed

+110
-56
lines changed

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ExceptionHandler
3838

3939
public function __construct($debug = true, $charset = null, $fileLinkFormat = null)
4040
{
41-
if (false !== strpos($charset, '%') xor false === strpos($fileLinkFormat, '%')) {
41+
if (false !== strpos($charset, '%')) {
4242
// Swap $charset and $fileLinkFormat for BC reasons
4343
$pivot = $fileLinkFormat;
4444
$fileLinkFormat = $charset;
@@ -153,19 +153,22 @@ 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+
168+
return;
168169
}
170+
171+
$this->sendPhpResponse($exception);
169172
}
170173

171174
/**

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

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,97 @@
1313

1414
use Symfony\Component\Debug\ExceptionHandler;
1515
use Symfony\Component\Debug\Exception\OutOfMemoryException;
16-
use Symfony\Component\HttpFoundation\Response;
1716
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1817
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1918

19+
require_once __DIR__.'/HeaderMock.php';
20+
2021
class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
2122
{
23+
protected function setUp()
24+
{
25+
testHeader();
26+
}
27+
28+
protected function tearDown()
29+
{
30+
testHeader();
31+
}
32+
2233
public function testDebug()
2334
{
2435
$handler = new ExceptionHandler(false);
25-
$response = $handler->createResponse(new \RuntimeException('Foo'));
2636

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);
2943

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

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);
3552
}
3653

3754
public function testStatusCode()
3855
{
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);
4063

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+
);
4468

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());
69+
$this->assertSame($expectedHeaders, testHeader());
4870
}
4971

5072
public function testHeaders()
5173
{
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();
5379

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());
5787
}
5888

5989
public function testNestedExceptions()
6090
{
6191
$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);
6397
}
6498

6599
public function testHandle()
66100
{
67101
$exception = new \Exception('foo');
68102

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

82108
$handler->handle($exception);
83109

@@ -93,18 +119,10 @@ public function testHandleOutOfMemoryException()
93119
{
94120
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
95121

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

109127
$that = $this;
110128
$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+
}

src/Symfony/Component/Debug/composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@
2525
"require-dev": {
2626
"symfony/phpunit-bridge": "~2.7",
2727
"symfony/class-loader": "~2.2",
28-
"symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2",
29-
"symfony/http-foundation": "~2.1"
30-
},
31-
"suggest": {
32-
"symfony/http-foundation": "",
33-
"symfony/http-kernel": ""
28+
"symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2"
3429
},
3530
"autoload": {
3631
"psr-4": { "Symfony\\Component\\Debug\\": "" }

0 commit comments

Comments
 (0)
0