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. $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. * @requires PHP 5.4
  48. */
  49. public function testPersistentParameterWithFalseLikeValues()
  50. {
  51. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  52. $this->assertNonPersistentConnection($connection1);
  53. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  54. $this->assertNonPersistentConnection($connection2);
  55. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  56. $this->assertNonPersistentConnection($connection3);
  57. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  58. $this->assertNonPersistentConnection($connection4);
  59. }
  60. /**
  61. * @group connected
  62. * @requires PHP 5.4
  63. */
  64. public function testPersistentParameterWithTrueLikeValues()
  65. {
  66. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  67. $this->assertPersistentConnection($connection1);
  68. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  69. $this->assertPersistentConnection($connection2);
  70. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  71. $this->assertPersistentConnection($connection3);
  72. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  73. $this->assertPersistentConnection($connection4);
  74. $connection1->disconnect();
  75. }
  76. /**
  77. * @group connected
  78. * @requires PHP 5.4
  79. */
  80. public function testPersistentConnectionsToSameNodeShareResource()
  81. {
  82. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  83. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  84. $this->assertPersistentConnection($connection1);
  85. $this->assertPersistentConnection($connection2);
  86. $this->assertSame($connection1->getResource(), $connection2->getResource());
  87. $connection1->disconnect();
  88. }
  89. /**
  90. * @group connected
  91. * @requires PHP 5.4
  92. */
  93. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  94. {
  95. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  96. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  97. $this->assertPersistentConnection($connection1);
  98. $this->assertPersistentConnection($connection2);
  99. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  100. }
  101. }