MultiBulkResponseTupleTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 MultiBulkResponseTupleTest extends StandardTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. * @expectedException RuntimeException
  21. * @expectedExceptionMessage Cannot initialize a tuple iterator with an already initiated iterator
  22. */
  23. public function testInitiatedMultiBulkIteratorsAreNotValid()
  24. {
  25. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  26. $iterator = new MultiBulkResponseSimple($connection, 2);
  27. $iterator->next();
  28. new MultiBulkResponseTuple($iterator);
  29. }
  30. /**
  31. * @group disconnected
  32. * @expectedException UnexpectedValueException
  33. * @expectedExceptionMessage Invalid reply size for a tuple iterator [3]
  34. */
  35. public function testMultiBulkWithOddSizesAreInvalid()
  36. {
  37. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  38. $iterator = new MultiBulkResponseSimple($connection, 3);
  39. new MultiBulkResponseTuple($iterator);
  40. }
  41. /**
  42. * @group connected
  43. */
  44. public function testIterableMultibulk()
  45. {
  46. $client = $this->getClient();
  47. $client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
  48. $this->assertInstanceOf('OuterIterator', $iterator = $client->zrange('metavars', 0, -1, 'withscores')->asTuple());
  49. $this->assertInstanceOf('Predis\Iterator\MultiBulkResponseTuple', $iterator);
  50. $this->assertInstanceOf('Predis\Iterator\MultiBulkResponseSimple', $iterator->getInnerIterator());
  51. $this->assertTrue($iterator->valid());
  52. $this->assertSame(3, $iterator->count());
  53. $this->assertSame(array('foo', '1'), $iterator->current());
  54. $this->assertSame(1, $iterator->next());
  55. $this->assertTrue($iterator->valid());
  56. $this->assertSame(array('hoge', '2'), $iterator->current());
  57. $this->assertSame(2, $iterator->next());
  58. $this->assertTrue($iterator->valid());
  59. $this->assertSame(array('lol', '3'), $iterator->current());
  60. $this->assertSame(3, $iterator->next());
  61. $this->assertFalse($iterator->valid());
  62. $this->assertTrue($client->ping());
  63. }
  64. /**
  65. * @group connected
  66. */
  67. public function testGarbageCollectorDropsUnderlyingConnection()
  68. {
  69. $client = $this->getClient();
  70. $client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
  71. $iterator = $client->zrange('metavars', 0, -1, 'withscores')->asTuple();
  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. $options = array(
  93. 'profile' => REDIS_SERVER_VERSION,
  94. );
  95. $client = new Client($parameters, $options);
  96. $client->connect();
  97. $client->select(REDIS_SERVER_DBNUM);
  98. $client->flushdb();
  99. return $client;
  100. }
  101. }