ErrorExceptionTest.php 3.7 KB

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