Yaffs site version 1.1
[yaffs-website] / vendor / symfony / debug / Tests / Exception / FlattenExceptionTest.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\Exception;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\Exception\FlattenException;
16 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
18 use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
19 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
20 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
21 use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
22 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
23 use Symfony\Component\HttpKernel\Exception\GoneHttpException;
24 use Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
25 use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
26 use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
27 use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
28 use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
29 use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
30
31 class FlattenExceptionTest extends TestCase
32 {
33     public function testStatusCode()
34     {
35         $flattened = FlattenException::create(new \RuntimeException(), 403);
36         $this->assertEquals('403', $flattened->getStatusCode());
37
38         $flattened = FlattenException::create(new \RuntimeException());
39         $this->assertEquals('500', $flattened->getStatusCode());
40
41         $flattened = FlattenException::create(new NotFoundHttpException());
42         $this->assertEquals('404', $flattened->getStatusCode());
43
44         $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
45         $this->assertEquals('401', $flattened->getStatusCode());
46
47         $flattened = FlattenException::create(new BadRequestHttpException());
48         $this->assertEquals('400', $flattened->getStatusCode());
49
50         $flattened = FlattenException::create(new NotAcceptableHttpException());
51         $this->assertEquals('406', $flattened->getStatusCode());
52
53         $flattened = FlattenException::create(new ConflictHttpException());
54         $this->assertEquals('409', $flattened->getStatusCode());
55
56         $flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
57         $this->assertEquals('405', $flattened->getStatusCode());
58
59         $flattened = FlattenException::create(new AccessDeniedHttpException());
60         $this->assertEquals('403', $flattened->getStatusCode());
61
62         $flattened = FlattenException::create(new GoneHttpException());
63         $this->assertEquals('410', $flattened->getStatusCode());
64
65         $flattened = FlattenException::create(new LengthRequiredHttpException());
66         $this->assertEquals('411', $flattened->getStatusCode());
67
68         $flattened = FlattenException::create(new PreconditionFailedHttpException());
69         $this->assertEquals('412', $flattened->getStatusCode());
70
71         $flattened = FlattenException::create(new PreconditionRequiredHttpException());
72         $this->assertEquals('428', $flattened->getStatusCode());
73
74         $flattened = FlattenException::create(new ServiceUnavailableHttpException());
75         $this->assertEquals('503', $flattened->getStatusCode());
76
77         $flattened = FlattenException::create(new TooManyRequestsHttpException());
78         $this->assertEquals('429', $flattened->getStatusCode());
79
80         $flattened = FlattenException::create(new UnsupportedMediaTypeHttpException());
81         $this->assertEquals('415', $flattened->getStatusCode());
82     }
83
84     public function testHeadersForHttpException()
85     {
86         $flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
87         $this->assertEquals(array('Allow' => 'POST'), $flattened->getHeaders());
88
89         $flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
90         $this->assertEquals(array('WWW-Authenticate' => 'Basic realm="My Realm"'), $flattened->getHeaders());
91
92         $flattened = FlattenException::create(new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
93         $this->assertEquals(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), $flattened->getHeaders());
94
95         $flattened = FlattenException::create(new ServiceUnavailableHttpException(120));
96         $this->assertEquals(array('Retry-After' => 120), $flattened->getHeaders());
97
98         $flattened = FlattenException::create(new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
99         $this->assertEquals(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), $flattened->getHeaders());
100
101         $flattened = FlattenException::create(new TooManyRequestsHttpException(120));
102         $this->assertEquals(array('Retry-After' => 120), $flattened->getHeaders());
103     }
104
105     /**
106      * @dataProvider flattenDataProvider
107      */
108     public function testFlattenHttpException(\Exception $exception, $statusCode)
109     {
110         $flattened = FlattenException::create($exception);
111         $flattened2 = FlattenException::create($exception);
112
113         $flattened->setPrevious($flattened2);
114
115         $this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
116         $this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
117         $this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
118     }
119
120     /**
121      * @dataProvider flattenDataProvider
122      */
123     public function testPrevious(\Exception $exception, $statusCode)
124     {
125         $flattened = FlattenException::create($exception);
126         $flattened2 = FlattenException::create($exception);
127
128         $flattened->setPrevious($flattened2);
129
130         $this->assertSame($flattened2, $flattened->getPrevious());
131
132         $this->assertSame(array($flattened2), $flattened->getAllPrevious());
133     }
134
135     /**
136      * @requires PHP 7.0
137      */
138     public function testPreviousError()
139     {
140         $exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
141
142         $flattened = FlattenException::create($exception)->getPrevious();
143
144         $this->assertEquals($flattened->getMessage(), 'Parse error: Oh noes!', 'The message is copied from the original exception.');
145         $this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
146         $this->assertEquals($flattened->getClass(), 'Symfony\Component\Debug\Exception\FatalThrowableError', 'The class is set to the class of the original exception');
147     }
148
149     /**
150      * @dataProvider flattenDataProvider
151      */
152     public function testLine(\Exception $exception)
153     {
154         $flattened = FlattenException::create($exception);
155         $this->assertSame($exception->getLine(), $flattened->getLine());
156     }
157
158     /**
159      * @dataProvider flattenDataProvider
160      */
161     public function testFile(\Exception $exception)
162     {
163         $flattened = FlattenException::create($exception);
164         $this->assertSame($exception->getFile(), $flattened->getFile());
165     }
166
167     /**
168      * @dataProvider flattenDataProvider
169      */
170     public function testToArray(\Exception $exception, $statusCode)
171     {
172         $flattened = FlattenException::create($exception);
173         $flattened->setTrace(array(), 'foo.php', 123);
174
175         $this->assertEquals(array(
176             array(
177                 'message' => 'test',
178                 'class' => 'Exception',
179                 'trace' => array(array(
180                     'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
181                     'args' => array(),
182                 )),
183             ),
184         ), $flattened->toArray());
185     }
186
187     public function flattenDataProvider()
188     {
189         return array(
190             array(new \Exception('test', 123), 500),
191         );
192     }
193
194     public function testRecursionInArguments()
195     {
196         $a = array('foo', array(2, &$a));
197         $exception = $this->createException($a);
198
199         $flattened = FlattenException::create($exception);
200         $trace = $flattened->getTrace();
201         $this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
202     }
203
204     public function testTooBigArray()
205     {
206         $a = array();
207         for ($i = 0; $i < 20; ++$i) {
208             for ($j = 0; $j < 50; ++$j) {
209                 for ($k = 0; $k < 10; ++$k) {
210                     $a[$i][$j][$k] = 'value';
211                 }
212             }
213         }
214         $a[20] = 'value';
215         $a[21] = 'value1';
216         $exception = $this->createException($a);
217
218         $flattened = FlattenException::create($exception);
219         $trace = $flattened->getTrace();
220         $serializeTrace = serialize($trace);
221
222         $this->assertContains('*SKIPPED over 10000 entries*', $serializeTrace);
223         $this->assertNotContains('*value1*', $serializeTrace);
224     }
225
226     private function createException($foo)
227     {
228         return new \Exception();
229     }
230
231     public function testSetTraceIncompleteClass()
232     {
233         $flattened = FlattenException::create(new \Exception('test', 123));
234         $flattened->setTrace(
235             array(
236                 array(
237                     'file' => __FILE__,
238                     'line' => 123,
239                     'function' => 'test',
240                     'args' => array(
241                         unserialize('O:14:"BogusTestClass":0:{}'),
242                     ),
243                 ),
244             ),
245             'foo.php', 123
246         );
247
248         $this->assertEquals(array(
249             array(
250                 'message' => 'test',
251                 'class' => 'Exception',
252                 'trace' => array(
253                     array(
254                         'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '',
255                         'file' => 'foo.php', 'line' => 123,
256                         'args' => array(),
257                     ),
258                     array(
259                         'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => 'test',
260                         'file' => __FILE__, 'line' => 123,
261                         'args' => array(
262                             array(
263                                 'incomplete-object', 'BogusTestClass',
264                             ),
265                         ),
266                     ),
267                 ),
268             ),
269         ), $flattened->toArray());
270     }
271 }