WebdisConnectionTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. use Predis\Profile;
  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. */
  84. public function testAddingConnectCommandsIsNotSupported()
  85. {
  86. $connection = new WebdisConnection($this->getParameters());
  87. $connection->addConnectCommand($this->getProfile()->createCommand('ping'));
  88. }
  89. /**
  90. * @group disconnected
  91. * @expectedException \Predis\NotSupportedException
  92. * @expectedExceptionMessage Command 'SELECT' is not allowed by Webdis.
  93. */
  94. public function testRejectCommandSelect()
  95. {
  96. $connection = new WebdisConnection($this->getParameters());
  97. $connection->executeCommand($this->getProfile()->createCommand('select', array(0)));
  98. }
  99. /**
  100. * @group disconnected
  101. * @expectedException \Predis\NotSupportedException
  102. * @expectedExceptionMessage Command 'AUTH' is not allowed by Webdis.
  103. */
  104. public function testRejectCommandAuth()
  105. {
  106. $connection = new WebdisConnection($this->getParameters());
  107. $connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar')));
  108. }
  109. /**
  110. * @group disconnected
  111. */
  112. public function testCanBeSerialized()
  113. {
  114. $parameters = $this->getParameters(array('alias' => 'webdis'));
  115. $connection = new WebdisConnection($parameters);
  116. $unserialized = unserialize(serialize($connection));
  117. $this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
  118. $this->assertEquals($parameters, $unserialized->getParameters());
  119. }
  120. // ******************************************************************** //
  121. // ---- INTEGRATION TESTS --------------------------------------------- //
  122. // ******************************************************************** //
  123. /**
  124. * @group connected
  125. */
  126. public function testExecutesCommandsOnServer()
  127. {
  128. $connection = $this->getConnection($profile);
  129. $cmdPing = $profile->createCommand('ping');
  130. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  131. $cmdGet = $profile->createCommand('get', array('foobar'));
  132. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  133. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  134. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  135. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  136. $this->assertNull($connection->executeCommand($cmdGet));
  137. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  138. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  139. }
  140. /**
  141. * @medium
  142. * @group disconnected
  143. * @group slow
  144. * @expectedException \Predis\Connection\ConnectionException
  145. */
  146. public function testThrowExceptionWhenUnableToConnect()
  147. {
  148. $parameters = $this->getParameters(array('host' => '169.254.10.10'));
  149. $connection = new WebdisConnection($parameters);
  150. $connection->executeCommand($this->getProfile()->createCommand('ping'));
  151. }
  152. // ******************************************************************** //
  153. // ---- HELPER METHODS ------------------------------------------------ //
  154. // ******************************************************************** //
  155. /**
  156. * Returns a named array with the default connection parameters and their values.
  157. *
  158. * @return array Default connection parameters.
  159. */
  160. protected function getDefaultParametersArray()
  161. {
  162. return array(
  163. 'scheme' => 'http',
  164. 'host' => WEBDIS_SERVER_HOST,
  165. 'port' => WEBDIS_SERVER_PORT,
  166. );
  167. }
  168. /**
  169. * Returns a new instance of a connection instance.
  170. *
  171. * @param mixed $profile Redis profile.
  172. * @param array $parameters Additional connection parameters.
  173. *
  174. * @return WebdisConnection
  175. */
  176. protected function getConnection(&$profile = null, array $parameters = array())
  177. {
  178. $parameters = $this->getParameters($parameters);
  179. $profile = $this->getProfile();
  180. $connection = new WebdisConnection($parameters);
  181. $connection->executeCommand($profile->createCommand('flushdb'));
  182. return $connection;
  183. }
  184. }