ExceptionTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Test case for Exceptions.
  4. */
  5. declare(strict_types=1);
  6. namespace WebSocket;
  7. use PHPUnit\Framework\TestCase;
  8. use Throwable;
  9. class ExceptionTest extends TestCase
  10. {
  11. public function setUp(): void
  12. {
  13. error_reporting(-1);
  14. }
  15. public function testConnectionException(): void
  16. {
  17. try {
  18. throw new ConnectionException(
  19. 'An error message',
  20. ConnectionException::EOF,
  21. ['test' => 'with data'],
  22. new TimeoutException(
  23. 'Nested exception',
  24. ConnectionException::TIMED_OUT
  25. )
  26. );
  27. } catch (Throwable $e) {
  28. }
  29. $this->assertInstanceOf('WebSocket\ConnectionException', $e);
  30. $this->assertInstanceOf('WebSocket\Exception', $e);
  31. $this->assertInstanceOf('Exception', $e);
  32. $this->assertInstanceOf('Throwable', $e);
  33. $this->assertEquals('An error message', $e->getMessage());
  34. $this->assertEquals(1025, $e->getCode());
  35. $this->assertEquals(['test' => 'with data'], $e->getData());
  36. $p = $e->getPrevious();
  37. $this->assertInstanceOf('WebSocket\TimeoutException', $p);
  38. $this->assertInstanceOf('WebSocket\ConnectionException', $p);
  39. $this->assertEquals('Nested exception', $p->getMessage());
  40. $this->assertEquals(1024, $p->getCode());
  41. $this->assertEquals([], $p->getData());
  42. }
  43. }