CommunicationExceptionTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 testExceptionMessage()
  21. {
  22. $message = 'Connection error message.';
  23. $connection = $this->getMockedConnectionBase();
  24. $exception = $this->getException($connection, $message);
  25. $this->setExpectedException('Predis\CommunicationException', $message);
  26. throw $exception;
  27. }
  28. /**
  29. * @group disconnected
  30. */
  31. public function testExceptionConnection()
  32. {
  33. $connection = $this->getMockedConnectionBase();
  34. $exception = $this->getException($connection, 'ERROR MESSAGE');
  35. $this->assertSame($connection, $exception->getConnection());
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testShouldResetConnection()
  41. {
  42. $connection = $this->getMockedConnectionBase();
  43. $exception = $this->getException($connection, 'ERROR MESSAGE');
  44. $this->assertTrue($exception->shouldResetConnection());
  45. }
  46. /**
  47. * @group disconnected
  48. * @expectedException \Predis\CommunicationException
  49. * @expectedExceptionMessage Communication error
  50. */
  51. public function testCommunicationExceptionHandling()
  52. {
  53. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  54. $connection->expects($this->once())->method('isConnected')->will($this->returnValue(true));
  55. $connection->expects($this->once())->method('disconnect');
  56. $exception = $this->getException($connection, 'Communication error');
  57. CommunicationException::handle($exception);
  58. }
  59. // ******************************************************************** //
  60. // ---- HELPER METHODS ------------------------------------------------ //
  61. // ******************************************************************** //
  62. /**
  63. * Returns a mocked connection instance.
  64. *
  65. * @param mixed $parameters Connection parameters.
  66. *
  67. * @return Connection\NodeConnectionInterface
  68. */
  69. protected function getMockedConnectionBase($parameters = null)
  70. {
  71. $builder = $this->getMockBuilder('Predis\Connection\AbstractConnection');
  72. if ($parameters === null) {
  73. $builder->disableOriginalConstructor();
  74. } elseif (!$parameters instanceof Connection\ParametersInterface) {
  75. $parameters = new Connection\Parameters($parameters);
  76. }
  77. return $builder->getMockForAbstractClass(array($parameters));
  78. }
  79. /**
  80. * Returns a connection exception instance.
  81. *
  82. * @param Connection\NodeConnectionInterface $connection Connection instance.
  83. * @param string $message Exception message.
  84. * @param int $code Exception code.
  85. * @param \Exception $inner Inner exception.
  86. *
  87. * @return \Predis\CommunicationException
  88. */
  89. protected function getException(
  90. Connection\NodeConnectionInterface $connection,
  91. $message,
  92. $code = 0,
  93. \Exception $inner = null
  94. ) {
  95. $arguments = array($connection, $message, $code, $inner);
  96. $mock = $this->getMockForAbstractClass('Predis\CommunicationException', $arguments);
  97. return $mock;
  98. }
  99. }