4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Test\Exception;
14 use Psy\Exception\ErrorException;
16 class ErrorExceptionTest extends \PHPUnit\Framework\TestCase
18 public function testInstance()
20 $e = new ErrorException();
22 $this->assertInstanceOf('Psy\Exception\Exception', $e);
23 $this->assertInstanceOf('ErrorException', $e);
24 $this->assertInstanceOf('Psy\Exception\ErrorException', $e);
27 public function testMessage()
29 $e = new ErrorException('foo');
31 $this->assertContains('foo', $e->getMessage());
32 $this->assertSame('foo', $e->getRawMessage());
36 * @dataProvider getLevels
38 public function testErrorLevels($level, $type)
40 $e = new ErrorException('foo', 0, $level);
41 $this->assertContains('PHP ' . $type, $e->getMessage());
45 * @dataProvider getLevels
47 public function testThrowException($level, $type)
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());
59 public function getLevels()
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'],
75 * @dataProvider getUserLevels
77 public function testThrowExceptionAsErrorHandler($level, $type)
79 set_error_handler(['Psy\Exception\ErrorException', 'throwException']);
81 trigger_error('{whot}', $level);
82 } catch (ErrorException $e) {
83 $this->assertContains('PHP ' . $type, $e->getMessage());
84 $this->assertContains('{whot}', $e->getMessage());
86 restore_error_handler();
89 public function getUserLevels()
92 [E_USER_ERROR, 'Error'],
93 [E_USER_WARNING, 'Warning'],
94 [E_USER_NOTICE, 'Notice'],
95 [E_USER_DEPRECATED, 'Deprecated'],
99 public function testIgnoreExecutionLoopFilename()
101 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/ExecutionLoop.php');
102 $this->assertEmpty($e->getFile());
104 $e = new ErrorException('{{message}}', 0, 1, 'c:\fake\path\to\Psy\ExecutionLoop.php');
105 $this->assertEmpty($e->getFile());
107 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
108 $this->assertNotEmpty($e->getFile());
111 public function testFromError()
113 if (version_compare(PHP_VERSION, '7.0.0', '<')) {
114 $this->markTestSkipped();
117 $error = new \Error('{{message}}', 0);
118 $exception = ErrorException::fromError($error);
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);