MultiBulkTupleTest.php 3.7 KB

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