StreamConnectionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. use Predis\Profile\ServerProfile;
  13. /**
  14. *
  15. */
  16. class StreamConnectionTest extends ConnectionTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testConstructorDoesNotOpenConnection()
  22. {
  23. $connection = new StreamConnection($this->getParameters());
  24. $this->assertFalse($connection->isConnected());
  25. }
  26. /**
  27. * @group disconnected
  28. */
  29. public function testExposesParameters()
  30. {
  31. $parameters = $this->getParameters();
  32. $connection = new StreamConnection($parameters);
  33. $this->assertSame($parameters, $connection->getParameters());
  34. }
  35. /**
  36. * @group disconnected
  37. * @expectedException InvalidArgumentException
  38. * @expectedExceptionMessage Invalid scheme: udp
  39. */
  40. public function testThrowsExceptionOnInvalidScheme()
  41. {
  42. $parameters = $this->getParameters(array('scheme' => 'udp'));
  43. $connection = new StreamConnection($parameters);
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testCanBeSerialized()
  49. {
  50. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  51. $connection = new StreamConnection($parameters);
  52. $unserialized = unserialize(serialize($connection));
  53. $this->assertEquals($connection, $unserialized);
  54. }
  55. // ******************************************************************** //
  56. // ---- INTEGRATION TESTS --------------------------------------------- //
  57. // ******************************************************************** //
  58. /**
  59. * @group connected
  60. */
  61. public function testReadsMultibulkRepliesAsIterators()
  62. {
  63. $connection = $this->getConnection($profile, true, array('iterable_multibulk' => 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\Iterator\MultiBulkResponse', $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 StreamConnection($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. }