WebdisConnectionTest.php 6.9 KB

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