ThrowUpExceptionTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\ThrowUpException;
  12. class ThrowUpExceptionTest extends \PHPUnit\Framework\TestCase
  13. {
  14. public function testException()
  15. {
  16. $previous = new \Exception('{{message}}', 123);
  17. $e = new ThrowUpException($previous);
  18. $this->assertInstanceOf('Psy\Exception\Exception', $e);
  19. $this->assertInstanceOf('Psy\Exception\ThrowUpException', $e);
  20. $this->assertEquals("Throwing Exception with message '{{message}}'", $e->getMessage());
  21. $this->assertEquals('{{message}}', $e->getRawMessage());
  22. $this->assertEquals(123, $e->getCode());
  23. $this->assertSame($previous, $e->getPrevious());
  24. }
  25. public function testFromThrowable()
  26. {
  27. $previous = new \Exception('{{message}}');
  28. $e = ThrowUpException::fromThrowable($previous);
  29. $this->assertInstanceOf('Psy\Exception\ThrowUpException', $e);
  30. $this->assertSame($previous, $e->getPrevious());
  31. }
  32. public function testFromThrowableWithError()
  33. {
  34. if (\version_compare(PHP_VERSION, '7.0.0', '<')) {
  35. $this->markTestSkipped();
  36. }
  37. $previous = new \Error('{{message}}');
  38. $e = ThrowUpException::fromThrowable($previous);
  39. $this->assertInstanceOf('Psy\Exception\ThrowUpException', $e);
  40. $this->assertInstanceOf('Psy\Exception\ErrorException', $e->getPrevious());
  41. $this->assertNotSame($previous, $e->getPrevious());
  42. $this->assertSame($previous, $e->getPrevious()->getPrevious());
  43. }
  44. /**
  45. * @expectedException \InvalidArgumentException
  46. * @expectedExceptionMessage throw-up can only throw Exceptions and Errors
  47. */
  48. public function testFromThrowableThrowsError()
  49. {
  50. $notThrowable = new \StdClass();
  51. ThrowUpException::fromThrowable($notThrowable);
  52. }
  53. }