WebdisConnectionTest.php 6.2 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. /**
  13. * @group ext-curl
  14. * @group ext-phpiredis
  15. * @group realm-connection
  16. * @group realm-webdis
  17. */
  18. class WebdisConnectionTest extends PredisTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testIsConnectedAlwaysReturnsTrue()
  24. {
  25. $connection = new WebdisConnection($this->getParameters());
  26. $this->assertTrue($connection->isConnected());
  27. }
  28. /**
  29. * @group disconnected
  30. * @expectedException Predis\NotSupportedException
  31. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::writeCommand() is not supported
  32. */
  33. public function testWritingCommandsIsNotSupported()
  34. {
  35. $connection = new WebdisConnection($this->getParameters());
  36. $connection->writeCommand($this->getProfile()->createCommand('ping'));
  37. }
  38. /**
  39. * @group disconnected
  40. * @expectedException Predis\NotSupportedException
  41. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::readResponse() is not supported
  42. */
  43. public function testReadingResponsesIsNotSupported()
  44. {
  45. $connection = new WebdisConnection($this->getParameters());
  46. $connection->readResponse($this->getProfile()->createCommand('ping'));
  47. }
  48. /**
  49. * @group disconnected
  50. * @expectedException Predis\NotSupportedException
  51. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::read() is not supported
  52. */
  53. public function testReadingFromConnectionIsNotSupported()
  54. {
  55. $connection = new WebdisConnection($this->getParameters());
  56. $connection->read();
  57. }
  58. /**
  59. * @group disconnected
  60. * @expectedException Predis\NotSupportedException
  61. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::pushInitCommand() is not supported
  62. */
  63. public function testPushingInitCommandsIsNotSupported()
  64. {
  65. $connection = new WebdisConnection($this->getParameters());
  66. $connection->pushInitCommand($this->getProfile()->createCommand('ping'));
  67. }
  68. /**
  69. * @group disconnected
  70. * @expectedException Predis\NotSupportedException
  71. * @expectedExceptionMessage Disabled command: SELECT
  72. */
  73. public function testRejectCommandSelect()
  74. {
  75. $connection = new WebdisConnection($this->getParameters());
  76. $connection->executeCommand($this->getProfile()->createCommand('select', array(0)));
  77. }
  78. /**
  79. * @group disconnected
  80. * @expectedException Predis\NotSupportedException
  81. * @expectedExceptionMessage Disabled command: AUTH
  82. */
  83. public function testRejectCommandAuth()
  84. {
  85. $connection = new WebdisConnection($this->getParameters());
  86. $connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar')));
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testCanBeSerialized()
  92. {
  93. $parameters = $this->getParameters(array('alias' => 'webdis'));
  94. $connection = new WebdisConnection($parameters);
  95. $unserialized = unserialize(serialize($connection));
  96. $this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
  97. $this->assertEquals($parameters, $unserialized->getParameters());
  98. }
  99. // ******************************************************************** //
  100. // ---- INTEGRATION TESTS --------------------------------------------- //
  101. // ******************************************************************** //
  102. /**
  103. * @group connected
  104. */
  105. public function testExecutesCommandsOnServer()
  106. {
  107. $connection = $this->getConnection($profile);
  108. $cmdPing = $profile->createCommand('ping');
  109. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  110. $cmdGet = $profile->createCommand('get', array('foobar'));
  111. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  112. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  113. $this->assertSame('PONG', $connection->executeCommand($cmdPing));
  114. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  115. $this->assertNull($connection->executeCommand($cmdGet));
  116. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  117. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  118. }
  119. /**
  120. * @medium
  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 a connection instance.
  149. *
  150. * @param mixed $profile Redis profile.
  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. }