StreamConnectionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 StreamConnectionTest extends PredisConnectionTestCase
  17. {
  18. const CONNECTION_CLASS = 'Predis\Connection\StreamConnection';
  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. $responseError =
  28. $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
  29. ->setMethods(array('executeCommand', 'createResource'))
  30. ->setConstructorArgs(array(new Parameters()))
  31. ->getMock();
  32. $connection->method('executeCommand')
  33. ->with($cmdSelect)
  34. ->will($this->returnValue(
  35. new ErrorResponse("ERR invalid DB index")
  36. ));
  37. $connection->method('createResource');
  38. $connection->addConnectCommand($cmdSelect);
  39. $connection->connect();
  40. }
  41. // ******************************************************************** //
  42. // ---- INTEGRATION TESTS --------------------------------------------- //
  43. // ******************************************************************** //
  44. /**
  45. * @group connected
  46. * @requires PHP 5.4
  47. */
  48. public function testPersistentParameterWithFalseLikeValues()
  49. {
  50. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  51. $this->assertNonPersistentConnection($connection1);
  52. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  53. $this->assertNonPersistentConnection($connection2);
  54. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  55. $this->assertNonPersistentConnection($connection3);
  56. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  57. $this->assertNonPersistentConnection($connection4);
  58. }
  59. /**
  60. * @group connected
  61. * @requires PHP 5.4
  62. */
  63. public function testPersistentParameterWithTrueLikeValues()
  64. {
  65. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  66. $this->assertPersistentConnection($connection1);
  67. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  68. $this->assertPersistentConnection($connection2);
  69. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  70. $this->assertPersistentConnection($connection3);
  71. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  72. $this->assertPersistentConnection($connection4);
  73. $connection1->disconnect();
  74. }
  75. /**
  76. * @group connected
  77. * @requires PHP 5.4
  78. */
  79. public function testPersistentConnectionsToSameNodeShareResource()
  80. {
  81. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  82. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  83. $this->assertPersistentConnection($connection1);
  84. $this->assertPersistentConnection($connection2);
  85. $this->assertSame($connection1->getResource(), $connection2->getResource());
  86. $connection1->disconnect();
  87. }
  88. /**
  89. * @group connected
  90. * @requires PHP 5.4
  91. */
  92. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  93. {
  94. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  95. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  96. $this->assertPersistentConnection($connection1);
  97. $this->assertPersistentConnection($connection2);
  98. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  99. }
  100. }