WebdisConnectionTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // ---- INTEGRATION TESTS --------------------------------------------- //
  92. // ******************************************************************** //
  93. /**
  94. * @group connected
  95. */
  96. public function testExecutesCommandsOnServer()
  97. {
  98. $connection = $this->getConnection($profile);
  99. $cmdPing = $profile->createCommand('ping');
  100. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  101. $cmdGet = $profile->createCommand('get', array('foobar'));
  102. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  103. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  104. $this->assertTrue($connection->executeCommand($cmdPing));
  105. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  106. $this->assertNull($connection->executeCommand($cmdGet));
  107. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  108. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  109. }
  110. /**
  111. * @group disconnected
  112. * @group slow
  113. * @expectedException Predis\Network\ConnectionException
  114. */
  115. public function testThrowExceptionWhenUnableToConnect()
  116. {
  117. $parameters = $this->getParameters(array('host' => '169.254.10.10'));
  118. $connection = new WebdisConnection($parameters);
  119. $connection->executeCommand($this->getProfile()->createCommand('ping'));
  120. }
  121. // ******************************************************************** //
  122. // ---- HELPER METHODS ------------------------------------------------ //
  123. // ******************************************************************** //
  124. /**
  125. * Returns a named array with the default connection parameters and their values.
  126. *
  127. * @return Array Default connection parameters.
  128. */
  129. protected function getDefaultParametersArray()
  130. {
  131. return array(
  132. 'scheme' => 'http',
  133. 'host' => WEBDIS_SERVER_HOST,
  134. 'port' => WEBDIS_SERVER_PORT,
  135. );
  136. }
  137. /**
  138. * Returns a new instance of connection parameters.
  139. *
  140. * @param array $additional Additional connection parameters.
  141. * @return ConnectionParameters Default connection parameters.
  142. */
  143. protected function getParameters($additional = array())
  144. {
  145. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  146. $parameters = new ConnectionParameters($parameters);
  147. return $parameters;
  148. }
  149. /**
  150. * Returns a new instance of server profile.
  151. *
  152. * @param array $additional Additional connection parameters.
  153. * @return ServerProfile
  154. */
  155. protected function getProfile($version = null)
  156. {
  157. return ServerProfile::get($version ?: REDIS_SERVER_VERSION);
  158. }
  159. /**
  160. * Returns a new instance of a connection instance.
  161. *
  162. * @param array $parameters Additional connection parameters.
  163. * @return WebdisConnection
  164. */
  165. protected function getConnection(&$profile = null, Array $parameters = array())
  166. {
  167. $parameters = $this->getParameters($parameters);
  168. $profile = $this->getProfile();
  169. $connection = new WebdisConnection($parameters);
  170. $connection->executeCommand($profile->createCommand('flushdb'));
  171. return $connection;
  172. }
  173. }