Replace "getClientFor()" with "on()" in Client.

This new method acts exactly like "getClientFor()" by returning a new
client instance for the specified node unless a callback is passed as
the second argument, in this case the callback is invoked and the new
client instance is passed to it. The value returned by the callback
is used as the return value of the "on()" method.
This commit is contained in:
Daniele Alessandri
2016-06-07 18:58:21 +02:00
parent 7c4c4ae58a
commit ee7104d2e5
4 changed files with 81 additions and 15 deletions
+52 -4
View File
@@ -659,7 +659,7 @@ class ClientTest extends PredisTestCase
/**
* @group disconnected
*/
public function testCreateClientWithConnectionFromAggregateConnection()
public function testOnMethodCreatesClientWithConnectionFromAggregateConnection()
{
$client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:', 'cluster' => 'predis'));
@@ -667,7 +667,7 @@ class ClientTest extends PredisTestCase
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $node01 = $client->getConnectionById('node01'));
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $node02 = $client->getConnectionById('node02'));
$clientNode02 = $client->getClientFor('node02');
$clientNode02 = $client->on('node02');
$this->assertInstanceOf('Predis\Client', $clientNode02);
$this->assertSame($node02, $clientNode02->getConnection());
@@ -677,12 +677,60 @@ class ClientTest extends PredisTestCase
/**
* @group disconnected
*/
public function testGetClientForReturnsInstanceOfSubclass()
public function testOnMethodReturnsInstanceOfSubclass()
{
$nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
$client = $this->getMock('Predis\Client', array('dummy'), array($nodes, array('cluster' => 'predis')), 'SubclassedClient');
$this->assertInstanceOf('SubclassedClient', $client->getClientFor('node02'));
$this->assertInstanceOf('SubclassedClient', $client->on('node02'));
}
/**
* @group disconnected
*/
public function testOnMethodInvokesCallableInSecondArgumentAndReturnsItsReturnValue()
{
$test = $this;
$client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('cluster' => 'predis'));
$callable = $this->getMock('stdClass', array('__invoke'));
$callable->expects($this->once())
->method('__invoke')
->with($this->callback(function ($clientNode) use ($test, $client) {
$test->isInstanceOf('Predis\ClientInterface', $clientNode);
$test->assertNotSame($client, $clientNode);
$test->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection = $clientNode->getConnection());
$test->assertSame('node02', $connection->getParameters()->alias);
return true;
}))
->will($this->returnValue('value'));
$this->assertSame('value', $client->on('node02', $callable));
}
/**
* @group disconnected
* @expectedException \Predis\NotSupportedException
* @expectedExceptionMessage Retrieving connections by ID is supported only by aggregate connections
*/
public function testOnMethodThrowsExceptionWithNodeConnection()
{
$client = new Client('tcp://127.0.0.1?alias=node01');
$client->on('node01');
}
/**
* @group disconnected
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid connection ID: `nodeXX`
*/
public function testOnMethodThrowsExceptionWithUnknownConnectionID()
{
$client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('cluster' => 'predis'));
$client->on('nodeXX');
}
/**