WebdisConnectionTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * @expectedException Predis\NotSupportedException
  32. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::writeRequest() is not supported.
  33. */
  34. public function testWritingCommandsIsNotSupported()
  35. {
  36. $connection = new WebdisConnection($this->getParameters());
  37. $connection->writeRequest($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::addConnectCommand() is not supported.
  63. *
  64. */
  65. public function testAddingConnectCommandsIsNotSupported()
  66. {
  67. $connection = new WebdisConnection($this->getParameters());
  68. $connection->addConnectCommand($this->getProfile()->createCommand('ping'));
  69. }
  70. /**
  71. * @group disconnected
  72. * @expectedException Predis\NotSupportedException
  73. * @expectedExceptionMessage Command 'SELECT' is not allowed by Webdis.
  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 Command 'AUTH' is not allowed by Webdis.
  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\Connection\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->assertEquals('PONG', $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. * @medium
  123. * @group disconnected
  124. * @group slow
  125. * @expectedException Predis\Connection\ConnectionException
  126. */
  127. public function testThrowExceptionWhenUnableToConnect()
  128. {
  129. $parameters = $this->getParameters(array('host' => '169.254.10.10'));
  130. $connection = new WebdisConnection($parameters);
  131. $connection->executeCommand($this->getProfile()->createCommand('ping'));
  132. }
  133. // ******************************************************************** //
  134. // ---- HELPER METHODS ------------------------------------------------ //
  135. // ******************************************************************** //
  136. /**
  137. * Returns a named array with the default connection parameters and their values.
  138. *
  139. * @return array Default connection parameters.
  140. */
  141. protected function getDefaultParametersArray()
  142. {
  143. return array(
  144. 'scheme' => 'http',
  145. 'host' => WEBDIS_SERVER_HOST,
  146. 'port' => WEBDIS_SERVER_PORT,
  147. );
  148. }
  149. /**
  150. * Returns a new instance of a connection instance.
  151. *
  152. * @param mixed $profile Redis profile.
  153. * @param array $parameters Additional connection parameters.
  154. * @return WebdisConnection
  155. */
  156. protected function getConnection(&$profile = null, array $parameters = array())
  157. {
  158. $parameters = $this->getParameters($parameters);
  159. $profile = $this->getProfile();
  160. $connection = new WebdisConnection($parameters);
  161. $connection->executeCommand($profile->createCommand('flushdb'));
  162. return $connection;
  163. }
  164. }