StreamConnectionTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // ---- INTEGRATION TESTS --------------------------------------------- //
  48. // ******************************************************************** //
  49. /**
  50. * @group connected
  51. */
  52. public function testReadsMultibulkRepliesAsIterators()
  53. {
  54. $connection = $this->getConnection($profile, true, array('iterable_multibulk' => true));
  55. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  56. $connection->writeCommand($profile->createCommand('lrange', array('metavars', 0, -1)));
  57. $this->assertInstanceOf('Predis\Iterators\MultiBulkResponse', $iterator = $connection->read());
  58. $this->assertSame(array('foo', 'hoge', 'lol'), iterator_to_array($iterator));
  59. }
  60. /**
  61. * @group connected
  62. * @expectedException Predis\Protocol\ProtocolException
  63. * @expectedExceptionMessage Unknown prefix: 'P'
  64. */
  65. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  66. {
  67. $connection = $this->getConnection($profile);
  68. $stream = $connection->getResource();
  69. $connection->writeCommand($profile->createCommand('ping'));
  70. fread($stream, 1);
  71. $connection->read();
  72. }
  73. // ******************************************************************** //
  74. // ---- HELPER METHODS ------------------------------------------------ //
  75. // ******************************************************************** //
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array())
  80. {
  81. $parameters = $this->getParameters($parameters);
  82. $profile = $this->getProfile();
  83. $connection = new StreamConnection($parameters);
  84. if ($initialize) {
  85. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  86. $connection->pushInitCommand($profile->createCommand('flushdb'));
  87. }
  88. return $connection;
  89. }
  90. }