StreamConnectionTest.php 4.2 KB

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