MultiBulkResponseSimpleTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Iterators;
  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\Iterators\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 testSyncWithFalseConsumesReplyFromUnderlyingConnection()
  44. {
  45. $client = $this->getClient();
  46. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  47. $iterator = $client->lrange('metavars', 0, -1);
  48. $iterator->sync(false);
  49. $this->assertTrue($client->isConnected());
  50. $this->assertTrue($client->ping());
  51. }
  52. /**
  53. * @group connected
  54. */
  55. public function testSyncWithTrueDropsUnderlyingConnection()
  56. {
  57. $client = $this->getClient();
  58. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  59. $iterator = $client->lrange('metavars', 0, -1);
  60. $iterator->sync(true);
  61. $this->assertFalse($client->isConnected());
  62. $this->assertTrue($client->ping());
  63. }
  64. /**
  65. * @group connected
  66. */
  67. public function testGarbageCollectorDropsUnderlyingConnection()
  68. {
  69. $client = $this->getClient();
  70. $client->rpush('metavars', 'foo', 'hoge', 'lol');
  71. $iterator = $client->lrange('metavars', 0, -1);
  72. unset($iterator);
  73. $this->assertFalse($client->isConnected());
  74. $this->assertTrue($client->ping());
  75. }
  76. // ******************************************************************** //
  77. // ---- HELPER METHODS ------------------------------------------------ //
  78. // ******************************************************************** //
  79. /**
  80. * Returns a new client instance.
  81. *
  82. * @return Client
  83. */
  84. protected function getClient()
  85. {
  86. $parameters = array(
  87. 'host' => REDIS_SERVER_HOST,
  88. 'port' => REDIS_SERVER_PORT,
  89. 'iterable_multibulk' => true,
  90. 'read_write_timeout' => 2,
  91. );
  92. $client = new Client($parameters, REDIS_SERVER_VERSION);
  93. $client->connect();
  94. $client->select(REDIS_SERVER_DBNUM);
  95. $client->flushdb();
  96. return $client;
  97. }
  98. }