ExceptionConverterTest.php 2.5 KB

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