CompositeStreamConnectionTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Predis\Command\RawCommand;
  12. use Predis\Response\Error as ErrorResponse;
  13. /**
  14. *
  15. */
  16. class CompositeStreamConnectionTest extends PredisConnectionTestCase
  17. {
  18. const CONNECTION_CLASS = 'Predis\Connection\CompositeStreamConnection';
  19. /**
  20. * @group disconnected
  21. * @expectedException \Predis\Connection\ConnectionException
  22. * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
  23. */
  24. public function testThrowsExceptionOnInitializationCommandFailure()
  25. {
  26. $cmdSelect = RawCommand::create('SELECT', '1000');
  27. $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
  28. ->setMethods(array('executeCommand', 'createResource'))
  29. ->setConstructorArgs(array(new Parameters()))
  30. ->getMock();
  31. $connection->method('executeCommand')
  32. ->with($cmdSelect)
  33. ->will($this->returnValue(
  34. new ErrorResponse('ERR invalid DB index')
  35. ));
  36. $connection->method('createResource');
  37. $connection->addConnectCommand($cmdSelect);
  38. $connection->connect();
  39. }
  40. // ******************************************************************** //
  41. // ---- INTEGRATION TESTS --------------------------------------------- //
  42. // ******************************************************************** //
  43. /**
  44. * @group connected
  45. */
  46. public function testReadsMultibulkResponsesAsIterators()
  47. {
  48. $connection = $this->createConnection(true);
  49. $profile = $this->getCurrentProfile();
  50. $connection->getProtocol()->useIterableMultibulk(true);
  51. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  52. $connection->writeRequest($profile->createCommand('lrange', array('metavars', 0, -1)));
  53. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulkIterator', $iterator = $connection->read());
  54. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  55. }
  56. /**
  57. * @group connected
  58. * @requires PHP 5.4
  59. */
  60. public function testPersistentParameterWithFalseLikeValues()
  61. {
  62. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  63. $this->assertNonPersistentConnection($connection1);
  64. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  65. $this->assertNonPersistentConnection($connection2);
  66. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  67. $this->assertNonPersistentConnection($connection3);
  68. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  69. $this->assertNonPersistentConnection($connection4);
  70. }
  71. /**
  72. * @group connected
  73. * @requires PHP 5.4
  74. */
  75. public function testPersistentParameterWithTrueLikeValues()
  76. {
  77. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  78. $this->assertPersistentConnection($connection1);
  79. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  80. $this->assertPersistentConnection($connection2);
  81. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  82. $this->assertPersistentConnection($connection3);
  83. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  84. $this->assertPersistentConnection($connection4);
  85. $connection1->disconnect();
  86. }
  87. /**
  88. * @group connected
  89. * @requires PHP 5.4
  90. */
  91. public function testPersistentConnectionsToSameNodeShareResource()
  92. {
  93. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  94. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  95. $this->assertPersistentConnection($connection1);
  96. $this->assertPersistentConnection($connection2);
  97. $this->assertSame($connection1->getResource(), $connection2->getResource());
  98. $connection1->disconnect();
  99. }
  100. /**
  101. * @group connected
  102. * @requires PHP 5.4
  103. */
  104. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  105. {
  106. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  107. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  108. $this->assertPersistentConnection($connection1);
  109. $this->assertPersistentConnection($connection2);
  110. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  111. }
  112. }