From 6d6c970ae25de9d1ccaa39644eadd17d4635177b Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 29 Aug 2020 00:20:04 +0200 Subject: [PATCH] [tests] Replace deprecated TestCase::at(). The "at" matcher will be removed in PHPUnit 10 but it is not a bad thing after all because it was cumbersome and error-prone. Took the opportunity to improve some tests while converting them. --- tests/Predis/ClientTest.php | 114 +++++----- .../Collection/Iterator/HashKeyTest.php | 157 ++++++-------- .../Collection/Iterator/KeyspaceTest.php | 133 ++++++------ .../Collection/Iterator/ListKeyTest.php | 44 ++-- .../Predis/Collection/Iterator/SetKeyTest.php | 135 +++++------- .../Collection/Iterator/SortedSetKeyTest.php | 139 ++++++------ .../Configuration/Option/ClusterTest.php | 20 +- .../Configuration/Option/ReplicationTest.php | 34 +-- .../Connection/Cluster/RedisClusterTest.php | 107 ++++----- tests/Predis/Connection/FactoryTest.php | 13 +- .../MasterSlaveReplicationTest.php | 143 +++++++----- .../Replication/SentinelReplicationTest.php | 203 ++++++++++-------- tests/Predis/Pipeline/AtomicTest.php | 133 ++++++++++-- .../Text/Handler/MultiBulkResponseTest.php | 26 +-- .../Protocol/Text/ProtocolProcessorTest.php | 26 +-- .../Protocol/Text/ResponseReaderTest.php | 26 +-- 16 files changed, 760 insertions(+), 693 deletions(-) diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index c11cc1d2..019e7c9f 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -518,15 +518,16 @@ class ClientTest extends PredisTestCase $connection = $this->getMockBuilder('Predis\Connection\ConnectionInterface')->getMock(); $connection - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($ping) - ->willReturn(new Response\Status('PONG')); - $connection - ->expects($this->at(1)) - ->method('executeCommand') - ->with($hgetall) - ->willReturn(array('foo', 'bar', 'hoge', 'piyo')); + ->withConsecutive( + array($ping), + array($hgetall) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('PONG'), + array('foo', 'bar', 'hoge', 'piyo') + ); $client = new Client($connection); @@ -655,20 +656,18 @@ class ClientTest extends PredisTestCase { $connection = $this->getMockBuilder('Predis\Connection\ConnectionInterface')->getMock(); $connection - ->expects($this->at(0)) + ->expects($this->exactly(3)) ->method('executeCommand') - ->with($this->isRedisCommand('SET', array('foo', 'bar'))) - ->willReturn(new Response\Status('OK')); - $connection - ->expects($this->at(1)) - ->method('executeCommand') - ->with($this->isRedisCommand('GET', array('foo'))) - ->willReturn('bar'); - $connection - ->expects($this->at(2)) - ->method('executeCommand') - ->with($this->isRedisCommand('PING')) - ->willReturn('PONG'); + ->withConsecutive( + array($this->isRedisCommand('SET', array('foo', 'bar'))), + array($this->isRedisCommand('GET', array('foo'))), + array($this->isRedisCommand('PING')) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + 'bar', + 'PONG' + ); $client = new Client($connection); @@ -687,15 +686,16 @@ class ClientTest extends PredisTestCase { $connection = $this->getMockBuilder('Predis\Connection\ConnectionInterface')->getMock(); $connection - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($this->isRedisCommand('SET', array('foo', 'bar'))) - ->willReturn(new Response\Status('OK')); - $connection - ->expects($this->at(1)) - ->method('executeCommand') - ->with($this->isRedisCommand('GET', array('foo'))) - ->willReturn('bar'); + ->withConsecutive( + array($this->isRedisCommand('SET', array('foo', 'bar'))), + array($this->isRedisCommand('GET', array('foo'))) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + 'bar' + ); $client = new Client($connection, array('prefix' => 'predis:')); @@ -1037,25 +1037,40 @@ class ClientTest extends PredisTestCase { $connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(); $connection - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('read') - ->willReturn(array('subscribe', 'channel', 1)); + ->willReturnOnConsecutiveCalls( + array('subscribe', 'channel', 1), + array('unsubscribe', 'channel', 0) + ); $connection - ->expects($this->at(2)) + ->expects($this->exactly(2)) ->method('writeRequest') - ->with($this->isRedisCommand('UNSUBSCRIBE')); - $connection - ->expects($this->at(3)) - ->method('read') - ->willReturn(array('unsubscribe', 'channel', 0)); + ->withConsecutive( + array($this->isRedisCommand('SUBSCRIBE')), + array($this->isRedisCommand('UNSUBSCRIBE')) + ); $callable = $this->getMockBuilder('stdClass') ->addMethods(array('__invoke')) ->getMock(); $callable - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('__invoke') - ->willReturn(false); + ->withConsecutive( + array( + $this->isInstanceOf('Predis\PubSub\Consumer'), + (object) array('kind' => 'subscribe', 'channel' => 'channel', 'payload' => 1) + ), + array( + $this->isInstanceOf('Predis\PubSub\Consumer'), + (object) array('kind' => 'unsubscribe', 'channel' => 'channel', 'payload' => 0) + ) + ) + ->willReturnOnConsecutiveCalls( + false, + null // <-- this value would be ignored as it is the callback to UNSUBSCRIBE + ); $client = new Client($connection); @@ -1135,11 +1150,13 @@ class ClientTest extends PredisTestCase */ public function testClientResendScriptCommandUsingEvalOnNoScriptErrors(): void { + $luaScriptBody = 'return redis.call(\'exists\', KEYS[1])'; + $command = $this->getMockForAbstractClass('Predis\Command\ScriptCommand', array(), '', true, true, true, array('parseResponse')); $command ->expects($this->once()) ->method('getScript') - ->willReturn('return redis.call(\'exists\', KEYS[1])'); + ->willReturn($luaScriptBody); $command ->expects($this->once()) ->method('parseResponse') @@ -1148,15 +1165,16 @@ class ClientTest extends PredisTestCase $connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(); $connection - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($command) - ->willReturn(new Response\Error('NOSCRIPT')); - $connection - ->expects($this->at(1)) - ->method('executeCommand') - ->with($this->isRedisCommand('EVAL')) - ->willReturn('OK'); + ->withConsecutive( + array($command), + array($this->isRedisCommand('EVAL', array($luaScriptBody))) + ) + ->willReturnOnConsecutiveCalls( + new Response\Error('NOSCRIPT'), + 'OK' + ); $client = new Client($connection); diff --git a/tests/Predis/Collection/Iterator/HashKeyTest.php b/tests/Predis/Collection/Iterator/HashKeyTest.php index 8173c728..45116e2f 100644 --- a/tests/Predis/Collection/Iterator/HashKeyTest.php +++ b/tests/Predis/Collection/Iterator/HashKeyTest.php @@ -178,19 +178,16 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('hscan') - ->with('key:hash', 0, array()) - ->will($this->returnValue( - array(2, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')) - )); - $client - ->expects($this->at(2)) - ->method('hscan') - ->with('key:hash', 2, array()) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array()), + array('key:hash', 2, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')), array(0, array('field:3rd' => 'value:3rd')) - )); + ); $iterator = new HashKey($client, 'key:hash'); @@ -228,19 +225,16 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('hscan') - ->with('key:hash', 0, array()) - ->will($this->returnValue( - array(4, array()) - )); - $client - ->expects($this->at(2)) - ->method('hscan') - ->with('key:hash', 4, array()) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array()), + array('key:hash', 4, array()) + ) + ->willReturnOnConsecutiveCalls( + array(4, array()), array(0, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')) - )); + ); $iterator = new HashKey($client, 'key:hash'); @@ -273,26 +267,18 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(3)) ->method('hscan') - ->with('key:hash', 0, array()) - ->will($this->returnValue( - array(2, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')) - )); - $client - ->expects($this->at(2)) - ->method('hscan') - ->with('key:hash', 2, array()) - ->will($this->returnValue( - array(5, array()) - )); - $client - ->expects($this->at(3)) - ->method('hscan') - ->with('key:hash', 5, array()) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array()), + array('key:hash', 2, array()), + array('key:hash', 5, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')), + array(5, array()), array(0, array('field:3rd' => 'value:3rd')) - )); + ); $iterator = new HashKey($client, 'key:hash'); @@ -330,12 +316,16 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('hscan') - ->with('key:hash', 0, array('MATCH' => 'field:*')) - ->will($this->returnValue( - array(2, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')) - )); + ->withConsecutive( + array('key:hash', 0, array('MATCH' => 'field:*')), + array('key:hash', 2, array('MATCH' => 'field:*')) + ) + ->willReturn( + array(2, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')), + array(0, array()) + ); $iterator = new HashKey($client, 'key:hash', 'field:*'); @@ -368,19 +358,16 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('hscan') - ->with('key:hash', 0, array('MATCH' => 'field:*')) - ->will($this->returnValue( - array(1, array('field:1st' => 'value:1st')) - )); - $client - ->expects($this->at(2)) - ->method('hscan') - ->with('key:hash', 1, array('MATCH' => 'field:*')) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array('MATCH' => 'field:*')), + array('key:hash', 1, array('MATCH' => 'field:*')) + ) + ->willReturn( + array(1, array('field:1st' => 'value:1st')), array(0, array('field:2nd' => 'value:2nd')) - )); + ); $iterator = new HashKey($client, 'key:hash', 'field:*'); @@ -413,12 +400,14 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('hscan') - ->with('key:hash', 0, array('COUNT' => 2)) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array('COUNT' => 2)) + ) + ->willReturn( array(0, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')) - )); + ); $iterator = new HashKey($client, 'key:hash', null, 2); @@ -451,19 +440,16 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('hscan') - ->with('key:hash', 0, array('COUNT' => 1)) - ->will($this->returnValue( - array(1, array('field:1st' => 'value:1st')) - )); - $client - ->expects($this->at(2)) - ->method('hscan') - ->with('key:hash', 1, array('COUNT' => 1)) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array('COUNT' => 1)), + array('key:hash', 1, array('COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('field:1st' => 'value:1st')), array(0, array('field:2nd' => 'value:2nd')) - )); + ); $iterator = new HashKey($client, 'key:hash', null, 1); @@ -496,12 +482,14 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once(1)) ->method('hscan') - ->with('key:hash', 0, array('MATCH' => 'field:*', 'COUNT' => 2)) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array('MATCH' => 'field:*', 'COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('field:1st' => 'value:1st', 'field:2nd' => 'value:2nd')) - )); + ); $iterator = new HashKey($client, 'key:hash', 'field:*', 2); @@ -534,19 +522,16 @@ class HashKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('hscan') - ->with('key:hash', 0, array('MATCH' => 'field:*', 'COUNT' => 1)) - ->will($this->returnValue( - array(1, array('field:1st' => 'value:1st')) - )); - $client - ->expects($this->at(2)) - ->method('hscan') - ->with('key:hash', 1, array('MATCH' => 'field:*', 'COUNT' => 1)) - ->will($this->returnValue( + ->withConsecutive( + array('key:hash', 0, array('MATCH' => 'field:*', 'COUNT' => 1)), + array('key:hash', 1, array('MATCH' => 'field:*', 'COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('field:1st' => 'value:1st')), array(0, array('field:2nd' => 'value:2nd')) - )); + ); $iterator = new HashKey($client, 'key:hash', 'field:*', 1); diff --git a/tests/Predis/Collection/Iterator/KeyspaceTest.php b/tests/Predis/Collection/Iterator/KeyspaceTest.php index 835a6412..f9f4e741 100644 --- a/tests/Predis/Collection/Iterator/KeyspaceTest.php +++ b/tests/Predis/Collection/Iterator/KeyspaceTest.php @@ -128,17 +128,14 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('scan') - ->with(0, array()) - ->willReturn( - array(2, array('key:1st', 'key:2nd')) - ); - $client - ->expects($this->at(2)) - ->method('scan') - ->with(2, array()) - ->willReturn( + ->withConsecutive( + array(0, array()), + array(2, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('key:1st', 'key:2nd')), array(0, array('key:3rd')) ); @@ -178,17 +175,14 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('scan') - ->with(0, array()) - ->willReturn( + ->withConsecutive( + array(0, array()), array(4, array()) - ); - $client - ->expects($this->at(2)) - ->method('scan') - ->with(4, array()) - ->willReturn( + ) + ->willReturnOnConsecutiveCalls( + array(4, array()), array(0, array('key:1st', 'key:2nd')) ); @@ -223,24 +217,16 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(3)) ->method('scan') - ->with(0, array()) - ->willReturn( - array(2, array('key:1st', 'key:2nd')) - ); - $client - ->expects($this->at(2)) - ->method('scan') - ->with(2, array()) - ->willReturn( + ->withConsecutive( + array(0, array()), + array(2, array()), array(5, array()) - ); - $client - ->expects($this->at(3)) - ->method('scan') - ->with(5, array()) - ->willReturn( + ) + ->willReturnOnConsecutiveCalls( + array(2, array('key:1st', 'key:2nd')), + array(5, array()), array(0, array('key:3rd')) ); @@ -280,10 +266,12 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('scan') - ->with(0, array('MATCH' => 'key:*')) - ->willReturn( + ->withConsecutive( + array(0, array('MATCH' => 'key:*')) + ) + ->willReturnOnConsecutiveCalls( array(0, array('key:1st', 'key:2nd')) ); @@ -318,17 +306,14 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('scan') - ->with(0, array('MATCH' => 'key:*')) - ->willReturn( - array(1, array('key:1st')) - ); - $client - ->expects($this->at(2)) - ->method('scan') - ->with(1, array('MATCH' => 'key:*')) - ->willReturn( + ->withConsecutive( + array(0, array('MATCH' => 'key:*')), + array(1, array('MATCH' => 'key:*')) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('key:1st')), array(0, array('key:2nd')) ); @@ -363,10 +348,12 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('scan') - ->with(0, array('COUNT' => 2)) - ->willReturn( + ->withConsecutive( + array(0, array('COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('key:1st', 'key:2nd')) ); @@ -400,16 +387,17 @@ class KeyspaceTest extends PredisTestCase ->expects($this->any()) ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); + + $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('scan') - ->with(0, array('COUNT' => 1)) - ->willReturn(array(1, array('key:1st'))); - $client - ->expects($this->at(2)) - ->method('scan') - ->with(1, array('COUNT' => 1)) - ->willReturn( + ->withConsecutive( + array(0, array('COUNT' => 1)), + array(1, array('COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('key:1st')), array(0, array('key:2nd')) ); @@ -444,10 +432,12 @@ class KeyspaceTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('scan') - ->with(0, array('MATCH' => 'key:*', 'COUNT' => 2)) - ->willReturn( + ->withConsecutive( + array(0, array('MATCH' => 'key:*', 'COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('key:1st', 'key:2nd')) ); @@ -481,18 +471,17 @@ class KeyspaceTest extends PredisTestCase ->expects($this->any()) ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); + + $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('scan') - ->with(0, array('MATCH' => 'key:*', 'COUNT' => 1)) - ->willReturn( - array(1, array('key:1st')) - ); - $client - ->expects($this->at(2)) - ->method('scan') - ->with(1, array('MATCH' => 'key:*', 'COUNT' => 1)) - ->willReturn( + ->withConsecutive( + array(0, array('MATCH' => 'key:*', 'COUNT' => 1)), + array(1, array('MATCH' => 'key:*', 'COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('key:1st')), array(0, array('key:2nd')) ); diff --git a/tests/Predis/Collection/Iterator/ListKeyTest.php b/tests/Predis/Collection/Iterator/ListKeyTest.php index 10615997..acb5ba0a 100644 --- a/tests/Predis/Collection/Iterator/ListKeyTest.php +++ b/tests/Predis/Collection/Iterator/ListKeyTest.php @@ -131,20 +131,19 @@ class ListKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('lrange') - ->with('key:list', 0, 9) - ->will($this->returnValue( + ->withConsecutive( + array('key:list', 0, 9), + array('key:list', 10, 19) + ) + ->willReturnOnConsecutiveCalls( 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) - ->willReturn(array('item:11', 'item:12')); + ), + array('item:11', 'item:12') + ); $iterator = new ListKey($client, 'key:list'); @@ -212,12 +211,12 @@ class ListKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('lrange') ->with('key:list', 0, 4) - ->will($this->returnValue( + ->willReturn( array('item:1', 'item:2') - )); + ); $iterator = new ListKey($client, 'key:list', 5); @@ -250,19 +249,16 @@ class ListKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->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( + ->withConsecutive( + array('key:list', 0, 1), + array('key:list', 2, 3) + ) + ->willReturnOnConsecutiveCalls( + array('item:1', 'item:2'), array('item:3') - )); + ); $iterator = new ListKey($client, 'key:list', 2); diff --git a/tests/Predis/Collection/Iterator/SetKeyTest.php b/tests/Predis/Collection/Iterator/SetKeyTest.php index a4e51065..00d8c269 100644 --- a/tests/Predis/Collection/Iterator/SetKeyTest.php +++ b/tests/Predis/Collection/Iterator/SetKeyTest.php @@ -128,17 +128,14 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('sscan') - ->with('key:set', 0, array()) - ->willReturn( - array(2, array('member:1st', 'member:2nd')) - ); - $client - ->expects($this->at(2)) - ->method('sscan') - ->with('key:set', 2, array()) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array()), + array('key:set', 2, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('member:1st', 'member:2nd')), array(0, array('member:3rd')) ); @@ -178,17 +175,14 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('sscan') - ->with('key:set', 0, array()) - ->willReturn( - array(4, array()) - ); - $client - ->expects($this->at(2)) - ->method('sscan') - ->with('key:set', 4, array()) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array()), + array('key:set', 4, array()) + ) + ->willReturnOnConsecutiveCalls( + array(4, array()), array(0, array('member:1st', 'member:2nd')) ); @@ -223,24 +217,16 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(3)) ->method('sscan') - ->with('key:set', 0, array()) - ->willReturn( - array(2, array('member:1st', 'member:2nd')) - ); - $client - ->expects($this->at(2)) - ->method('sscan') - ->with('key:set', 2, array()) - ->willReturn( - array(5, array()) - ); - $client - ->expects($this->at(3)) - ->method('sscan') - ->with('key:set', 5, array()) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array()), + array('key:set', 2, array()), + array('key:set', 5, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('member:1st', 'member:2nd')), + array(5, array()), array(0, array('member:3rd')) ); @@ -280,10 +266,12 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('sscan') - ->with('key:set', 0, array('MATCH' => 'member:*')) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array('MATCH' => 'member:*')) + ) + ->willReturnOnConsecutiveCalls( array(0, array('member:1st', 'member:2nd')) ); @@ -318,17 +306,14 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('sscan') - ->with('key:set', 0, array('MATCH' => 'member:*')) - ->willReturn( - array(1, array('member:1st')) - ); - $client - ->expects($this->at(2)) - ->method('sscan') - ->with('key:set', 1, array('MATCH' => 'member:*')) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array('MATCH' => 'member:*')), + array('key:set', 1, array('MATCH' => 'member:*')) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('member:1st')), array(0, array('member:2nd')) ); @@ -363,10 +348,12 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('sscan') - ->with('key:set', 0, array('COUNT' => 2)) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array('COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('member:1st', 'member:2nd')) ); @@ -401,17 +388,14 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('sscan') - ->with('key:set', 0, array('COUNT' => 1)) - ->willReturn( - array(1, array('member:1st')) - ); - $client - ->expects($this->at(2)) - ->method('sscan') - ->with('key:set', 1, array('COUNT' => 1)) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array('COUNT' => 1)), + array('key:set', 1, array('COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('member:1st')), array(0, array('member:2nd')) ); @@ -446,10 +430,12 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('sscan') - ->with('key:set', 0, array('MATCH' => 'member:*', 'COUNT' => 2)) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array('MATCH' => 'member:*', 'COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('member:1st', 'member:2nd')) ); @@ -484,17 +470,14 @@ class SetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('sscan') - ->with('key:set', 0, array('MATCH' => 'member:*', 'COUNT' => 1)) - ->willReturn( - array(1, array('member:1st')) - ); - $client - ->expects($this->at(2)) - ->method('sscan') - ->with('key:set', 1, array('MATCH' => 'member:*', 'COUNT' => 1)) - ->willReturn( + ->withConsecutive( + array('key:set', 0, array('MATCH' => 'member:*', 'COUNT' => 1)), + array('key:set', 1, array('MATCH' => 'member:*', 'COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('member:1st')), array(0, array('member:2nd')) ); diff --git a/tests/Predis/Collection/Iterator/SortedSetKeyTest.php b/tests/Predis/Collection/Iterator/SortedSetKeyTest.php index 7c841d50..383702f6 100644 --- a/tests/Predis/Collection/Iterator/SortedSetKeyTest.php +++ b/tests/Predis/Collection/Iterator/SortedSetKeyTest.php @@ -173,17 +173,14 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('zscan') - ->with('key:zset', 0, array()) - ->willReturn( - array(2, array('member:1st' => 1.0, 'member:2nd' => 2.0)) - ); - $client - ->expects($this->at(2)) - ->method('zscan') - ->with('key:zset', 2, array()) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array()), + array('key:zset', 2, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('member:1st' => 1.0, 'member:2nd' => 2.0)), array(0, array('member:3rd' => 3.0)) ); @@ -223,17 +220,14 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('zscan') - ->with('key:zset', 0, array()) - ->willReturn( - array(4, array()) - ); - $client - ->expects($this->at(2)) - ->method('zscan') - ->with('key:zset', 4, array()) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array()), + array('key:zset', 4, array()) + ) + ->willReturnOnConsecutiveCalls( + array(4, array()), array(0, array('member:1st' => 1.0, 'member:2nd' => 2.0)) ); @@ -268,24 +262,16 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(3)) ->method('zscan') - ->with('key:zset', 0, array()) - ->willReturn( - array(2, array('member:1st' => 1.0, 'member:2nd' => 2.0)) - ); - $client - ->expects($this->at(2)) - ->method('zscan') - ->with('key:zset', 2, array()) - ->willReturn( - array(5, array()) - ); - $client - ->expects($this->at(3)) - ->method('zscan') - ->with('key:zset', 5, array()) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array()), + array('key:zset', 2, array()), + array('key:zset', 5, array()) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('member:1st' => 1.0, 'member:2nd' => 2.0)), + array(5, array()), array(0, array('member:3rd' => 3.0)) ); @@ -325,11 +311,15 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('zscan') - ->with('key:zset', 0, array('MATCH' => 'member:*')) - ->willReturn( - array(2, array('member:1st' => 1.0, 'member:2nd' => 2.0)) + ->withConsecutive( + array('key:zset', 0, array('MATCH' => 'member:*')), + array('key:zset', 2, array('MATCH' => 'member:*')) + ) + ->willReturnOnConsecutiveCalls( + array(2, array('member:1st' => 1.0, 'member:2nd' => 2.0)), + array(0, array()) ); $iterator = new SortedSetKey($client, 'key:zset', 'member:*'); @@ -363,17 +353,14 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('zscan') - ->with('key:zset', 0, array('MATCH' => 'member:*')) - ->willReturn( - array(1, array('member:1st' => 1.0)) - ); - $client - ->expects($this->at(2)) - ->method('zscan') - ->with('key:zset', 1, array('MATCH' => 'member:*')) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array('MATCH' => 'member:*')), + array('key:zset', 1, array('MATCH' => 'member:*')) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('member:1st' => 1.0)), array(0, array('member:2nd' => 2.0)) ); @@ -408,10 +395,12 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('zscan') - ->with('key:zset', 0, array('COUNT' => 2)) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array('COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('member:1st' => 1.0, 'member:2nd' => 2.0)) ); @@ -446,17 +435,14 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('zscan') - ->with('key:zset', 0, array('COUNT' => 1)) - ->willReturn( - array(1, array('member:1st' => 1.0)) - ); - $client - ->expects($this->at(2)) - ->method('zscan') - ->with('key:zset', 1, array('COUNT' => 1)) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array('COUNT' => 1)), + array('key:zset', 1, array('COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('member:1st' => 1.0)), array(0, array('member:2nd' => 2.0)) ); @@ -491,10 +477,12 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->once()) ->method('zscan') - ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 2)) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 2)) + ) + ->willReturnOnConsecutiveCalls( array(0, array('member:1st' => 1.0, 'member:2nd' => 2.0)) ); @@ -529,17 +517,14 @@ class SortedSetKeyTest extends PredisTestCase ->method('getCommandFactory') ->willReturn($this->getCommandFactory()); $client - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('zscan') - ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 1)) - ->willReturn( - array(1, array('member:1st' => 1.0)) - ); - $client - ->expects($this->at(2)) - ->method('zscan') - ->with('key:zset', 1, array('MATCH' => 'member:*', 'COUNT' => 1)) - ->willReturn( + ->withConsecutive( + array('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 1)), + array('key:zset', 1, array('MATCH' => 'member:*', 'COUNT' => 1)) + ) + ->willReturnOnConsecutiveCalls( + array(1, array('member:1st' => 1.0)), array(0, array('member:2nd' => 2.0)) ); diff --git a/tests/Predis/Configuration/Option/ClusterTest.php b/tests/Predis/Configuration/Option/ClusterTest.php index 93ff7338..28057060 100644 --- a/tests/Predis/Configuration/Option/ClusterTest.php +++ b/tests/Predis/Configuration/Option/ClusterTest.php @@ -113,20 +113,18 @@ class ClusterTest extends PredisTestCase /** @var OptionsInterface|MockObject */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); + $options - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('__get') - ->with('connections') - ->will($this->returnValue( - $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock() - )); - $options - ->expects($this->at(1)) - ->method('__get') - ->with('crc16') - ->will($this->returnValue( + ->withConsecutive( + array('connections'), + array('crc16') + ) + ->willReturnOnConsecutiveCalls( + $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(), $this->getMockBuilder('Predis\Cluster\Hash\HashGeneratorInterface')->getMock() - )); + ); $this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'redis')); $this->assertInstanceOf('Predis\Connection\Cluster\RedisCluster', $initializer($parameters = array())); diff --git a/tests/Predis/Configuration/Option/ReplicationTest.php b/tests/Predis/Configuration/Option/ReplicationTest.php index 241b9d33..fbb2cd85 100644 --- a/tests/Predis/Configuration/Option/ReplicationTest.php +++ b/tests/Predis/Configuration/Option/ReplicationTest.php @@ -46,15 +46,16 @@ class ReplicationTest extends PredisTestCase /** @var OptionsInterface|MockObject */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); $options - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('__get') - ->with('autodiscovery') - ->willReturn(true); - $options - ->expects($this->at(1)) - ->method('__get') - ->with('connections') - ->willReturn($connectionFactory); + ->withConsecutive( + array('autodiscovery'), + array('connections') + ) + ->willReturnOnConsecutiveCalls( + true, + $connectionFactory + ); $this->assertInstanceOf('Closure', $initializer = $option->getDefault($options)); $this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $connection = $initializer($options)); @@ -142,17 +143,16 @@ class ReplicationTest extends PredisTestCase /** @var OptionsInterface|MockObject */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); $options - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('__get') - ->with('service') - ->willReturn('mymaster'); - $options - ->expects($this->at(1)) - ->method('__get') - ->with('connections') - ->will($this->returnValue( + ->withConsecutive( + array('service'), + array('connections') + ) + ->willReturnOnConsecutiveCalls( + 'mymaster', $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock() - )); + ); $parameters = array( $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(), diff --git a/tests/Predis/Connection/Cluster/RedisClusterTest.php b/tests/Predis/Connection/Cluster/RedisClusterTest.php index efaa622f..afb8a618 100644 --- a/tests/Predis/Connection/Cluster/RedisClusterTest.php +++ b/tests/Predis/Connection/Cluster/RedisClusterTest.php @@ -315,21 +315,26 @@ class RedisClusterTest extends PredisTestCase $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); $factory - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6383', - )) - ->willReturn($connection3); - $factory - ->expects($this->at(1)) - ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6384', - )) - ->willReturn($connection4); + ->withConsecutive( + array( + array( + 'host' => '127.0.0.1', + 'port' => '6383', + ) + ), + array( + array( + 'host' => '127.0.0.1', + 'port' => '6384', + ) + ) + ) + ->willReturnOnConsecutiveCalls( + $connection3, + $connection4 + ); // TODO: I'm not sure about mocking a protected method, but it'll do for now /** @var Connection\Cluster\RedisCluster|MockObject */ @@ -623,19 +628,16 @@ class RedisClusterTest extends PredisTestCase $connection4 = $this->getMockConnection('tcp://127.0.0.1:9381'); $connection4 - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($this->isRedisCommand( - 'GET', array('node:1001') - )) - ->willReturn('value:1001'); - $connection4 - ->expects($this->at(1)) - ->method('executeCommand') - ->with($this->isRedisCommand( - 'GET', array('node:5001') - )) - ->willReturn('value:5001'); + ->withConsecutive( + array($this->isRedisCommand('GET', array('node:1001'))), + array($this->isRedisCommand('GET', array('node:5001'))) + ) + ->willReturnOnConsecutiveCalls( + 'value:1001', + 'value:5001' + ); /** @var Connection\FactoryInterface|MockObject */ $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); @@ -950,14 +952,16 @@ class RedisClusterTest extends PredisTestCase $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380'); $connection2 - ->expects($this->at(2)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($this->isRedisCommand('ASKING')); - $connection2 - ->expects($this->at(3)) - ->method('executeCommand') - ->with($command) - ->willReturn('foobar'); + ->withConsecutive( + array($this->isRedisCommand('ASKING')), + array($this->isRedisCommand($command)) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + 'foobar' + ); /** @var Connection\FactoryInterface|MockObject */ $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); @@ -999,16 +1003,16 @@ class RedisClusterTest extends PredisTestCase $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381'); $connection3 - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($this->isRedisCommand( - 'ASKING' - )); - $connection3 - ->expects($this->at(1)) - ->method('executeCommand') - ->with($command) - ->willReturn('foobar'); + ->withConsecutive( + array($this->isRedisCommand('ASKING')), + array($this->isRedisCommand($command)) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + 'foobar' + ); /** @var Connection\FactoryInterface|MockObject */ $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); @@ -1228,17 +1232,16 @@ class RedisClusterTest extends PredisTestCase $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380'); $connection2 - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($this->isRedisCommand( - 'CLUSTER', array('SLOTS') - )) - ->willReturn($rspSlotsArray); - $connection2 - ->expects($this->at(3)) - ->method('executeCommand') - ->with($cmdGET) - ->willReturn('foobar'); + ->withConsecutive( + array($this->isRedisCommand('CLUSTER', array('SLOTS'))), + array($this->isRedisCommand($cmdGET)) + ) + ->willReturnOnConsecutiveCalls( + $rspSlotsArray, + 'foobar' + ); /** @var Connection\FactoryInterface|MockObject */ $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); diff --git a/tests/Predis/Connection/FactoryTest.php b/tests/Predis/Connection/FactoryTest.php index 91ea6ae1..95ae06ec 100644 --- a/tests/Predis/Connection/FactoryTest.php +++ b/tests/Predis/Connection/FactoryTest.php @@ -304,15 +304,14 @@ class FactoryTest extends PredisTestCase ->method('getParameters') ->willReturn($parameters); $connection - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('addConnectCommand') - ->with($this->isRedisCommand('AUTH', array('foobar'))); - $connection - ->expects($this->at(2)) - ->method('addConnectCommand') - ->with($this->isRedisCommand('SELECT', array(0))); + ->withConsecutive( + array($this->isRedisCommand('AUTH', array('foobar'))), + array($this->isRedisCommand('SELECT', array('0'))) + ); - $factory = new Factory(); + $factory = new Factory(); // TODO: using reflection to make a protected method accessible :facepalm: $reflection = new \ReflectionObject($factory); diff --git a/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php b/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php index 29970e6d..79aff931 100644 --- a/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php +++ b/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php @@ -1019,8 +1019,9 @@ class MasterSlaveReplicationTest extends PredisTestCase $slave1 ->expects($this->exactly(2)) ->method('executeCommand') - ->with( - $this->logicalOr($cmdEval, $cmdEvalSha) + ->withConsecutive( + array($this->isRedisCommand($cmdEval)), + array($this->isRedisCommand($cmdEvalSha)) ); $replication = new MasterSlaveReplication(); @@ -1063,8 +1064,10 @@ class MasterSlaveReplicationTest extends PredisTestCase $master ->expects($this->once()) ->method('executeCommand') - ->with($cmdInfo) - ->will($this->returnValue(' + ->with( + $this->isRedisCommand($cmdInfo) + ) + ->willReturn(' # Replication role:master connected_slaves:2 @@ -1076,7 +1079,7 @@ repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:12978 ' - )); + ); $replication = new MasterSlaveReplication(); $replication->setConnectionFactory($connFactory); @@ -1107,32 +1110,41 @@ repl_backlog_histlen:12978 /** @var Connection\FactoryInterface|MockObject */ $connFactory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); $connFactory - ->expects($this->at(0)) + ->expects($this->exactly(3)) ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6381', - 'role' => 'master', - )) - ->willReturn($master); - $connFactory - ->expects($this->at(1)) - ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6382', - 'role' => 'slave', - )) - ->willReturn($slave1); - $connFactory - ->expects($this->at(2)) - ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6383', - 'role' => 'slave', - )) - ->willReturn($slave2); + ->withConsecutive( + # Connection to master node + array( + array( + 'host' => '127.0.0.1', + 'port' => '6381', + 'role' => 'master', + ) + ), + + # Connection to first slave + array( + array( + 'host' => '127.0.0.1', + 'port' => '6382', + 'role' => 'slave', + ) + ), + + # Connection to second slave + array( + array( + 'host' => '127.0.0.1', + 'port' => '6383', + 'role' => 'slave', + ) + ) + ) + ->willReturnOnConsecutiveCalls( + $master, + $slave1, + $slave2 + ); $slave1 ->expects($this->once()) @@ -1206,37 +1218,48 @@ repl_backlog_histlen:12978 /** @var Connection\FactoryInterface|MockObject */ $connFactory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); $connFactory - ->expects($this->at(0)) + ->expects($this->exactly(3)) ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6381', - 'role' => 'master', - )) - ->willReturn($master); - $connFactory - ->expects($this->at(1)) - ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6382', - 'role' => 'slave', - )) - ->willReturn($slave1); - $connFactory - ->expects($this->at(2)) - ->method('create') - ->with(array( - 'host' => '127.0.0.1', - 'port' => '6383', - 'role' => 'slave', - )) - ->willReturn($slave2); + ->withConsecutive( + # Connection to master node + array( + array( + 'host' => '127.0.0.1', + 'port' => '6381', + 'role' => 'master', + ) + ), + + # Connection to first slave + array( + array( + 'host' => '127.0.0.1', + 'port' => '6382', + 'role' => 'slave', + ) + ), + + # Connection to second slave + array( + array( + 'host' => '127.0.0.1', + 'port' => '6383', + 'role' => 'slave', + ) + ) + ) + ->willReturnOnConsecutiveCalls( + $master, + $slave1, + $slave2 + ); $masterKO ->expects($this->once()) ->method('executeCommand') - ->with($cmdInfo) + ->with( + $this->isRedisCommand($cmdInfo) + ) ->willThrowException( new Connection\ConnectionException($masterKO) ); @@ -1268,8 +1291,10 @@ repl_backlog_histlen:0 $master ->expects($this->once()) ->method('executeCommand') - ->with($cmdInfo) - ->will($this->returnValue(' + ->with( + $this->isRedisCommand($cmdInfo) + ) + ->willReturn(' # Replication role:master connected_slaves:2 @@ -1281,7 +1306,7 @@ repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:12978 ' - )); + ); $replication = new MasterSlaveReplication(); $replication->setConnectionFactory($connFactory); diff --git a/tests/Predis/Connection/Replication/SentinelReplicationTest.php b/tests/Predis/Connection/Replication/SentinelReplicationTest.php index c1abcd46..68c77c3f 100644 --- a/tests/Predis/Connection/Replication/SentinelReplicationTest.php +++ b/tests/Predis/Connection/Replication/SentinelReplicationTest.php @@ -466,12 +466,12 @@ class SentinelReplicationTest extends PredisTestCase { $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel'); $sentinel1 - ->expects($this->at(0)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'SENTINEL', array('get-master-addr-by-name', 'svc') - )) - ->willReturn( + ->withConsecutive( + array($this->isRedisCommand('SENTINEL', array('get-master-addr-by-name', 'svc'))) + ) + ->willReturnOnConsecutiveCalls( array('127.0.0.1', '6381') ); @@ -511,12 +511,12 @@ class SentinelReplicationTest extends PredisTestCase { $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel'); $sentinel1 - ->expects($this->at(0)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'SENTINEL', array('slaves', 'svc') - )) - ->willReturn( + ->withConsecutive( + array($this->isRedisCommand('SENTINEL', array('slaves', 'svc'))) + ) + ->willReturnOnConsecutiveCalls( array( array( 'name', '127.0.0.1:6382', @@ -670,21 +670,14 @@ class SentinelReplicationTest extends PredisTestCase { $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel'); $sentinel1 - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->with($this->isRedisCommand( - 'SENTINEL', array('slaves', 'svc') - )) - ->willReturn( - array() - ); - $sentinel1 - ->expects($this->at(1)) - ->method('executeCommand') - ->with($this->isRedisCommand( - 'SENTINEL', array('get-master-addr-by-name', 'svc') - )) - ->willReturn( + ->withConsecutive( + array($this->isRedisCommand('SENTINEL', array('slaves', 'svc'))), + array($this->isRedisCommand('SENTINEL', array('get-master-addr-by-name', 'svc'))) + ) + ->willReturnOnConsecutiveCalls( + array(), array('127.0.0.1', '6381') ); @@ -912,12 +905,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturnOnConsecutiveCalls(false, true); $master - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand('ROLE')) - ->willReturn(array( - 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)), - )); + ->withConsecutive( + array($this->isRedisCommand('ROLE')) + ) + ->willReturnOnConsecutiveCalls( + array('master', 3129659, array(array('127.0.0.1', 6382, 3129242))) + ); $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave'); @@ -950,12 +945,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturnOnConsecutiveCalls(false, true); $slave1 - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand('ROLE')) - ->willReturn(array( - 'slave', '127.0.0.1', 9000, 'connected', 3167038, - )); + ->withConsecutive( + array($this->isRedisCommand('ROLE')) + ) + ->willReturnOnConsecutiveCalls( + array('slave', '127.0.0.1', 9000, 'connected', 3167038) + ); $replication = $this->getReplicationConnection('svc', array($sentinel1)); @@ -984,25 +981,29 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturnOnConsecutiveCalls(false, true); $master - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand('ROLE')) - ->willReturn(array( - 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)), - )); + ->withConsecutive( + array($this->isRedisCommand('ROLE')) + ) + ->willReturnOnConsecutiveCalls( + array('master', 3129659, array(array('127.0.0.1', 6382, 3129242))) + ); $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave'); $slave1 - ->expects($this->exactly(1)) + ->expects($this->once()) ->method('isConnected') ->willReturnOnConsecutiveCalls(false); $slave1 - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand('ROLE')) - ->willReturn(array( - 'slave', '127.0.0.1', 9000, 'connected', 3167038, - )); + ->withConsecutive( + array($this->isRedisCommand('ROLE')) + ) + ->willReturnOnConsecutiveCalls( + array('slave', '127.0.0.1', 9000, 'connected', 3167038) + ); $replication = $this->getReplicationConnection('svc', array($sentinel1)); @@ -1038,12 +1039,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(false); $master - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand('ROLE')) - ->willReturn(array( - 'slave', '127.0.0.1', 9000, 'connected', 3167038, - )); + ->withConsecutive( + array($this->isRedisCommand('ROLE')) + ) + ->willReturnOnConsecutiveCalls( + array('slave', '127.0.0.1', 9000, 'connected', 3167038) + ); $replication = $this->getReplicationConnection('svc', array($sentinel1)); @@ -1061,10 +1064,10 @@ class SentinelReplicationTest extends PredisTestCase $sentinel1 ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'SENTINEL', array('slaves', 'svc') - )) - ->willReturn( + ->withConsecutive( + array($this->isRedisCommand('SENTINEL', array('slaves', 'svc'))) + ) + ->willReturnOnConsecutiveCalls( array( array( 'name', '127.0.0.1:6382', @@ -1084,12 +1087,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(false); $master - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand('ROLE')) - ->willReturn(array( - 'master', '0', array(), - )); + ->withConsecutive( + array($this->isRedisCommand('ROLE')) + ) + ->willReturnOnConsecutiveCalls( + array('master', '0', array()) + ); $replication = $this->getReplicationConnection('svc', array($sentinel1)); @@ -1117,12 +1122,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $master - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'SET', array('key', $cmdGetResponse) - )) - ->willReturn($cmdSetResponse); + ->withConsecutive( + array($this->isRedisCommand('SET', array('key', $cmdGetResponse))) + ) + ->willReturnOnConsecutiveCalls( + $cmdSetResponse + ); $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave'); $slave1 @@ -1130,12 +1137,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $slave1 - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'GET', array('key') - )) - ->willReturn($cmdGetResponse); + ->withConsecutive( + array($this->isRedisCommand('GET', array('key'))) + ) + ->willReturnOnConsecutiveCalls( + $cmdGetResponse + ); $replication = $this->getReplicationConnection('svc', array($sentinel1)); @@ -1184,11 +1193,11 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $slave1 - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'GET', array('key') - )) + ->with( + $this->isRedisCommand('GET', array('key')) + ) ->willThrowException( new Connection\ConnectionException($slave1, 'Unknown connection error [127.0.0.1:6382]') ); @@ -1199,12 +1208,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $slave2 - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'GET', array('key') - )) - ->willReturn('value'); + ->withConsecutive( + array($this->isRedisCommand('GET', array('key'))) + ) + ->willReturnOnConsecutiveCalls( + 'value' + ); /** @var Connection\FactoryInterface|MockObject */ $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); @@ -1250,11 +1261,11 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $masterOld - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'DEL', array('key') - )) + ->with( + $this->isRedisCommand('DEL', array('key')) + ) ->willThrowException( new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]') ); @@ -1265,12 +1276,14 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $masterNew - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'DEL', array('key') - )) - ->willReturn(1); + ->withConsecutive( + array($this->isRedisCommand('DEL', array('key'))) + ) + ->willReturnOnConsecutiveCalls( + 1 + ); /** @var Connection\FactoryInterface|MockObject */ $factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock(); @@ -1316,11 +1329,11 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $masterOld - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'DEL', array('key') - )) + ->with( + $this->isRedisCommand('DEL', array('key')) + ) ->willThrowException( new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]') ); @@ -1359,11 +1372,11 @@ class SentinelReplicationTest extends PredisTestCase ->method('isConnected') ->willReturn(true); $master - ->expects($this->at(3)) + ->expects($this->once()) ->method('executeCommand') - ->with($this->isRedisCommand( - 'DEL', array('key') - )) + ->with( + $this->isRedisCommand('DEL', array('key')) + ) ->willThrowException( new Connection\ConnectionException($master, 'Unknown connection error [127.0.0.1:6381]') ); diff --git a/tests/Predis/Pipeline/AtomicTest.php b/tests/Predis/Pipeline/AtomicTest.php index 6fe3914c..2a09ea5f 100644 --- a/tests/Predis/Pipeline/AtomicTest.php +++ b/tests/Predis/Pipeline/AtomicTest.php @@ -32,14 +32,30 @@ class AtomicTest extends PredisTestCase $connection ->expects($this->exactly(2)) ->method('executeCommand') - ->willReturnOnConsecutiveCalls(true, array($pong, $pong, $pong)); + ->withConsecutive( + array($this->isRedisCommand('MULTI')), + array($this->isRedisCommand('EXEC')) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + array($pong, $pong, $pong) + ); $connection ->expects($this->exactly(3)) - ->method('writeRequest'); + ->method('writeRequest') + ->withConsecutive( + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')) + ); $connection - ->expects($this->at(3)) + ->expects($this->exactly(3)) ->method('readResponse') - ->willReturnOnConsecutiveCalls($queued, $queued, $queued); + ->willReturnOnConsecutiveCalls( + $queued, + $queued, + $queued + ); $pipeline = new Atomic(new Client($connection)); @@ -58,11 +74,36 @@ class AtomicTest extends PredisTestCase $this->expectException('Predis\ClientException'); $this->expectExceptionMessage('The underlying transaction has been aborted by the server'); + $queued = new Response\Status('QUEUED'); + $connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(); $connection ->expects($this->exactly(2)) ->method('executeCommand') - ->willReturnOnConsecutiveCalls(true, null); + ->withConsecutive( + array($this->isRedisCommand('MULTI')), + array($this->isRedisCommand('EXEC')) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + null + ); + $connection + ->expects($this->exactly(3)) + ->method('writeRequest') + ->withConsecutive( + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')) + ); + $connection + ->expects($this->exactly(3)) + ->method('readResponse') + ->willReturnOnConsecutiveCalls( + $queued, + $queued, + $queued + ); $pipeline = new Atomic(new Client($connection)); @@ -86,17 +127,32 @@ class AtomicTest extends PredisTestCase $connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(); $connection - ->expects($this->at(0)) + ->expects($this->exactly(2)) ->method('executeCommand') - ->willReturn(true); + ->withConsecutive( + array($this->isRedisCommand('MULTI')), + array($this->isRedisCommand('DISCARD')) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + new Response\Status('OK') + ); + $connection + ->expects($this->exactly(3)) + ->method('writeRequest') + ->withConsecutive( + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')) + ); $connection ->expects($this->exactly(3)) ->method('readResponse') - ->willReturnOnConsecutiveCalls($queued, $queued, $error); - $connection - ->expects($this->at(7)) - ->method('executeCommand') - ->with($this->isRedisCommand('DISCARD')); + ->willReturnOnConsecutiveCalls( + $queued, + $queued, + $error + ); $pipeline = new Atomic(new Client($connection)); @@ -115,13 +171,31 @@ class AtomicTest extends PredisTestCase $this->expectException('Predis\Response\ServerException'); $this->expectExceptionMessage('ERR Test error'); - $error = new Response\Error('ERR Test error'); - $connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(); + $connection + ->expects($this->exactly(2)) + ->method('executeCommand') + ->withConsecutive( + array($this->isRedisCommand('MULTI')), + array($this->isRedisCommand('DISCARD')) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + new Response\Status('OK') + ); + $connection + ->expects($this->exactly(2)) + ->method('writeRequest') + ->withConsecutive( + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')) + ); $connection ->expects($this->once()) ->method('readResponse') - ->willReturn($error); + ->willReturn( + new Response\Error('ERR Test error') + ); $pipeline = new Atomic(new Client($connection)); @@ -141,14 +215,33 @@ class AtomicTest extends PredisTestCase $error = new Response\Error('ERR Test error'); $connection = $this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(); + $connection + ->expects($this->exactly(2)) + ->method('executeCommand') + ->withConsecutive( + array($this->isRedisCommand('MULTI')), + array($this->isRedisCommand('EXEC')) + ) + ->willReturnOnConsecutiveCalls( + new Response\Status('OK'), + array($pong, $pong, $error) + ); + $connection + ->expects($this->exactly(3)) + ->method('writeRequest') + ->withConsecutive( + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')), + array($this->isRedisCommand('PING')) + ); $connection ->expects($this->exactly(3)) ->method('readResponse') - ->willReturnOnConsecutiveCalls($queued, $queued, $queued); - $connection - ->expects($this->at(7)) - ->method('executeCommand') - ->willReturn(array($pong, $pong, $error)); + ->willReturnOnConsecutiveCalls( + $queued, + $queued, + $queued + ); $pipeline = new Atomic(new Client($connection, array('exceptions' => false))); diff --git a/tests/Predis/Protocol/Text/Handler/MultiBulkResponseTest.php b/tests/Predis/Protocol/Text/Handler/MultiBulkResponseTest.php index dad4ecfe..22f05a1e 100644 --- a/tests/Predis/Protocol/Text/Handler/MultiBulkResponseTest.php +++ b/tests/Predis/Protocol/Text/Handler/MultiBulkResponseTest.php @@ -27,23 +27,23 @@ class MultiBulkResponseTest extends PredisTestCase $connection ->expects($this->once()) ->method('getProtocol') - ->willReturn(new CompositeProtocolProcessor()); + ->willReturn( + new CompositeProtocolProcessor() + ); $connection - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('readLine') - ->willReturn('$3'); + ->willReturnOnConsecutiveCalls( + '$3', + '$3' + ); $connection - ->expects($this->at(2)) + ->expects($this->exactly(2)) ->method('readBuffer') - ->willReturn("foo\r\n"); - $connection - ->expects($this->at(3)) - ->method('readLine') - ->willReturn('$3'); - $connection - ->expects($this->at(4)) - ->method('readBuffer') - ->willReturn("bar\r\n"); + ->willReturnOnConsecutiveCalls( + "foo\r\n", + "bar\r\n" + ); $handler = new Handler\MultiBulkResponse(); diff --git a/tests/Predis/Protocol/Text/ProtocolProcessorTest.php b/tests/Predis/Protocol/Text/ProtocolProcessorTest.php index bb77eb78..49941759 100644 --- a/tests/Predis/Protocol/Text/ProtocolProcessorTest.php +++ b/tests/Predis/Protocol/Text/ProtocolProcessorTest.php @@ -57,25 +57,15 @@ class ProtocolProcessorTest extends PredisTestCase $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface'); $connection - ->expects($this->at(0)) + ->expects($this->exactly(5)) ->method('readLine') - ->willReturn('+OK'); - $connection - ->expects($this->at(1)) - ->method('readLine') - ->willReturn('-ERR error message'); - $connection - ->expects($this->at(2)) - ->method('readLine') - ->willReturn(':2'); - $connection - ->expects($this->at(3)) - ->method('readLine') - ->willReturn('$-1'); - $connection - ->expects($this->at(4)) - ->method('readLine') - ->willReturn('*-1'); + ->willReturnOnConsecutiveCalls( + '+OK', + '-ERR error message', + ':2', + '$-1', + '*-1' + ); $this->assertEquals('OK', $protocol->read($connection)); $this->assertEquals('ERR error message', $protocol->read($connection)); diff --git a/tests/Predis/Protocol/Text/ResponseReaderTest.php b/tests/Predis/Protocol/Text/ResponseReaderTest.php index caec84d8..a74741bf 100644 --- a/tests/Predis/Protocol/Text/ResponseReaderTest.php +++ b/tests/Predis/Protocol/Text/ResponseReaderTest.php @@ -56,25 +56,15 @@ class ResponseReaderTest extends PredisTestCase { $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface'); $connection - ->expects($this->at(0)) + ->expects($this->exactly(5)) ->method('readLine') - ->willReturn('+OK'); - $connection - ->expects($this->at(1)) - ->method('readLine') - ->willReturn('-ERR error message'); - $connection - ->expects($this->at(2)) - ->method('readLine') - ->willReturn(':2'); - $connection - ->expects($this->at(3)) - ->method('readLine') - ->willReturn('$-1'); - $connection - ->expects($this->at(4)) - ->method('readLine') - ->willReturn('*-1'); + ->willReturnOnConsecutiveCalls( + '+OK', + '-ERR error message', + ':2', + '$-1', + '*-1' + ); $reader = new ResponseReader();