MultiBulkTupleTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Response\Iterator;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\Connection\ComposableStreamConnection;
  14. use Predis\Connection\ConnectionParameters;
  15. use Predis\Protocol\Text\ProtocolProcessor as TextProtocolProcessor;
  16. /**
  17. * @group realm-iterators
  18. */
  19. class MultiBulkTupleTest extends StandardTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. * @expectedException RuntimeException
  24. * @expectedExceptionMessage Cannot initialize a tuple iterator with an already initiated iterator
  25. */
  26. public function testInitiatedMultiBulkIteratorsAreNotValid()
  27. {
  28. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  29. $iterator = new MultiBulk($connection, 2);
  30. $iterator->next();
  31. new MultiBulkTuple($iterator);
  32. }
  33. /**
  34. * @group disconnected
  35. * @expectedException UnexpectedValueException
  36. * @expectedExceptionMessage Invalid response size for a tuple iterator [3]
  37. */
  38. public function testMultiBulkWithOddSizesAreInvalid()
  39. {
  40. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  41. $iterator = new MultiBulk($connection, 3);
  42. new MultiBulkTuple($iterator);
  43. }
  44. /**
  45. * @group connected
  46. */
  47. public function testIterableMultibulk()
  48. {
  49. $client = $this->getClient();
  50. $client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
  51. $this->assertInstanceOf('OuterIterator', $iterator = $client->zrange('metavars', 0, -1, 'withscores')->asTuple());
  52. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulkTuple', $iterator);
  53. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulk', $iterator->getInnerIterator());
  54. $this->assertTrue($iterator->valid());
  55. $this->assertSame(3, $iterator->count());
  56. $this->assertSame(array('foo', '1'), $iterator->current());
  57. $this->assertSame(1, $iterator->next());
  58. $this->assertTrue($iterator->valid());
  59. $this->assertSame(array('hoge', '2'), $iterator->current());
  60. $this->assertSame(2, $iterator->next());
  61. $this->assertTrue($iterator->valid());
  62. $this->assertSame(array('lol', '3'), $iterator->current());
  63. $this->assertSame(3, $iterator->next());
  64. $this->assertFalse($iterator->valid());
  65. $this->assertTrue($client->ping());
  66. }
  67. /**
  68. * @group connected
  69. */
  70. public function testGarbageCollectorDropsUnderlyingConnection()
  71. {
  72. $client = $this->getClient();
  73. $client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
  74. $iterator = $client->zrange('metavars', 0, -1, 'withscores')->asTuple();
  75. unset($iterator);
  76. $this->assertFalse($client->isConnected());
  77. $this->assertTrue($client->ping());
  78. }
  79. // ******************************************************************** //
  80. // ---- HELPER METHODS ------------------------------------------------ //
  81. // ******************************************************************** //
  82. /**
  83. * Returns a new client instance.
  84. *
  85. * @return Client
  86. */
  87. protected function getClient()
  88. {
  89. $parameters = new ConnectionParameters(array(
  90. 'host' => REDIS_SERVER_HOST,
  91. 'port' => REDIS_SERVER_PORT,
  92. 'read_write_timeout' => 2,
  93. ));
  94. $options = array(
  95. 'profile' => REDIS_SERVER_VERSION,
  96. );
  97. $protocol = new TextProtocolProcessor();
  98. $protocol->useIterableMultibulk(true);
  99. $connection = new ComposableStreamConnection($parameters, $protocol);
  100. $client = new Client($connection, $options);
  101. $client->connect();
  102. $client->select(REDIS_SERVER_DBNUM);
  103. $client->flushdb();
  104. return $client;
  105. }
  106. }