6ff6a74f4f99e90684ac22d33e64dbd2f42f821e
[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\Exception\OutOfMemoryException;
16 use Symfony\Component\Debug\ExceptionHandler;
17 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
18 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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('Whoops, looks like something went wrong.', $response);
43         $this->assertNotContains('<div class="trace trace-as-html">', $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('Whoops, looks like something went wrong.', $response);
52         $this->assertContains('<div class="trace trace-as-html">', $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<p class="break-long-words trace-message">Foo</p>%A<p class="break-long-words trace-message">Bar</p>%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         $handler->setHandler(function ($e) use ($exception) {
112             $this->assertSame($exception, $e);
113         });
114
115         $handler->handle($exception);
116     }
117
118     public function testHandleOutOfMemoryException()
119     {
120         $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
121
122         $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
123         $handler
124             ->expects($this->once())
125             ->method('sendPhpResponse');
126
127         $handler->setHandler(function ($e) {
128             $this->fail('OutOfMemoryException should bypass the handler');
129         });
130
131         $handler->handle($exception);
132     }
133 }