annotate vendor/psy/psysh/test/Exception/ErrorExceptionTest.php @ 5:12f9dff5fda9 tip

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