PhpiredisSocketConnectionTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Connection;
  11. /**
  12. * @group ext-phpiredis
  13. * @requires extension phpiredis
  14. */
  15. class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorDoesNotOpenConnection()
  21. {
  22. $connection = new PhpiredisSocketConnection($this->getParameters());
  23. $this->assertFalse($connection->isConnected());
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testSupportsSchemeTCP()
  29. {
  30. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  31. $connection = new PhpiredisSocketConnection($parameters);
  32. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testSupportsSchemeRedis()
  38. {
  39. $parameters = $this->getParameters(array('scheme' => 'redis'));
  40. $connection = new PhpiredisSocketConnection($parameters);
  41. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testSupportsSchemeUnix()
  47. {
  48. $parameters = $this->getParameters(array('scheme' => 'unix'));
  49. $connection = new PhpiredisSocketConnection($parameters);
  50. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  51. }
  52. /**
  53. * @group disconnected
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage Invalid scheme: 'udp'.
  56. */
  57. public function testThrowsExceptionOnInvalidScheme()
  58. {
  59. $parameters = $this->getParameters(array('scheme' => 'udp'));
  60. new PhpiredisSocketConnection($parameters);
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testExposesParameters()
  66. {
  67. $parameters = $this->getParameters();
  68. $connection = new PhpiredisSocketConnection($parameters);
  69. $this->assertSame($parameters, $connection->getParameters());
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testCanBeSerialized()
  75. {
  76. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  77. $connection = new PhpiredisSocketConnection($parameters);
  78. $unserialized = unserialize(serialize($connection));
  79. $this->assertInstanceOf('Predis\Connection\PhpiredisSocketConnection', $unserialized);
  80. $this->assertEquals($parameters, $unserialized->getParameters());
  81. }
  82. // ******************************************************************** //
  83. // ---- INTEGRATION TESTS --------------------------------------------- //
  84. // ******************************************************************** //
  85. /**
  86. * @group connected
  87. */
  88. public function testExecutesCommandsOnServer()
  89. {
  90. $connection = $this->getConnection($profile, true);
  91. $cmdPing = $profile->createCommand('ping');
  92. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  93. $cmdGet = $profile->createCommand('get', array('foobar'));
  94. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  95. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  96. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  97. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  98. $this->assertNull($connection->executeCommand($cmdGet));
  99. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  100. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  101. }
  102. /**
  103. * @group connected
  104. * @expectedException \Predis\Connection\ConnectionException
  105. * @expectedExceptionMessage Cannot resolve the address of 'bogus.tld'.
  106. */
  107. public function testThrowsExceptionOnUnresolvableHostname()
  108. {
  109. $parameters = $this->getParameters(array('host' => 'bogus.tld'));
  110. $connection = new PhpiredisSocketConnection($parameters);
  111. $connection->connect();
  112. }
  113. /**
  114. * @group connected
  115. * @expectedException \Predis\Protocol\ProtocolException
  116. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  117. */
  118. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  119. {
  120. $connection = $this->getConnection($profile);
  121. $socket = $connection->getResource();
  122. $connection->writeRequest($profile->createCommand('ping'));
  123. socket_read($socket, 1);
  124. $connection->read();
  125. }
  126. // ******************************************************************** //
  127. // ---- HELPER METHODS ------------------------------------------------ //
  128. // ******************************************************************** //
  129. /**
  130. * {@inheritdoc}
  131. */
  132. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  133. {
  134. $parameters = $this->getParameters($parameters);
  135. $profile = $this->getProfile();
  136. $connection = new PhpiredisSocketConnection($parameters);
  137. if ($initialize) {
  138. $connection->addConnectCommand(
  139. $profile->createCommand('select', array($parameters->database))
  140. );
  141. $connection->addConnectCommand(
  142. $profile->createCommand('flushdb')
  143. );
  144. }
  145. return $connection;
  146. }
  147. }