MultiBulkResponseSimpleTest.php 3.8 KB

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