PhpiredisSocketConnectionTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  12. * @group ext-phpiredis
  13. */
  14. class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
  15. {
  16. /**
  17. * @group disconnected
  18. */
  19. public function testConstructorDoesNotOpenConnection()
  20. {
  21. $connection = new PhpiredisSocketConnection($this->getParameters());
  22. $this->assertFalse($connection->isConnected());
  23. }
  24. /**
  25. * @group disconnected
  26. */
  27. public function testSupportsSchemeTCP()
  28. {
  29. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  30. $connection = new PhpiredisSocketConnection($parameters);
  31. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testSupportsSchemeRedis()
  37. {
  38. $parameters = $this->getParameters(array('scheme' => 'redis'));
  39. $connection = new PhpiredisSocketConnection($parameters);
  40. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testSupportsSchemeUnix()
  46. {
  47. $parameters = $this->getParameters(array('scheme' => 'unix'));
  48. $connection = new PhpiredisSocketConnection($parameters);
  49. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  50. }
  51. /**
  52. * @group disconnected
  53. * @expectedException \InvalidArgumentException
  54. * @expectedExceptionMessage Invalid scheme: 'udp'.
  55. */
  56. public function testThrowsExceptionOnInvalidScheme()
  57. {
  58. $parameters = $this->getParameters(array('scheme' => 'udp'));
  59. new PhpiredisSocketConnection($parameters);
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testExposesParameters()
  65. {
  66. $parameters = $this->getParameters();
  67. $connection = new PhpiredisSocketConnection($parameters);
  68. $this->assertSame($parameters, $connection->getParameters());
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testCanBeSerialized()
  74. {
  75. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  76. $connection = new PhpiredisSocketConnection($parameters);
  77. $unserialized = unserialize(serialize($connection));
  78. $this->assertInstanceOf('Predis\Connection\PhpiredisSocketConnection', $unserialized);
  79. $this->assertEquals($parameters, $unserialized->getParameters());
  80. }
  81. // ******************************************************************** //
  82. // ---- INTEGRATION TESTS --------------------------------------------- //
  83. // ******************************************************************** //
  84. /**
  85. * @group connected
  86. */
  87. public function testExecutesCommandsOnServer()
  88. {
  89. $connection = $this->getConnection($profile, true);
  90. $cmdPing = $profile->createCommand('ping');
  91. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  92. $cmdGet = $profile->createCommand('get', array('foobar'));
  93. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  94. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  95. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  96. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  97. $this->assertNull($connection->executeCommand($cmdGet));
  98. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  99. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  100. }
  101. /**
  102. * @group connected
  103. * @expectedException \Predis\Connection\ConnectionException
  104. * @expectedExceptionMessage Cannot resolve the address of 'bogus.tld'.
  105. */
  106. public function testThrowsExceptionOnUnresolvableHostname()
  107. {
  108. $parameters = $this->getParameters(array('host' => 'bogus.tld'));
  109. $connection = new PhpiredisSocketConnection($parameters);
  110. $connection->connect();
  111. }
  112. /**
  113. * @group connected
  114. * @expectedException \Predis\Protocol\ProtocolException
  115. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  116. */
  117. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  118. {
  119. $connection = $this->getConnection($profile);
  120. $socket = $connection->getResource();
  121. $connection->writeRequest($profile->createCommand('ping'));
  122. socket_read($socket, 1);
  123. $connection->read();
  124. }
  125. // ******************************************************************** //
  126. // ---- HELPER METHODS ------------------------------------------------ //
  127. // ******************************************************************** //
  128. /**
  129. * {@inheritdoc}
  130. */
  131. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  132. {
  133. $parameters = $this->getParameters($parameters);
  134. $profile = $this->getProfile();
  135. $connection = new PhpiredisSocketConnection($parameters);
  136. if ($initialize) {
  137. $connection->addConnectCommand(
  138. $profile->createCommand('select', array($parameters->database))
  139. );
  140. $connection->addConnectCommand(
  141. $profile->createCommand('flushdb')
  142. );
  143. }
  144. return $connection;
  145. }
  146. }