MultiBulkResponseTupleTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 MultiBulkResponseTupleTest extends StandardTestCase
  17. {
  18. /**
  19. * @group connected
  20. */
  21. public function testIterableMultibulk()
  22. {
  23. $client = $this->getClient();
  24. $client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
  25. $this->assertInstanceOf('OuterIterator', $iterator = $client->zrange('metavars', 0, -1, 'withscores'));
  26. $this->assertInstanceOf('Predis\Iterators\MultiBulkResponseTuple', $iterator);
  27. $this->assertInstanceOf('Predis\Iterators\MultiBulkResponseSimple', $iterator->getInnerIterator());
  28. $this->assertTrue($iterator->valid());
  29. $this->assertSame(3, $iterator->count());
  30. $this->assertSame(array('foo', '1'), $iterator->current());
  31. $this->assertSame(1, $iterator->next());
  32. $this->assertTrue($iterator->valid());
  33. $this->assertSame(array('hoge', '2'), $iterator->current());
  34. $this->assertSame(2, $iterator->next());
  35. $this->assertTrue($iterator->valid());
  36. $this->assertSame(array('lol', '3'), $iterator->current());
  37. $this->assertSame(3, $iterator->next());
  38. $this->assertFalse($iterator->valid());
  39. $this->assertTrue($client->ping());
  40. }
  41. /**
  42. * @group connected
  43. */
  44. public function testGarbageCollectorDropsUnderlyingConnection()
  45. {
  46. $client = $this->getClient();
  47. $client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
  48. $iterator = $client->zrange('metavars', 0, -1, 'withscores');
  49. unset($iterator);
  50. $this->assertFalse($client->isConnected());
  51. $this->assertTrue($client->ping());
  52. }
  53. // ******************************************************************** //
  54. // ---- HELPER METHODS ------------------------------------------------ //
  55. // ******************************************************************** //
  56. /**
  57. * Returns a new client instance.
  58. *
  59. * @return Client
  60. */
  61. protected function getClient()
  62. {
  63. $parameters = array(
  64. 'host' => REDIS_SERVER_HOST,
  65. 'port' => REDIS_SERVER_PORT,
  66. 'iterable_multibulk' => true,
  67. 'read_write_timeout' => 2,
  68. );
  69. $client = new Client($parameters, REDIS_SERVER_VERSION);
  70. $client->connect();
  71. $client->select(REDIS_SERVER_DBNUM);
  72. $client->flushdb();
  73. return $client;
  74. }
  75. }