annotate vendor/psy/psysh/test/Exception/ErrorExceptionTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
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@16 62 [E_WARNING, 'Warning'],
Chris@16 63 [E_CORE_WARNING, 'Warning'],
Chris@16 64 [E_COMPILE_WARNING, 'Warning'],
Chris@16 65 [E_USER_WARNING, 'Warning'],
Chris@16 66 [E_STRICT, 'Strict error'],
Chris@16 67 [E_DEPRECATED, 'Deprecated'],
Chris@16 68 [E_USER_DEPRECATED, 'Deprecated'],
Chris@16 69 [E_RECOVERABLE_ERROR, 'Recoverable fatal error'],
Chris@16 70 [0, 'Error'],
Chris@13 71 ];
Chris@13 72 }
Chris@13 73
Chris@13 74 /**
Chris@13 75 * @dataProvider getUserLevels
Chris@13 76 */
Chris@13 77 public function testThrowExceptionAsErrorHandler($level, $type)
Chris@13 78 {
Chris@17 79 \set_error_handler(['Psy\Exception\ErrorException', 'throwException']);
Chris@13 80 try {
Chris@17 81 \trigger_error('{whot}', $level);
Chris@13 82 } catch (ErrorException $e) {
Chris@13 83 $this->assertContains('PHP ' . $type, $e->getMessage());
Chris@13 84 $this->assertContains('{whot}', $e->getMessage());
Chris@13 85 }
Chris@17 86 \restore_error_handler();
Chris@13 87 }
Chris@13 88
Chris@13 89 public function getUserLevels()
Chris@13 90 {
Chris@13 91 return [
Chris@13 92 [E_USER_ERROR, 'Error'],
Chris@13 93 [E_USER_WARNING, 'Warning'],
Chris@13 94 [E_USER_NOTICE, 'Notice'],
Chris@13 95 [E_USER_DEPRECATED, 'Deprecated'],
Chris@13 96 ];
Chris@13 97 }
Chris@13 98
Chris@13 99 public function testIgnoreExecutionLoopFilename()
Chris@13 100 {
Chris@13 101 $e = new ErrorException('{{message}}', 0, 1, '/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, 'c:\fake\path\to\Psy\ExecutionLoop.php');
Chris@13 105 $this->assertEmpty($e->getFile());
Chris@13 106
Chris@13 107 $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
Chris@13 108 $this->assertNotEmpty($e->getFile());
Chris@13 109 }
Chris@16 110
Chris@16 111 public function testFromError()
Chris@16 112 {
Chris@17 113 if (\version_compare(PHP_VERSION, '7.0.0', '<')) {
Chris@16 114 $this->markTestSkipped();
Chris@16 115 }
Chris@16 116
Chris@16 117 $error = new \Error('{{message}}', 0);
Chris@16 118 $exception = ErrorException::fromError($error);
Chris@16 119
Chris@16 120 $this->assertContains('PHP Error: {{message}}', $exception->getMessage());
Chris@16 121 $this->assertEquals(0, $exception->getCode());
Chris@16 122 $this->assertEquals($error->getFile(), $exception->getFile());
Chris@16 123 $this->assertSame($exception->getPrevious(), $error);
Chris@16 124 }
Chris@13 125 }