123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- /*
- * This file is part of the Predis package.
- *
- * (c) Daniele Alessandri <suppakilla@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Predis\Collection\Iterator;
- use PredisTestCase;
- /**
- * @group realm-iterators
- */
- class ListKeyTest extends PredisTestCase
- {
- /**
- * @group disconnected
- * @expectedException \Predis\NotSupportedException
- * @expectedExceptionMessage 'LRANGE' is not supported by the current command factory.
- */
- public function testThrowsExceptionOnMissingCommand()
- {
- $commands = $this->getMock('Predis\Command\FactoryInterface');
- $commands
- ->expects($this->any())
- ->method('supportsCommand')
- ->will($this->returnValue(false));
- $client = $this->getMock('Predis\ClientInterface');
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($commands));
- new ListKey($client, 'key:list');
- }
- /**
- * @group disconnected
- */
- public function testIterationWithNoResults()
- {
- $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- $client
- ->expects($this->once())
- ->method('lrange')
- ->with('key:list', 0, 9)
- ->will($this->returnValue(
- array()
- ));
- $iterator = new ListKey($client, 'key:list');
- $iterator->rewind();
- $this->assertFalse($iterator->valid());
- }
- /**
- * @group disconnected
- */
- public function testIterationOnSingleFetch()
- {
- $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- $client
- ->expects($this->once())
- ->method('lrange')
- ->with('key:list', 0, 9)
- ->will($this->returnValue(
- array('item:1', 'item:2', 'item:3')
- ));
- $iterator = new ListKey($client, 'key:list');
- $iterator->rewind();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:1', $iterator->current());
- $this->assertSame(0, $iterator->key());
- $iterator->next();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:2', $iterator->current());
- $this->assertSame(1, $iterator->key());
- $iterator->next();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:3', $iterator->current());
- $this->assertSame(2, $iterator->key());
- $iterator->next();
- $this->assertFalse($iterator->valid());
- }
- /**
- * @group disconnected
- */
- public function testIterationOnMultipleFetches()
- {
- $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- $client
- ->expects($this->at(1))
- ->method('lrange')
- ->with('key:list', 0, 9)
- ->will($this->returnValue(
- array(
- 'item:1', 'item:2', 'item:3', 'item:4', 'item:5',
- 'item:6', 'item:7', 'item:8', 'item:9', 'item:10',
- )
- ));
- $client->expects($this->at(2))
- ->method('lrange')
- ->with('key:list', 10, 19)
- ->will($this->returnValue(array('item:11', 'item:12')));
- $iterator = new ListKey($client, 'key:list');
- for ($i = 1, $iterator->rewind(); $i <= 12; $i++, $iterator->next()) {
- $this->assertTrue($iterator->valid());
- $this->assertSame("item:$i", $iterator->current());
- $this->assertSame($i - 1, $iterator->key());
- }
- $this->assertFalse($iterator->valid());
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage The $count argument must be a positive integer.
- */
- public function testThrowsExceptionOnConstructorWithNonIntegerCountParameter()
- {
- $client = $this->getMock('Predis\ClientInterface');
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- new ListKey($client, 'key:list', 'wrong');
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage The $count argument must be a positive integer.
- */
- public function testThrowsExceptionOnConstructorWithNegativeCountParameter()
- {
- $client = $this->getMock('Predis\ClientInterface');
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- new ListKey($client, 'key:list', 'wrong');
- }
- /**
- * @group disconnected
- */
- public function testIterationWithCountParameter()
- {
- $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- $client
- ->expects($this->at(1))
- ->method('lrange')
- ->with('key:list', 0, 4)
- ->will($this->returnValue(
- array('item:1', 'item:2')
- ));
- $iterator = new ListKey($client, 'key:list', 5);
- $iterator->rewind();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:1', $iterator->current());
- $this->assertSame(0, $iterator->key());
- $iterator->next();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:2', $iterator->current());
- $this->assertSame(1, $iterator->key());
- $iterator->next();
- $this->assertFalse($iterator->valid());
- }
- /**
- * @group disconnected
- */
- public function testIterationWithCountParameterOnMultipleFetches()
- {
- $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- $client
- ->expects($this->at(1))
- ->method('lrange')
- ->with('key:list', 0, 1)
- ->will($this->returnValue(
- array('item:1', 'item:2')
- ));
- $client
- ->expects($this->at(2))
- ->method('lrange')
- ->with('key:list', 2, 3)
- ->will($this->returnValue(
- array('item:3')
- ));
- $iterator = new ListKey($client, 'key:list', 2);
- $iterator->rewind();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:1', $iterator->current());
- $this->assertSame(0, $iterator->key());
- $iterator->next();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:2', $iterator->current());
- $this->assertSame(1, $iterator->key());
- $iterator->next();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:3', $iterator->current());
- $this->assertSame(2, $iterator->key());
- $iterator->next();
- $this->assertFalse($iterator->valid());
- }
- /**
- * @group disconnected
- */
- public function testIterationRewindable()
- {
- $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
- $client
- ->expects($this->any())
- ->method('getCommandFactory')
- ->will($this->returnValue($this->getCommandFactory()));
- $client
- ->expects($this->exactly(2))
- ->method('lrange')
- ->with('key:list', 0, 9)
- ->will($this->returnValue(
- array('item:1', 'item:2')
- ));
- $iterator = new ListKey($client, 'key:list');
- $iterator->rewind();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:1', $iterator->current());
- $this->assertSame(0, $iterator->key());
- $iterator->rewind();
- $this->assertTrue($iterator->valid());
- $this->assertSame('item:1', $iterator->current());
- $this->assertSame(0, $iterator->key());
- $iterator->next();
- $this->assertTrue($iterator->valid());
- $this->assertSame(1, $iterator->key());
- $this->assertSame('item:2', $iterator->current());
- $iterator->next();
- $this->assertFalse($iterator->valid());
- }
- }
|