ThrowUpExceptionTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. use Psy\Exception\ThrowUpException;
  14. class ThrowUpExceptionTest extends \Psy\Test\TestCase
  15. {
  16. public function testException()
  17. {
  18. $previous = new \Exception('{{message}}', 123);
  19. $e = new ThrowUpException($previous);
  20. $this->assertInstanceOf(Exception::class, $e);
  21. $this->assertInstanceOf(ThrowUpException::class, $e);
  22. $this->assertSame("Throwing Exception with message '{{message}}'", $e->getMessage());
  23. $this->assertSame('{{message}}', $e->getRawMessage());
  24. $this->assertSame(123, $e->getCode());
  25. $this->assertSame($previous, $e->getPrevious());
  26. }
  27. public function testFromThrowable()
  28. {
  29. $previous = new \Exception('{{message}}');
  30. $e = ThrowUpException::fromThrowable($previous);
  31. $this->assertInstanceOf(ThrowUpException::class, $e);
  32. $this->assertSame($previous, $e->getPrevious());
  33. }
  34. public function testFromThrowableWithError()
  35. {
  36. $previous = new \Error('{{message}}');
  37. $e = ThrowUpException::fromThrowable($previous);
  38. $this->assertInstanceOf(ThrowUpException::class, $e);
  39. $this->assertInstanceOf(ErrorException::class, $e->getPrevious());
  40. $this->assertNotSame($previous, $e->getPrevious());
  41. $this->assertSame($previous, $e->getPrevious()->getPrevious());
  42. }
  43. public function testFromThrowableThrowsError()
  44. {
  45. $this->expectException(\InvalidArgumentException::class);
  46. $this->expectExceptionMessage('throw-up can only throw Exceptions and Errors');
  47. $notThrowable = new \stdClass();
  48. ThrowUpException::fromThrowable($notThrowable);
  49. $this->fail();
  50. }
  51. }