PhpiredisStreamConnectionTest.php 5.7 KB

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