No Description

ErrorExceptionTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of Psy Shell
  4. *
  5. * (c) 2012-2014 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\Exception;
  12. use Psy\Exception\ErrorException;
  13. class ErrorExceptionTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testInstance()
  16. {
  17. $e = new ErrorException();
  18. $this->assertTrue($e instanceof Exception);
  19. $this->assertTrue($e instanceof \ErrorException);
  20. $this->assertTrue($e instanceof ErrorException);
  21. }
  22. public function testMessage()
  23. {
  24. $e = new ErrorException('foo');
  25. $this->assertContains('foo', $e->getMessage());
  26. $this->assertEquals('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->assertContains('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->assertContains('PHP ' . $type, $e->getMessage());
  45. $this->assertContains('{whot}', $e->getMessage());
  46. $this->assertContains('in {file}', $e->getMessage());
  47. $this->assertContains('on line 13', $e->getMessage());
  48. }
  49. }
  50. public function getLevels()
  51. {
  52. return array(
  53. array(E_WARNING, 'warning'),
  54. array(E_CORE_WARNING, 'warning'),
  55. array(E_COMPILE_WARNING, 'warning'),
  56. array(E_USER_WARNING, 'warning'),
  57. array(E_STRICT, 'Strict error'),
  58. array(0, 'error'),
  59. );
  60. }
  61. /**
  62. * @dataProvider getUserLevels
  63. */
  64. public function testThrowExceptionAsErrorHandler($level, $type)
  65. {
  66. set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
  67. try {
  68. trigger_error('{whot}', $level);
  69. } catch (ErrorException $e) {
  70. $this->assertContains('PHP ' . $type, $e->getMessage());
  71. $this->assertContains('{whot}', $e->getMessage());
  72. }
  73. restore_error_handler();
  74. }
  75. public function getUserLevels()
  76. {
  77. return array(
  78. array(E_USER_ERROR, 'error'),
  79. array(E_USER_WARNING, 'warning'),
  80. array(E_USER_NOTICE, 'error'),
  81. array(E_USER_DEPRECATED, 'error'),
  82. );
  83. }
  84. public function testIgnoreExecutionLoopFilename()
  85. {
  86. $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/ExecutionLoop/Loop.php');
  87. $this->assertEmpty($e->getFile());
  88. $e = new ErrorException('{{message}}', 0, 1, 'c:\fake\path\to\Psy\ExecutionLoop\Loop.php');
  89. $this->assertEmpty($e->getFile());
  90. $e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
  91. $this->assertNotEmpty($e->getFile());
  92. }
  93. }