WebdisConnectionTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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::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->assertEquals('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. * @medium
  122. * @group disconnected
  123. * @group slow
  124. * @expectedException Predis\Connection\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 a connection instance.
  150. *
  151. * @param array $parameters Additional connection parameters.
  152. * @return WebdisConnection
  153. */
  154. protected function getConnection(&$profile = null, array $parameters = array())
  155. {
  156. $parameters = $this->getParameters($parameters);
  157. $profile = $this->getProfile();
  158. $connection = new WebdisConnection($parameters);
  159. $connection->executeCommand($profile->createCommand('flushdb'));
  160. return $connection;
  161. }
  162. }