MultiBulkTupleTest.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\Response\Iterator;
  11. use Predis\Client;
  12. use Predis\Connection\CompositeStreamConnection;
  13. use Predis\Protocol\Text\ProtocolProcessor as TextProtocolProcessor;
  14. use PredisTestCase;
  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. $iterator = new MultiBulkTuple($client->zrange('metavars', '0', '-1', 'withscores'));
  51. $this->assertInstanceOf('OuterIterator', $iterator);
  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. $iterator->next();
  58. $this->assertTrue($iterator->valid());
  59. $this->assertSame(array('hoge', '2'), $iterator->current());
  60. $iterator->next();
  61. $this->assertTrue($iterator->valid());
  62. $this->assertSame(array('lol', '3'), $iterator->current());
  63. $iterator->next();
  64. $this->assertFalse($iterator->valid());
  65. $this->assertEquals('PONG', $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 = new MultiBulkTuple($client->zrange('metavars', '0', '-1', 'withscores'));
  75. unset($iterator);
  76. $this->assertFalse($client->isConnected());
  77. $this->assertEquals('PONG', $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 = $this->getParameters(array('read_write_timeout' => 2));
  90. $protocol = new TextProtocolProcessor();
  91. $protocol->useIterableMultibulk(true);
  92. $connection = new CompositeStreamConnection($parameters, $protocol);
  93. $client = new Client($connection);
  94. $client->connect();
  95. $client->flushdb();
  96. return $client;
  97. }
  98. }