MultiBulkTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 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. $iterator->valid();
  30. $this->assertSame(3, $iterator->count());
  31. $this->assertSame('foo', $iterator->current());
  32. $iterator->next();
  33. $this->assertTrue($iterator->valid());
  34. $this->assertSame('hoge', $iterator->current());
  35. $iterator->next();
  36. $this->assertTrue($iterator->valid());
  37. $this->assertSame('lol', $iterator->current());
  38. $iterator->next();
  39. $this->assertFalse($iterator->valid());
  40. $this->assertEquals('PONG', $client->ping());
  41. }
  42. /**
  43. * @group connected
  44. */
  45. public function testDropWithFalseConsumesResponseFromUnderlyingConnection()
  46. {
  47. $client = $this->getClient();
  48. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  49. $iterator = $client->lrange('metavars', 0, -1);
  50. $iterator->drop(false);
  51. $this->assertTrue($client->isConnected());
  52. $this->assertEquals('PONG', $client->ping());
  53. }
  54. /**
  55. * @group connected
  56. */
  57. public function testDropWithTrueDropsUnderlyingConnection()
  58. {
  59. $client = $this->getClient();
  60. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  61. $iterator = $client->lrange('metavars', 0, -1);
  62. $iterator->drop(true);
  63. $this->assertFalse($client->isConnected());
  64. $this->assertEquals('PONG', $client->ping());
  65. }
  66. /**
  67. * @group connected
  68. */
  69. public function testGarbageCollectorDropsUnderlyingConnection()
  70. {
  71. $client = $this->getClient();
  72. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  73. $iterator = $client->lrange('metavars', 0, -1);
  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. }