CompositeStreamConnectionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  12. *
  13. */
  14. class CompositeStreamConnectionTest extends PredisConnectionTestCase
  15. {
  16. const CONNECTION_CLASS = 'Predis\Connection\CompositeStreamConnection';
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorDoesNotOpenConnection()
  21. {
  22. $connection = new CompositeStreamConnection($this->getParameters());
  23. $this->assertFalse($connection->isConnected());
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testSupportsSchemeTCP()
  29. {
  30. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  31. $connection = new StreamConnection($parameters);
  32. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testSupportsSchemeRedis()
  38. {
  39. $parameters = $this->getParameters(array('scheme' => 'redis'));
  40. $connection = new StreamConnection($parameters);
  41. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testSupportsSchemeUnix()
  47. {
  48. $parameters = $this->getParameters(array('scheme' => 'unix'));
  49. $connection = new StreamConnection($parameters);
  50. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  51. }
  52. /**
  53. * @group disconnected
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage Invalid scheme: 'udp'.
  56. */
  57. public function testThrowsExceptionOnInvalidScheme()
  58. {
  59. $parameters = $this->getParameters(array('scheme' => 'udp'));
  60. new CompositeStreamConnection($parameters);
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testExposesParameters()
  66. {
  67. $parameters = $this->getParameters();
  68. $connection = new CompositeStreamConnection($parameters);
  69. $this->assertSame($parameters, $connection->getParameters());
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testCanBeSerialized()
  75. {
  76. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  77. $connection = new CompositeStreamConnection($parameters);
  78. $unserialized = unserialize(serialize($connection));
  79. $this->assertEquals($connection, $unserialized);
  80. }
  81. // ******************************************************************** //
  82. // ---- INTEGRATION TESTS --------------------------------------------- //
  83. // ******************************************************************** //
  84. /**
  85. * @group connected
  86. */
  87. public function testReadsMultibulkResponsesAsIterators()
  88. {
  89. $connection = $this->getConnection($profile, true);
  90. $connection->getProtocol()->useIterableMultibulk(true);
  91. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  92. $connection->writeRequest($profile->createCommand('lrange', array('metavars', 0, -1)));
  93. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulkIterator', $iterator = $connection->read());
  94. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  95. }
  96. /**
  97. * @group connected
  98. * @expectedException \Predis\Protocol\ProtocolException
  99. * @expectedExceptionMessage Unknown response prefix: 'P'.
  100. */
  101. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  102. {
  103. $connection = $this->getConnection($profile);
  104. $stream = $connection->getResource();
  105. $connection->writeRequest($profile->createCommand('ping'));
  106. fread($stream, 1);
  107. $connection->read();
  108. }
  109. }