ef9fb9ab4da92e56921a169f51bbc5b4f64228c0
[yaffs-website] / vendor / psy / psysh / test / Exception / ErrorExceptionTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
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 Psy\Test\Exception;
13
14 use Psy\Exception\ErrorException;
15
16 class ErrorExceptionTest extends \PHPUnit\Framework\TestCase
17 {
18     public function testInstance()
19     {
20         $e = new ErrorException();
21
22         $this->assertInstanceOf('Psy\Exception\Exception', $e);
23         $this->assertInstanceOf('ErrorException', $e);
24         $this->assertInstanceOf('Psy\Exception\ErrorException', $e);
25     }
26
27     public function testMessage()
28     {
29         $e = new ErrorException('foo');
30
31         $this->assertContains('foo', $e->getMessage());
32         $this->assertSame('foo', $e->getRawMessage());
33     }
34
35     /**
36      * @dataProvider getLevels
37      */
38     public function testErrorLevels($level, $type)
39     {
40         $e = new ErrorException('foo', 0, $level);
41         $this->assertContains('PHP ' . $type, $e->getMessage());
42     }
43
44     /**
45      * @dataProvider getLevels
46      */
47     public function testThrowException($level, $type)
48     {
49         try {
50             ErrorException::throwException($level, '{whot}', '{file}', '13');
51         } catch (ErrorException $e) {
52             $this->assertContains('PHP ' . $type, $e->getMessage());
53             $this->assertContains('{whot}', $e->getMessage());
54             $this->assertContains('in {file}', $e->getMessage());
55             $this->assertContains('on line 13', $e->getMessage());
56         }
57     }
58
59     public function getLevels()
60     {
61         return [
62             [E_WARNING,           'Warning'],
63             [E_CORE_WARNING,      'Warning'],
64             [E_COMPILE_WARNING,   'Warning'],
65             [E_USER_WARNING,      'Warning'],
66             [E_STRICT,            'Strict error'],
67             [E_DEPRECATED,        'Deprecated'],
68             [E_USER_DEPRECATED,   'Deprecated'],
69             [E_RECOVERABLE_ERROR, 'Recoverable fatal error'],
70             [0,                   'Error'],
71         ];
72     }
73
74     /**
75      * @dataProvider getUserLevels
76      */
77     public function testThrowExceptionAsErrorHandler($level, $type)
78     {
79         \set_error_handler(['Psy\Exception\ErrorException', 'throwException']);
80         try {
81             \trigger_error('{whot}', $level);
82         } catch (ErrorException $e) {
83             $this->assertContains('PHP ' . $type, $e->getMessage());
84             $this->assertContains('{whot}', $e->getMessage());
85         }
86         \restore_error_handler();
87     }
88
89     public function getUserLevels()
90     {
91         return [
92             [E_USER_ERROR,      'Error'],
93             [E_USER_WARNING,    'Warning'],
94             [E_USER_NOTICE,     'Notice'],
95             [E_USER_DEPRECATED, 'Deprecated'],
96         ];
97     }
98
99     public function testIgnoreExecutionLoopFilename()
100     {
101         $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/ExecutionLoop.php');
102         $this->assertEmpty($e->getFile());
103
104         $e = new ErrorException('{{message}}', 0, 1, 'c:\fake\path\to\Psy\ExecutionLoop.php');
105         $this->assertEmpty($e->getFile());
106
107         $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
108         $this->assertNotEmpty($e->getFile());
109     }
110
111     public function testFromError()
112     {
113         if (\version_compare(PHP_VERSION, '7.0.0', '<')) {
114             $this->markTestSkipped();
115         }
116
117         $error = new \Error('{{message}}', 0);
118         $exception = ErrorException::fromError($error);
119
120         $this->assertContains('PHP Error:  {{message}}', $exception->getMessage());
121         $this->assertEquals(0, $exception->getCode());
122         $this->assertEquals($error->getFile(), $exception->getFile());
123         $this->assertSame($exception->getPrevious(), $error);
124     }
125 }