WebdisConnectionTest.php 6.9 KB

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