4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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;
15 use Psy\Exception\Exception;
17 class ErrorExceptionTest extends \PHPUnit_Framework_TestCase
19 public function testInstance()
21 $e = new ErrorException();
23 $this->assertTrue($e instanceof Exception);
24 $this->assertTrue($e instanceof \ErrorException);
25 $this->assertTrue($e instanceof ErrorException);
28 public function testMessage()
30 $e = new ErrorException('foo');
32 $this->assertContains('foo', $e->getMessage());
33 $this->assertEquals('foo', $e->getRawMessage());
37 * @dataProvider getLevels
39 public function testErrorLevels($level, $type)
41 $e = new ErrorException('foo', 0, $level);
42 $this->assertContains('PHP ' . $type, $e->getMessage());
46 * @dataProvider getLevels
48 public function testThrowException($level, $type)
51 ErrorException::throwException($level, '{whot}', '{file}', '13');
52 } catch (ErrorException $e) {
53 $this->assertContains('PHP ' . $type, $e->getMessage());
54 $this->assertContains('{whot}', $e->getMessage());
55 $this->assertContains('in {file}', $e->getMessage());
56 $this->assertContains('on line 13', $e->getMessage());
60 public function getLevels()
63 array(E_WARNING, 'warning'),
64 array(E_CORE_WARNING, 'warning'),
65 array(E_COMPILE_WARNING, 'warning'),
66 array(E_USER_WARNING, 'warning'),
67 array(E_STRICT, 'Strict error'),
73 * @dataProvider getUserLevels
75 public function testThrowExceptionAsErrorHandler($level, $type)
77 set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
79 trigger_error('{whot}', $level);
80 } catch (ErrorException $e) {
81 $this->assertContains('PHP ' . $type, $e->getMessage());
82 $this->assertContains('{whot}', $e->getMessage());
84 restore_error_handler();
87 public function getUserLevels()
90 array(E_USER_ERROR, 'error'),
91 array(E_USER_WARNING, 'warning'),
92 array(E_USER_NOTICE, 'error'),
93 array(E_USER_DEPRECATED, 'error'),
97 public function testIgnoreExecutionLoopFilename()
99 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/ExecutionLoop/Loop.php');
100 $this->assertEmpty($e->getFile());
102 $e = new ErrorException('{{message}}', 0, 1, 'c:\fake\path\to\Psy\ExecutionLoop\Loop.php');
103 $this->assertEmpty($e->getFile());
105 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
106 $this->assertNotEmpty($e->getFile());