StreamConnectionTest.php 3.7 KB

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