MultiBulkTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 MultiBulkTest extends PredisTestCase
  19. {
  20. /**
  21. * @group connected
  22. */
  23. public function testIterableMultibulk()
  24. {
  25. $client = $this->getClient();
  26. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  27. $this->assertInstanceOf('Iterator', $iterator = $client->lrange('metavars', 0, -1));
  28. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulk', $iterator);
  29. $this->assertTrue($iterator->valid());
  30. $this->assertSame(3, $iterator->count());
  31. $this->assertSame('foo', $iterator->current());
  32. $this->assertSame(1, $iterator->next());
  33. $this->assertTrue($iterator->valid());
  34. $this->assertSame('hoge', $iterator->current());
  35. $this->assertSame(2, $iterator->next());
  36. $this->assertTrue($iterator->valid());
  37. $this->assertSame('lol', $iterator->current());
  38. $this->assertSame(3, $iterator->next());
  39. $this->assertFalse($iterator->valid());
  40. $this->assertEquals('PONG', $client->ping());
  41. }
  42. /**
  43. * @group connected
  44. */
  45. public function testIterableMultibulkCanBeWrappedAsTupleIterator()
  46. {
  47. $client = $this->getClient();
  48. $client->mset('foo', 'bar', 'hoge', 'piyo');
  49. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulk', $iterator = $client->mget('foo', 'bar'));
  50. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulkTuple', $iterator->asTuple());
  51. }
  52. /**
  53. * @group connected
  54. */
  55. public function testDropWithFalseConsumesResponseFromUnderlyingConnection()
  56. {
  57. $client = $this->getClient();
  58. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  59. $iterator = $client->lrange('metavars', 0, -1);
  60. $iterator->drop(false);
  61. $this->assertTrue($client->isConnected());
  62. $this->assertEquals('PONG', $client->ping());
  63. }
  64. /**
  65. * @group connected
  66. */
  67. public function testDropWithTrueDropsUnderlyingConnection()
  68. {
  69. $client = $this->getClient();
  70. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  71. $iterator = $client->lrange('metavars', 0, -1);
  72. $iterator->drop(true);
  73. $this->assertFalse($client->isConnected());
  74. $this->assertEquals('PONG', $client->ping());
  75. }
  76. /**
  77. * @group connected
  78. */
  79. public function testGarbageCollectorDropsUnderlyingConnection()
  80. {
  81. $client = $this->getClient();
  82. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  83. $iterator = $client->lrange('metavars', 0, -1);
  84. unset($iterator);
  85. $this->assertFalse($client->isConnected());
  86. $this->assertEquals('PONG', $client->ping());
  87. }
  88. // ******************************************************************** //
  89. // ---- HELPER METHODS ------------------------------------------------ //
  90. // ******************************************************************** //
  91. /**
  92. * Returns a new client instance.
  93. *
  94. * @return Client
  95. */
  96. protected function getClient()
  97. {
  98. $parameters = $this->getParameters(array('read_write_timeout' => 2));
  99. $protocol = new TextProtocolProcessor();
  100. $protocol->useIterableMultibulk(true);
  101. $connection = new CompositeStreamConnection($parameters, $protocol);
  102. $client = new Client($connection);
  103. $client->connect();
  104. $client->flushdb();
  105. return $client;
  106. }
  107. }