CommunicationExceptionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class CommunicationExceptionTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testExceptionReturnsInnerConnection()
  21. {
  22. $connection = $this->getMockConnection();
  23. $exception = $this->createMockException($connection, 'Communication error message');
  24. $this->assertSame($connection, $exception->getConnection());
  25. }
  26. /**
  27. * @group disconnected
  28. * @expectedException \Predis\CommunicationException
  29. * @expectedExceptionMessage Connection error message
  30. */
  31. public function testExceptionMessage()
  32. {
  33. $connection = $this->getMockConnection();
  34. $exception = $this->createMockException($connection, 'Connection error message');
  35. throw $exception;
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testShouldResetConnectionIsTrue()
  41. {
  42. $connection = $this->getMockConnection();
  43. $exception = $this->createMockException($connection, 'Communication error message');
  44. $this->assertTrue($exception->shouldResetConnection());
  45. }
  46. /**
  47. * @group disconnected
  48. * @expectedException \Predis\CommunicationException
  49. * @expectedExceptionMessage Communication error message
  50. */
  51. public function testCommunicationExceptionHandling()
  52. {
  53. $connection = $this->getMockConnection();
  54. $connection
  55. ->expects($this->once())
  56. ->method('isConnected')
  57. ->will($this->returnValue(true));
  58. $connection
  59. ->expects($this->once())
  60. ->method('disconnect');
  61. $exception = $this->createMockException($connection, 'Communication error message');
  62. CommunicationException::handle($exception);
  63. }
  64. /**
  65. * @group disconnected
  66. * @expectedException \Predis\CommunicationException
  67. * @expectedExceptionMessage Communication error message
  68. */
  69. public function testCommunicationExceptionHandlingWhenShouldResetConnectionIsFalse()
  70. {
  71. $connection = $this->getMockConnection();
  72. $connection
  73. ->expects($this->never())
  74. ->method('isConnected');
  75. $connection
  76. ->expects($this->never())
  77. ->method('disconnect');
  78. $exception = $this->getMockBuilder('Predis\CommunicationException')
  79. ->setConstructorArgs(array($connection, 'Communication error message'))
  80. ->setMethods(array('shouldResetConnection'))
  81. ->getMockForAbstractClass();
  82. $exception
  83. ->expects($this->once())
  84. ->method('shouldResetConnection')
  85. ->will($this->returnValue(false));
  86. CommunicationException::handle($exception);
  87. }
  88. // ******************************************************************** //
  89. // ---- HELPER METHODS ------------------------------------------------ //
  90. // ******************************************************************** //
  91. /**
  92. * Returns a connection exception instance.
  93. *
  94. * @param Connection\NodeConnectionInterface $connection Connection instance.
  95. * @param string $message Exception message.
  96. * @param int $code Exception code.
  97. * @param \Exception $inner Inner exception.
  98. *
  99. * @return \Predis\CommunicationException
  100. */
  101. protected function createMockException(
  102. Connection\NodeConnectionInterface $connection,
  103. $message,
  104. $code = 0,
  105. \Exception $inner = null
  106. ) {
  107. return $this->getMockBuilder('Predis\CommunicationException')
  108. ->setConstructorArgs(array($connection, $message, $code, $inner))
  109. ->getMockForAbstractClass();
  110. }
  111. }