ExceptionConverterTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\InvalidArgumentTypeException('message', 2, 'foo'),
  78. 'MongoException',
  79. ],
  80. [
  81. new \MongoDB\Exception\UnexpectedTypeException('message', 2),
  82. 'MongoException',
  83. ],
  84. [
  85. new \MongoDB\Exception\UnexpectedValueException('message', 2),
  86. 'MongoException',
  87. ],
  88. [
  89. new \MongoDB\Exception\UnexpectedValueTypeException('message', 2, 'foo'),
  90. 'MongoException',
  91. ],
  92. ];
  93. }
  94. }