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\Network\IConnectionSingle;
  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 IConnectionSingle
  55. */
  56. protected function getMockedConnectionBase($parameters = null)
  57. {
  58. $builder = $this->getMockBuilder('Predis\Network\ConnectionBase');
  59. if ($parameters === null) {
  60. $builder->disableOriginalConstructor();
  61. }
  62. else if (!$parameters instanceof IConnectionParameters) {
  63. $parameters = new ConnectionParameters($parameters);
  64. }
  65. return $builder->getMockForAbstractClass(array($parameters));
  66. }
  67. /**
  68. * Returns a connection exception instance.
  69. *
  70. * @param IConnectionSingle $message Connection instance.
  71. * @param string $message Exception message.
  72. * @param int $code Exception code.
  73. * @param \Exception $inner Inner exception.
  74. * @return \Exception
  75. */
  76. protected function getException(IConnectionSingle $connection, $message, $code = 0, \Exception $inner = null)
  77. {
  78. $arguments = array($connection, $message, $code, $inner);
  79. $mock = $this->getMockForAbstractClass('Predis\CommunicationException', $arguments);
  80. return $mock;
  81. }
  82. }