WebdisConnectionTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. use PredisTestCase;
  12. /**
  13. * @group ext-curl
  14. * @group ext-phpiredis
  15. * @group realm-connection
  16. * @group realm-webdis
  17. * @requires extension phpiredis
  18. * @requires extension curl
  19. */
  20. class WebdisConnectionTest extends PredisTestCase
  21. {
  22. /**
  23. * @group disconnected
  24. */
  25. public function testIsConnectedAlwaysReturnsTrue()
  26. {
  27. $connection = $this->createConnection();
  28. $this->assertTrue($connection->isConnected());
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testSupportsSchemeUnix()
  34. {
  35. $connection = $this->createConnectionWithParams(array('scheme' => 'http'));
  36. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  37. }
  38. /**
  39. * @group disconnected
  40. * @expectedException \InvalidArgumentException
  41. * @expectedExceptionMessage Invalid scheme: 'tcp'.
  42. */
  43. public function testThrowsExceptionOnInvalidScheme()
  44. {
  45. $connection = $this->createConnectionWithParams(array('scheme' => 'tcp'));
  46. }
  47. /**
  48. * @group disconnected
  49. * @expectedException \Predis\NotSupportedException
  50. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::writeRequest() is not supported.
  51. */
  52. public function testWritingCommandsIsNotSupported()
  53. {
  54. $connection = $this->createConnection();
  55. $connection->writeRequest($this->getCurrentProfile()->createCommand('ping'));
  56. }
  57. /**
  58. * @group disconnected
  59. * @expectedException \Predis\NotSupportedException
  60. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::readResponse() is not supported
  61. */
  62. public function testReadingResponsesIsNotSupported()
  63. {
  64. $connection = $this->createConnection();
  65. $connection->readResponse($this->getCurrentProfile()->createCommand('ping'));
  66. }
  67. /**
  68. * @group disconnected
  69. * @expectedException \Predis\NotSupportedException
  70. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::read() is not supported.
  71. */
  72. public function testReadingFromConnectionIsNotSupported()
  73. {
  74. $connection = $this->createConnection();
  75. $connection->read();
  76. }
  77. /**
  78. * @group disconnected
  79. * @expectedException \Predis\NotSupportedException
  80. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::addConnectCommand() is not supported.
  81. */
  82. public function testAddingConnectCommandsIsNotSupported()
  83. {
  84. $connection = $this->createConnection();
  85. $connection->addConnectCommand($this->getCurrentProfile()->createCommand('ping'));
  86. }
  87. /**
  88. * @group disconnected
  89. * @expectedException \Predis\NotSupportedException
  90. * @expectedExceptionMessage Command 'SELECT' is not allowed by Webdis.
  91. */
  92. public function testRejectCommandSelect()
  93. {
  94. $connection = $this->createConnection();
  95. $connection->executeCommand($this->getCurrentProfile()->createCommand('select', array(0)));
  96. }
  97. /**
  98. * @group disconnected
  99. * @expectedException \Predis\NotSupportedException
  100. * @expectedExceptionMessage Command 'AUTH' is not allowed by Webdis.
  101. */
  102. public function testRejectCommandAuth()
  103. {
  104. $connection = $this->createConnection();
  105. $connection->executeCommand($this->getCurrentProfile()->createCommand('auth', array('foobar')));
  106. }
  107. /**
  108. * @group disconnected
  109. */
  110. public function testCanBeSerialized()
  111. {
  112. $parameters = $this->getParameters(array(
  113. 'alias' => 'redis',
  114. 'read_write_timeout' => 10,
  115. ));
  116. $connection = $this->createConnectionWithParams($parameters);
  117. $unserialized = unserialize(serialize($connection));
  118. $this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
  119. $this->assertEquals($parameters, $unserialized->getParameters());
  120. }
  121. // ******************************************************************** //
  122. // ---- INTEGRATION TESTS --------------------------------------------- //
  123. // ******************************************************************** //
  124. /**
  125. * @group connected
  126. */
  127. public function testExecutesMultipleCommandsOnServer()
  128. {
  129. $profile = $this->getCurrentProfile();
  130. $cmdPing = $profile->createCommand('ping');
  131. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  132. $cmdGet = $profile->createCommand('get', array('foobar'));
  133. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  134. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  135. $connection = $this->createConnection(true);
  136. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  137. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  138. $this->assertNull($connection->executeCommand($cmdGet));
  139. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  140. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  141. }
  142. /**
  143. * @medium
  144. * @group disconnected
  145. * @group slow
  146. * @expectedException \Predis\Connection\ConnectionException
  147. */
  148. public function testThrowExceptionWhenUnableToConnect()
  149. {
  150. $connection = $this->createConnectionWithParams(array('host' => '169.254.10.10'));
  151. $connection->executeCommand($this->getCurrentProfile()->createCommand('ping'));
  152. }
  153. // ******************************************************************** //
  154. // ---- HELPER METHODS ------------------------------------------------ //
  155. // ******************************************************************** //
  156. /**
  157. * Returns a named array with the default connection parameters and their values.
  158. *
  159. * @return array Default connection parameters.
  160. */
  161. protected function getDefaultParametersArray()
  162. {
  163. return array(
  164. 'scheme' => 'http',
  165. 'host' => WEBDIS_SERVER_HOST,
  166. 'port' => WEBDIS_SERVER_PORT,
  167. );
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. protected function createConnection()
  173. {
  174. return $this->createConnectionWithParams(array());
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. protected function createConnectionWithParams($parameters)
  180. {
  181. $profile = $this->getCurrentProfile();
  182. if (!$parameters instanceof ParametersInterface) {
  183. $parameters = $this->getParameters($parameters);
  184. }
  185. $connection = new WebdisConnection($parameters);
  186. $connection->executeCommand($profile->createCommand('flushdb'));
  187. return $connection;
  188. }
  189. }