ErrorExceptionTest.php 3.7 KB

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