CommunicationExceptionTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use Predis\Connection\SingleConnectionInterface;
  13. /**
  14. *
  15. */
  16. class CommunicationExceptionTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testExceptionMessage()
  22. {
  23. $message = 'Connection error message.';
  24. $connection = $this->getMockedConnectionBase();
  25. $exception = $this->getException($connection, $message);
  26. $this->setExpectedException('Predis\CommunicationException', $message);
  27. throw $exception;
  28. }
  29. /**
  30. * @group disconnected
  31. */
  32. public function testExceptionConnection()
  33. {
  34. $connection = $this->getMockedConnectionBase();
  35. $exception = $this->getException($connection, 'ERROR MESSAGE');
  36. $this->assertSame($connection, $exception->getConnection());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testShouldResetConnection()
  42. {
  43. $connection = $this->getMockedConnectionBase();
  44. $exception = $this->getException($connection, 'ERROR MESSAGE');
  45. $this->assertTrue($exception->shouldResetConnection());
  46. }
  47. /**
  48. * @group disconnected
  49. * @expectedException Predis\CommunicationException
  50. * @expectedExceptionMessage Communication error
  51. */
  52. public function testCommunicationExceptionHandling()
  53. {
  54. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  55. $connection->expects($this->once())->method('isConnected')->will($this->returnValue(true));
  56. $connection->expects($this->once())->method('disconnect');
  57. $exception = $this->getException($connection, 'Communication error');
  58. CommunicationException::handle($exception);
  59. }
  60. // ******************************************************************** //
  61. // ---- HELPER METHODS ------------------------------------------------ //
  62. // ******************************************************************** //
  63. /**
  64. * Returns a mocked connection instance.
  65. *
  66. * @param mixed $parameters Connection parameters.
  67. * @return SingleConnectionInterface
  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 ConnectionParametersInterface) {
  75. $parameters = new ConnectionParameters($parameters);
  76. }
  77. return $builder->getMockForAbstractClass(array($parameters));
  78. }
  79. /**
  80. * Returns a connection exception instance.
  81. *
  82. * @param Connection\SingleConnectionInterface $connection Connection instance.
  83. * @param string $message Exception message.
  84. * @param int $code Exception code.
  85. * @param \Exception $inner Inner exception.
  86. * @return \Exception
  87. */
  88. protected function getException(SingleConnectionInterface $connection, $message, $code = 0, \Exception $inner = null)
  89. {
  90. $arguments = array($connection, $message, $code, $inner);
  91. $mock = $this->getMockForAbstractClass('Predis\CommunicationException', $arguments);
  92. return $mock;
  93. }
  94. }