Yaffs site version 1.1
[yaffs-website] / vendor / symfony / debug / Tests / ExceptionHandlerTest.php
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\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\ExceptionHandler;
16 use Symfony\Component\Debug\Exception\OutOfMemoryException;
17 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
19
20 require_once __DIR__.'/HeaderMock.php';
21
22 class ExceptionHandlerTest extends TestCase
23 {
24     protected function setUp()
25     {
26         testHeader();
27     }
28
29     protected function tearDown()
30     {
31         testHeader();
32     }
33
34     public function testDebug()
35     {
36         $handler = new ExceptionHandler(false);
37
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);
44
45         $handler = new ExceptionHandler(true);
46
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);
53     }
54
55     public function testStatusCode()
56     {
57         $handler = new ExceptionHandler(false, 'iso8859-1');
58
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=iso8859-1', true, null),
68         );
69
70         $this->assertSame($expectedHeaders, testHeader());
71     }
72
73     public function testHeaders()
74     {
75         $handler = new ExceptionHandler(false, 'iso8859-1');
76
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=iso8859-1', true, null),
85         );
86
87         $this->assertSame($expectedHeaders, testHeader());
88     }
89
90     public function testNestedExceptions()
91     {
92         $handler = new ExceptionHandler(true);
93         ob_start();
94         $handler->sendPhpResponse(new \RuntimeException('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);
98     }
99
100     public function testHandle()
101     {
102         $exception = new \Exception('foo');
103
104         $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
105         $handler
106             ->expects($this->exactly(2))
107             ->method('sendPhpResponse');
108
109         $handler->handle($exception);
110
111         $that = $this;
112         $handler->setHandler(function ($e) use ($exception, $that) {
113             $that->assertSame($exception, $e);
114         });
115
116         $handler->handle($exception);
117     }
118
119     public function testHandleOutOfMemoryException()
120     {
121         $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
122
123         $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
124         $handler
125             ->expects($this->once())
126             ->method('sendPhpResponse');
127
128         $that = $this;
129         $handler->setHandler(function ($e) use ($that) {
130             $that->fail('OutOfMemoryException should bypass the handler');
131         });
132
133         $handler->handle($exception);
134     }
135 }