MultiBulkTest.php 4.1 KB

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