CommunicationExceptionTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Connection\SingleConnectionInterface;
  13. /**
  14. *
  15. */
  16. class CommunicationExceptionTest extends StandardTestCase
  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. // ---- HELPER METHODS ------------------------------------------------ //
  49. // ******************************************************************** //
  50. /**
  51. * Returns a mocked connection instance.
  52. *
  53. * @param mixed $parameters Connection parameters.
  54. * @return SingleConnectionInterface
  55. */
  56. protected function getMockedConnectionBase($parameters = null)
  57. {
  58. $builder = $this->getMockBuilder('Predis\Connection\AbstractConnection');
  59. if ($parameters === null) {
  60. $builder->disableOriginalConstructor();
  61. } else if (!$parameters instanceof ConnectionParametersInterface) {
  62. $parameters = new ConnectionParameters($parameters);
  63. }
  64. return $builder->getMockForAbstractClass(array($parameters));
  65. }
  66. /**
  67. * Returns a connection exception instance.
  68. *
  69. * @param SingleConnectionInterface $message Connection instance.
  70. * @param string $message Exception message.
  71. * @param int $code Exception code.
  72. * @param \Exception $inner Inner exception.
  73. * @return \Exception
  74. */
  75. protected function getException(SingleConnectionInterface $connection, $message, $code = 0, \Exception $inner = null)
  76. {
  77. $arguments = array($connection, $message, $code, $inner);
  78. $mock = $this->getMockForAbstractClass('Predis\CommunicationException', $arguments);
  79. return $mock;
  80. }
  81. }