ExceptionConverterTest.php 2.7 KB

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