WebdisConnectionTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * @requires extension phpiredis
  18. * @requires extension curl
  19. */
  20. class WebdisConnectionTest extends PredisTestCase
  21. {
  22. const CONNECTION_CLASS = 'Predis\Connection\WebdisConnection';
  23. /**
  24. * @group disconnected
  25. */
  26. public function testIsConnectedAlwaysReturnsTrue()
  27. {
  28. $connection = new WebdisConnection($this->getParameters());
  29. $this->assertTrue($connection->isConnected());
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testSupportsSchemeUnix()
  35. {
  36. $parameters = $this->getParameters(array('scheme' => 'http'));
  37. $connection = new WebdisConnection($parameters);
  38. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  39. }
  40. /**
  41. * @group disconnected
  42. * @expectedException \InvalidArgumentException
  43. * @expectedExceptionMessage Invalid scheme: 'tcp'.
  44. */
  45. public function testThrowsExceptionOnInvalidScheme()
  46. {
  47. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  48. new WebdisConnection($parameters);
  49. }
  50. /**
  51. * @group disconnected
  52. * @expectedException \Predis\NotSupportedException
  53. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::writeRequest() is not supported.
  54. */
  55. public function testWritingCommandsIsNotSupported()
  56. {
  57. $connection = new WebdisConnection($this->getParameters());
  58. $connection->writeRequest($this->getProfile()->createCommand('ping'));
  59. }
  60. /**
  61. * @group disconnected
  62. * @expectedException \Predis\NotSupportedException
  63. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::readResponse() is not supported
  64. */
  65. public function testReadingResponsesIsNotSupported()
  66. {
  67. $connection = new WebdisConnection($this->getParameters());
  68. $connection->readResponse($this->getProfile()->createCommand('ping'));
  69. }
  70. /**
  71. * @group disconnected
  72. * @expectedException \Predis\NotSupportedException
  73. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::read() is not supported.
  74. */
  75. public function testReadingFromConnectionIsNotSupported()
  76. {
  77. $connection = new WebdisConnection($this->getParameters());
  78. $connection->read();
  79. }
  80. /**
  81. * @group disconnected
  82. * @expectedException \Predis\NotSupportedException
  83. * @expectedExceptionMessage The method Predis\Connection\WebdisConnection::addConnectCommand() is not supported.
  84. */
  85. public function testAddingConnectCommandsIsNotSupported()
  86. {
  87. $connection = new WebdisConnection($this->getParameters());
  88. $connection->addConnectCommand($this->getProfile()->createCommand('ping'));
  89. }
  90. /**
  91. * @group disconnected
  92. * @expectedException \Predis\NotSupportedException
  93. * @expectedExceptionMessage Command 'SELECT' is not allowed by Webdis.
  94. */
  95. public function testRejectCommandSelect()
  96. {
  97. $connection = new WebdisConnection($this->getParameters());
  98. $connection->executeCommand($this->getProfile()->createCommand('select', array(0)));
  99. }
  100. /**
  101. * @group disconnected
  102. * @expectedException \Predis\NotSupportedException
  103. * @expectedExceptionMessage Command 'AUTH' is not allowed by Webdis.
  104. */
  105. public function testRejectCommandAuth()
  106. {
  107. $connection = new WebdisConnection($this->getParameters());
  108. $connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar')));
  109. }
  110. /**
  111. * @group disconnected
  112. */
  113. public function testCanBeSerialized()
  114. {
  115. $parameters = $this->getParameters(array('alias' => 'webdis'));
  116. $connection = new WebdisConnection($parameters);
  117. $unserialized = unserialize(serialize($connection));
  118. $this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
  119. $this->assertEquals($parameters, $unserialized->getParameters());
  120. }
  121. // ******************************************************************** //
  122. // ---- INTEGRATION TESTS --------------------------------------------- //
  123. // ******************************************************************** //
  124. /**
  125. * @medium
  126. * @group disconnected
  127. * @group slow
  128. * @expectedException \Predis\Connection\ConnectionException
  129. */
  130. public function testThrowExceptionWhenUnableToConnect()
  131. {
  132. $parameters = $this->getParameters(array('host' => '169.254.10.10'));
  133. $connection = new WebdisConnection($parameters);
  134. $connection->executeCommand($this->getProfile()->createCommand('ping'));
  135. }
  136. // ******************************************************************** //
  137. // ---- HELPER METHODS ------------------------------------------------ //
  138. // ******************************************************************** //
  139. /**
  140. * Returns a named array with the default connection parameters and their values.
  141. *
  142. * @return array Default connection parameters.
  143. */
  144. protected function getDefaultParametersArray()
  145. {
  146. return array(
  147. 'scheme' => 'http',
  148. 'host' => WEBDIS_SERVER_HOST,
  149. 'port' => WEBDIS_SERVER_PORT,
  150. );
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. protected function getConnection(&$profile = null, array $parameters = array())
  156. {
  157. $class = static::CONNECTION_CLASS;
  158. $parameters = $this->getParameters($parameters);
  159. $profile = $this->getProfile();
  160. $connection = new $class($parameters);
  161. $connection->executeCommand($profile->createCommand('flushdb'));
  162. return $connection;
  163. }
  164. }