ExceptionConverterTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\Exception;
  4. use Alcaeus\MongoDbAdapter\ExceptionConverter;
  5. use PHPUnit\Framework\TestCase;
  6. class ExceptionConverterTest extends TestCase
  7. {
  8. /**
  9. * @dataProvider exceptionProvider
  10. */
  11. public function testConvertException($e, $expectedClass)
  12. {
  13. $exception = ExceptionConverter::toLegacy($e);
  14. $this->assertInstanceOf($expectedClass, $exception);
  15. $this->assertSame($e->getMessage(), $exception->getMessage());
  16. $this->assertSame($e->getCode(), $exception->getCode());
  17. $this->assertSame($e, $exception->getPrevious());
  18. }
  19. public function exceptionProvider()
  20. {
  21. return [
  22. // Driver
  23. [
  24. new Exception\AuthenticationException('message', 1),
  25. 'MongoConnectionException',
  26. ],
  27. [
  28. new Exception\BulkWriteException('message', 2),
  29. 'MongoCursorException',
  30. ],
  31. [
  32. new Exception\ConnectionException('message', 2),
  33. 'MongoConnectionException',
  34. ],
  35. [
  36. new Exception\ConnectionTimeoutException('message', 2),
  37. 'MongoConnectionException',
  38. ],
  39. [
  40. new Exception\ExecutionTimeoutException('message', 2),
  41. 'MongoExecutionTimeoutException',
  42. ],
  43. [
  44. new Exception\InvalidArgumentException('message', 2),
  45. 'MongoException',
  46. ],
  47. [
  48. new Exception\LogicException('message', 2),
  49. 'MongoException',
  50. ],
  51. [
  52. new Exception\RuntimeException('message', 2),
  53. 'MongoException',
  54. ],
  55. [
  56. new Exception\SSLConnectionException('message', 2),
  57. 'MongoConnectionException',
  58. ],
  59. [
  60. new Exception\UnexpectedValueException('message', 2),
  61. 'MongoException',
  62. ],
  63. // Library
  64. [
  65. new \MongoDB\Exception\BadMethodCallException('message', 2),
  66. 'MongoException',
  67. ],
  68. [
  69. new \MongoDB\Exception\InvalidArgumentException('message', 2),
  70. 'MongoException',
  71. ],
  72. [
  73. new \MongoDB\Exception\UnexpectedValueException('message', 2),
  74. 'MongoException',
  75. ],
  76. ];
  77. }
  78. }