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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Profile\ServerProfile;
  13. /**
  14. * @group ext-curl
  15. * @group ext-phpiredis
  16. * @group realm-connection
  17. * @group realm-webdis
  18. */
  19. class WebdisConnectionTest extends StandardTestCase
  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. * @expectedException Predis\NotSupportedException
  32. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::writeCommand() is not supported
  33. */
  34. public function testWritingCommandsIsNotSupported()
  35. {
  36. $connection = new WebdisConnection($this->getParameters());
  37. $connection->writeCommand($this->getProfile()->createCommand('ping'));
  38. }
  39. /**
  40. * @group disconnected
  41. * @expectedException Predis\NotSupportedException
  42. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::readResponse() is not supported
  43. */
  44. public function testReadingResponsesIsNotSupported()
  45. {
  46. $connection = new WebdisConnection($this->getParameters());
  47. $connection->readResponse($this->getProfile()->createCommand('ping'));
  48. }
  49. /**
  50. * @group disconnected
  51. * @expectedException Predis\NotSupportedException
  52. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::read() is not supported
  53. */
  54. public function testReadingFromConnectionIsNotSupported()
  55. {
  56. $connection = new WebdisConnection($this->getParameters());
  57. $connection->read();
  58. }
  59. /**
  60. * @group disconnected
  61. * @expectedException Predis\NotSupportedException
  62. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::pushInitCommand() is not supported
  63. */
  64. public function testPushingInitCommandsIsNotSupported()
  65. {
  66. $connection = new WebdisConnection($this->getParameters());
  67. $connection->pushInitCommand($this->getProfile()->createCommand('ping'));
  68. }
  69. /**
  70. * @group disconnected
  71. * @expectedException Predis\NotSupportedException
  72. * @expectedExceptionMessage Disabled command: SELECT
  73. */
  74. public function testRejectCommandSelect()
  75. {
  76. $connection = new WebdisConnection($this->getParameters());
  77. $connection->executeCommand($this->getProfile()->createCommand('select', array(0)));
  78. }
  79. /**
  80. * @group disconnected
  81. * @expectedException Predis\NotSupportedException
  82. * @expectedExceptionMessage Disabled command: AUTH
  83. */
  84. public function testRejectCommandAuth()
  85. {
  86. $connection = new WebdisConnection($this->getParameters());
  87. $connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar')));
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testCanBeSerialized()
  93. {
  94. $parameters = $this->getParameters(array('alias' => 'webdis'));
  95. $connection = new WebdisConnection($parameters);
  96. $unserialized = unserialize(serialize($connection));
  97. $this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
  98. $this->assertEquals($parameters, $unserialized->getParameters());
  99. }
  100. // ******************************************************************** //
  101. // ---- INTEGRATION TESTS --------------------------------------------- //
  102. // ******************************************************************** //
  103. /**
  104. * @group connected
  105. */
  106. public function testExecutesCommandsOnServer()
  107. {
  108. $connection = $this->getConnection($profile);
  109. $cmdPing = $profile->createCommand('ping');
  110. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  111. $cmdGet = $profile->createCommand('get', array('foobar'));
  112. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  113. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  114. $this->assertSame('PONG', $connection->executeCommand($cmdPing));
  115. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  116. $this->assertNull($connection->executeCommand($cmdGet));
  117. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  118. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  119. }
  120. /**
  121. * @group disconnected
  122. * @group slow
  123. * @expectedException Predis\Connection\ConnectionException
  124. */
  125. public function testThrowExceptionWhenUnableToConnect()
  126. {
  127. $parameters = $this->getParameters(array('host' => '169.254.10.10'));
  128. $connection = new WebdisConnection($parameters);
  129. $connection->executeCommand($this->getProfile()->createCommand('ping'));
  130. }
  131. // ******************************************************************** //
  132. // ---- HELPER METHODS ------------------------------------------------ //
  133. // ******************************************************************** //
  134. /**
  135. * Returns a named array with the default connection parameters and their values.
  136. *
  137. * @return Array Default connection parameters.
  138. */
  139. protected function getDefaultParametersArray()
  140. {
  141. return array(
  142. 'scheme' => 'http',
  143. 'host' => WEBDIS_SERVER_HOST,
  144. 'port' => WEBDIS_SERVER_PORT,
  145. );
  146. }
  147. /**
  148. * Returns a new instance of connection parameters.
  149. *
  150. * @param array $additional Additional connection parameters.
  151. * @return ConnectionParameters Default connection parameters.
  152. */
  153. protected function getParameters($additional = array())
  154. {
  155. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  156. $parameters = new ConnectionParameters($parameters);
  157. return $parameters;
  158. }
  159. /**
  160. * Returns a new instance of server profile.
  161. *
  162. * @param array $additional Additional connection parameters.
  163. * @return ServerProfile
  164. */
  165. protected function getProfile($version = null)
  166. {
  167. return ServerProfile::get($version ?: REDIS_SERVER_VERSION);
  168. }
  169. /**
  170. * Returns a new instance of a connection instance.
  171. *
  172. * @param array $parameters Additional connection parameters.
  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. }