ComposableStreamConnectionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ComposableStreamConnectionTest extends PredisConnectionTestCase
  15. {
  16. /**
  17. * @group disconnected
  18. */
  19. public function testConstructorDoesNotOpenConnection()
  20. {
  21. $connection = new ComposableStreamConnection($this->getParameters());
  22. $this->assertFalse($connection->isConnected());
  23. }
  24. /**
  25. * @group disconnected
  26. */
  27. public function testExposesParameters()
  28. {
  29. $parameters = $this->getParameters();
  30. $connection = new ComposableStreamConnection($parameters);
  31. $this->assertSame($parameters, $connection->getParameters());
  32. }
  33. /**
  34. * @group disconnected
  35. * @expectedException InvalidArgumentException
  36. * @expectedExceptionMessage Invalid scheme: udp
  37. */
  38. public function testThrowsExceptionOnInvalidScheme()
  39. {
  40. $parameters = $this->getParameters(array('scheme' => 'udp'));
  41. $connection = new ComposableStreamConnection($parameters);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testCanBeSerialized()
  47. {
  48. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  49. $connection = new ComposableStreamConnection($parameters);
  50. $unserialized = unserialize(serialize($connection));
  51. $this->assertEquals($connection, $unserialized);
  52. }
  53. // ******************************************************************** //
  54. // ---- INTEGRATION TESTS --------------------------------------------- //
  55. // ******************************************************************** //
  56. /**
  57. * @group connected
  58. */
  59. public function testReadsMultibulkRepliesAsIterators()
  60. {
  61. $connection = $this->getConnection($profile, true);
  62. $connection->getProtocol()->setOption('iterable_multibulk', true);
  63. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  64. $connection->writeCommand($profile->createCommand('lrange', array('metavars', 0, -1)));
  65. $this->assertInstanceOf('Predis\Iterator\MultiBulkResponse', $iterator = $connection->read());
  66. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  67. }
  68. /**
  69. * @group connected
  70. * @expectedException Predis\Protocol\ProtocolException
  71. * @expectedExceptionMessage Unknown prefix: 'P'
  72. */
  73. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  74. {
  75. $connection = $this->getConnection($profile);
  76. $stream = $connection->getResource();
  77. $connection->writeCommand($profile->createCommand('ping'));
  78. fread($stream, 1);
  79. $connection->read();
  80. }
  81. // ******************************************************************** //
  82. // ---- HELPER METHODS ------------------------------------------------ //
  83. // ******************************************************************** //
  84. /**
  85. * {@inheritdoc}
  86. */
  87. protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array())
  88. {
  89. $parameters = $this->getParameters($parameters);
  90. $profile = $this->getProfile();
  91. $connection = new ComposableStreamConnection($parameters);
  92. if ($initialize) {
  93. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  94. $connection->pushInitCommand($profile->createCommand('flushdb'));
  95. }
  96. return $connection;
  97. }
  98. }