Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Test\Exception;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\Exception\ErrorException;
|
Chris@13
|
15
|
Chris@13
|
16 class ErrorExceptionTest extends \PHPUnit\Framework\TestCase
|
Chris@13
|
17 {
|
Chris@13
|
18 public function testInstance()
|
Chris@13
|
19 {
|
Chris@13
|
20 $e = new ErrorException();
|
Chris@13
|
21
|
Chris@13
|
22 $this->assertInstanceOf('Psy\Exception\Exception', $e);
|
Chris@13
|
23 $this->assertInstanceOf('ErrorException', $e);
|
Chris@13
|
24 $this->assertInstanceOf('Psy\Exception\ErrorException', $e);
|
Chris@13
|
25 }
|
Chris@13
|
26
|
Chris@13
|
27 public function testMessage()
|
Chris@13
|
28 {
|
Chris@13
|
29 $e = new ErrorException('foo');
|
Chris@13
|
30
|
Chris@13
|
31 $this->assertContains('foo', $e->getMessage());
|
Chris@13
|
32 $this->assertSame('foo', $e->getRawMessage());
|
Chris@13
|
33 }
|
Chris@13
|
34
|
Chris@13
|
35 /**
|
Chris@13
|
36 * @dataProvider getLevels
|
Chris@13
|
37 */
|
Chris@13
|
38 public function testErrorLevels($level, $type)
|
Chris@13
|
39 {
|
Chris@13
|
40 $e = new ErrorException('foo', 0, $level);
|
Chris@13
|
41 $this->assertContains('PHP ' . $type, $e->getMessage());
|
Chris@13
|
42 }
|
Chris@13
|
43
|
Chris@13
|
44 /**
|
Chris@13
|
45 * @dataProvider getLevels
|
Chris@13
|
46 */
|
Chris@13
|
47 public function testThrowException($level, $type)
|
Chris@13
|
48 {
|
Chris@13
|
49 try {
|
Chris@13
|
50 ErrorException::throwException($level, '{whot}', '{file}', '13');
|
Chris@13
|
51 } catch (ErrorException $e) {
|
Chris@13
|
52 $this->assertContains('PHP ' . $type, $e->getMessage());
|
Chris@13
|
53 $this->assertContains('{whot}', $e->getMessage());
|
Chris@13
|
54 $this->assertContains('in {file}', $e->getMessage());
|
Chris@13
|
55 $this->assertContains('on line 13', $e->getMessage());
|
Chris@13
|
56 }
|
Chris@13
|
57 }
|
Chris@13
|
58
|
Chris@13
|
59 public function getLevels()
|
Chris@13
|
60 {
|
Chris@13
|
61 return [
|
Chris@13
|
62 [E_WARNING, 'Warning'],
|
Chris@13
|
63 [E_CORE_WARNING, 'Warning'],
|
Chris@13
|
64 [E_COMPILE_WARNING, 'Warning'],
|
Chris@13
|
65 [E_USER_WARNING, 'Warning'],
|
Chris@13
|
66 [E_STRICT, 'Strict error'],
|
Chris@13
|
67 [0, 'Error'],
|
Chris@13
|
68 ];
|
Chris@13
|
69 }
|
Chris@13
|
70
|
Chris@13
|
71 /**
|
Chris@13
|
72 * @dataProvider getUserLevels
|
Chris@13
|
73 */
|
Chris@13
|
74 public function testThrowExceptionAsErrorHandler($level, $type)
|
Chris@13
|
75 {
|
Chris@13
|
76 set_error_handler(['Psy\Exception\ErrorException', 'throwException']);
|
Chris@13
|
77 try {
|
Chris@13
|
78 trigger_error('{whot}', $level);
|
Chris@13
|
79 } catch (ErrorException $e) {
|
Chris@13
|
80 $this->assertContains('PHP ' . $type, $e->getMessage());
|
Chris@13
|
81 $this->assertContains('{whot}', $e->getMessage());
|
Chris@13
|
82 }
|
Chris@13
|
83 restore_error_handler();
|
Chris@13
|
84 }
|
Chris@13
|
85
|
Chris@13
|
86 public function getUserLevels()
|
Chris@13
|
87 {
|
Chris@13
|
88 return [
|
Chris@13
|
89 [E_USER_ERROR, 'Error'],
|
Chris@13
|
90 [E_USER_WARNING, 'Warning'],
|
Chris@13
|
91 [E_USER_NOTICE, 'Notice'],
|
Chris@13
|
92 [E_USER_DEPRECATED, 'Deprecated'],
|
Chris@13
|
93 ];
|
Chris@13
|
94 }
|
Chris@13
|
95
|
Chris@13
|
96 public function testIgnoreExecutionLoopFilename()
|
Chris@13
|
97 {
|
Chris@13
|
98 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/ExecutionLoop.php');
|
Chris@13
|
99 $this->assertEmpty($e->getFile());
|
Chris@13
|
100
|
Chris@13
|
101 $e = new ErrorException('{{message}}', 0, 1, 'c:\fake\path\to\Psy\ExecutionLoop.php');
|
Chris@13
|
102 $this->assertEmpty($e->getFile());
|
Chris@13
|
103
|
Chris@13
|
104 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
|
Chris@13
|
105 $this->assertNotEmpty($e->getFile());
|
Chris@13
|
106 }
|
Chris@13
|
107 }
|