CompositeStreamConnectionTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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
  28. ->getMockBuilder(static::CONNECTION_CLASS)
  29. ->setMethods(array('executeCommand', 'createResource'))
  30. ->setConstructorArgs(array(new Parameters()))
  31. ->getMock();
  32. $connection
  33. ->method('executeCommand')
  34. ->with($cmdSelect)
  35. ->will($this->returnValue(
  36. new ErrorResponse('ERR invalid DB index')
  37. ));
  38. $connection->method('createResource');
  39. $connection->addConnectCommand($cmdSelect);
  40. $connection->connect();
  41. }
  42. // ******************************************************************** //
  43. // ---- INTEGRATION TESTS --------------------------------------------- //
  44. // ******************************************************************** //
  45. /**
  46. * @group connected
  47. */
  48. public function testReadsMultibulkResponsesAsIterators()
  49. {
  50. $connection = $this->createConnection(true);
  51. $commands = $this->getCommandFactory();
  52. $connection->getProtocol()->useIterableMultibulk(true);
  53. $connection->executeCommand($commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  54. $connection->writeRequest($commands->createCommand('lrange', array('metavars', 0, -1)));
  55. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulkIterator', $iterator = $connection->read());
  56. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  57. }
  58. /**
  59. * @group connected
  60. * @requires PHP 5.4
  61. */
  62. public function testPersistentParameterWithFalseLikeValues()
  63. {
  64. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  65. $this->assertNonPersistentConnection($connection1);
  66. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  67. $this->assertNonPersistentConnection($connection2);
  68. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  69. $this->assertNonPersistentConnection($connection3);
  70. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  71. $this->assertNonPersistentConnection($connection4);
  72. }
  73. /**
  74. * @group connected
  75. * @requires PHP 5.4
  76. */
  77. public function testPersistentParameterWithTrueLikeValues()
  78. {
  79. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  80. $this->assertPersistentConnection($connection1);
  81. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  82. $this->assertPersistentConnection($connection2);
  83. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  84. $this->assertPersistentConnection($connection3);
  85. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  86. $this->assertPersistentConnection($connection4);
  87. $connection1->disconnect();
  88. }
  89. /**
  90. * @group connected
  91. * @requires PHP 5.4
  92. */
  93. public function testPersistentConnectionsToSameNodeShareResource()
  94. {
  95. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  96. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  97. $this->assertPersistentConnection($connection1);
  98. $this->assertPersistentConnection($connection2);
  99. $this->assertSame($connection1->getResource(), $connection2->getResource());
  100. $connection1->disconnect();
  101. }
  102. /**
  103. * @group connected
  104. * @requires PHP 5.4
  105. */
  106. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  107. {
  108. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  109. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  110. $this->assertPersistentConnection($connection1);
  111. $this->assertPersistentConnection($connection2);
  112. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  113. }
  114. }