ComposableStreamConnectionTest.php 3.7 KB

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