From 4363345f7c2bb232558d80788264ed035d7867e9 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Mon, 25 Mar 2019 19:31:56 +0300 Subject: [PATCH] iterate not iterable connections #552 --- src/Client.php | 4 +++- tests/Predis/ClientTest.php | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index ee272d9c..16f61c49 100644 --- a/src/Client.php +++ b/src/Client.php @@ -535,7 +535,9 @@ class Client implements ClientInterface, \IteratorAggregate $connection = $this->getConnection(); if (!$connection instanceof \Traversable) { - throw new ClientException('The underlying connection is not traversable'); + return new \ArrayIterator(array( + (string) $connection => new static($connection, $this->getOptions()) + )); } foreach ($connection as $node) { diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 93ef909f..865a8657 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -853,15 +853,17 @@ class ClientTest extends PredisTestCase /** * @group disconnected - * @expectedException \Predis\ClientException - * @expectedExceptionMessage The underlying connection is not traversable */ - public function testGetIteratorWithNonTraversableConnectionThrowsException() + public function testGetIteratorWithNonTraversableConnectionNoException() { - $connection = $this->getMock('Predis\Connection\NodeConnectionInterface'); + $connection = $this->getMockConnection('tcp://127.0.0.1:6381'); $client = new Client($connection); - $client->getIterator(); + $iterator = $client->getIterator(); + + $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current()); + $this->assertSame($connection, $nodeClient->getConnection()); + $this->assertSame('127.0.0.1:6381', $iterator->key()); } // ******************************************************************** //