mirror of
https://github.com/predis/predis.git
synced 2026-10-09 21:38:07 +00:00
[tests] Adapt after changes in aggregate connection initialization.
See commit dd5d665 for the above mentioned changes.
Tests for Option\Aggregate, Option\Replication and Option\Cluster should
be refactored because code is a bit too repetitive and a bunch of tests
are shared among them (replication and cluster options extend aggregate
and share the same logic after all).
This commit is contained in:
@@ -30,8 +30,8 @@ class ReplicationTest extends PredisTestCase
|
||||
/** @var OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
|
||||
$this->assertInstanceOf('Closure', $initializer = $option->getDefault($options));
|
||||
$this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $initializer($options));
|
||||
$this->assertInstanceOf('closure', $initializer = $option->getDefault($options));
|
||||
$this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $initializer($parameters = []));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,8 +57,8 @@ class ReplicationTest extends PredisTestCase
|
||||
$connectionFactory
|
||||
);
|
||||
|
||||
$this->assertInstanceOf('Closure', $initializer = $option->getDefault($options));
|
||||
$this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $connection = $initializer($options));
|
||||
$this->assertInstanceOf('closure', $initializer = $option->getDefault($options));
|
||||
$this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $connection = $initializer([]));
|
||||
|
||||
// TODO: I know, I know...
|
||||
$reflection = new \ReflectionProperty($connection, 'autoDiscovery');
|
||||
@@ -73,6 +73,7 @@ class ReplicationTest extends PredisTestCase
|
||||
public function testAcceptsCallableAsConnectionInitializer(): void
|
||||
{
|
||||
$option = new Replication();
|
||||
$parameters = ['127.0.0.1:6379', '127.0.0.1:6380'];
|
||||
|
||||
/** @var OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
@@ -84,11 +85,179 @@ class ReplicationTest extends PredisTestCase
|
||||
$callable
|
||||
->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
|
||||
->with($parameters, $options, $option)
|
||||
->willReturn($connection);
|
||||
|
||||
$this->assertInstanceOf('Closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertSame($connection, $initializer($parameters = array()));
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertSame($connection, $initializer($parameters));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testReturnedCallableWrapperTriggersAggregationByDefault(): void
|
||||
{
|
||||
$option = new Replication();
|
||||
$parameters = ['127.0.0.1:6379', '127.0.0.1:6380'];
|
||||
|
||||
$factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock();
|
||||
$factory
|
||||
->expects($this->exactly(2))
|
||||
->method('create')
|
||||
->withConsecutive(
|
||||
[$parameters[0]],
|
||||
[$parameters[1]]
|
||||
)
|
||||
->willReturnOnConsecutiveCalls(
|
||||
$nodeConnection1 = $this->getMockConnection($parameters[0]),
|
||||
$nodeConnection2 = $this->getMockConnection($parameters[1])
|
||||
);
|
||||
|
||||
/** @var MockObject|OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
$options
|
||||
->expects($this->once())
|
||||
->method('__get')
|
||||
->with('connections')
|
||||
->willReturn($factory);
|
||||
|
||||
$connection = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')->getMock();
|
||||
$connection
|
||||
->expects($this->exactly(2))
|
||||
->method('add')
|
||||
->withConsecutive(
|
||||
[$nodeConnection1],
|
||||
[$nodeConnection2]
|
||||
);
|
||||
|
||||
$callable = $this->getMockBuilder('stdClass')
|
||||
->addMethods(array('__invoke'))
|
||||
->getMock();
|
||||
$callable
|
||||
->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($parameters, $options, $option)
|
||||
->willReturn($connection);
|
||||
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertSame($connection, $initializer($parameters, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testReturnedCallableWrapperDoesNotTriggerAggregationWhenSecondArgumentIsFalse(): void
|
||||
{
|
||||
$option = new Replication();
|
||||
$parameters = ['127.0.0.1:6379', '127.0.0.1:6380'];
|
||||
|
||||
/** @var MockObject|OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
$options
|
||||
->expects($this->never())
|
||||
->method('__get')
|
||||
->with('connections');
|
||||
|
||||
$connection = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')->getMock();
|
||||
$connection
|
||||
->expects($this->never())
|
||||
->method('add');
|
||||
|
||||
$callable = $this->getMockBuilder('stdClass')
|
||||
->addMethods(array('__invoke'))
|
||||
->getMock();
|
||||
$callable
|
||||
->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($parameters, $options, $option)
|
||||
->willReturn($connection);
|
||||
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertSame($connection, $initializer($parameters, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testReturnedCallableWrapperTriggersAggregationWhenSecondArgumentIsTrue(): void
|
||||
{
|
||||
$option = new Replication();
|
||||
$parameters = ['127.0.0.1:6379', '127.0.0.1:6380'];
|
||||
|
||||
$factory = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock();
|
||||
$factory
|
||||
->expects($this->exactly(2))
|
||||
->method('create')
|
||||
->withConsecutive(
|
||||
[$parameters[0]],
|
||||
[$parameters[1]]
|
||||
)
|
||||
->willReturnOnConsecutiveCalls(
|
||||
$nodeConnection1 = $this->getMockConnection($parameters[0]),
|
||||
$nodeConnection2 = $this->getMockConnection($parameters[1])
|
||||
);
|
||||
|
||||
/** @var MockObject|OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
$options
|
||||
->expects($this->once())
|
||||
->method('__get')
|
||||
->with('connections')
|
||||
->willReturn($factory);
|
||||
|
||||
$connection = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')->getMock();
|
||||
$connection
|
||||
->expects($this->exactly(2))
|
||||
->method('add')
|
||||
->withConsecutive(
|
||||
[$nodeConnection1],
|
||||
[$nodeConnection2]
|
||||
);
|
||||
|
||||
$callable = $this->getMockBuilder('stdClass')
|
||||
->addMethods(array('__invoke'))
|
||||
->getMock();
|
||||
$callable
|
||||
->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($parameters, $options, $option)
|
||||
->willReturn($connection);
|
||||
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertSame($connection, $initializer($parameters, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testReturnedCallableWrapperDoesNotTriggerAggregationWhenFirstArgumentIsEmptyAndSecondArgumentIsTrue(): void
|
||||
{
|
||||
$option = new Replication();
|
||||
$parameters = [];
|
||||
|
||||
/** @var MockObject|OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
$options
|
||||
->expects($this->never())
|
||||
->method('__get')
|
||||
->with('connections');
|
||||
|
||||
$connection = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')->getMock();
|
||||
$connection
|
||||
->expects($this->never())
|
||||
->method('add');
|
||||
|
||||
$callable = $this->getMockBuilder('stdClass')
|
||||
->addMethods(array('__invoke'))
|
||||
->getMock();
|
||||
$callable
|
||||
->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($parameters, $options, $option)
|
||||
->willReturn($connection);
|
||||
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertSame($connection, $initializer($parameters, true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,9 +266,12 @@ class ReplicationTest extends PredisTestCase
|
||||
public function testThrowsExceptionOnInvalidReturnTypeOfConnectionInitializer(): void
|
||||
{
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage('Predis\Configuration\Option\Replication expects a valid connection type returned by callable initializer');
|
||||
$this->expectExceptionMessageMatches(
|
||||
'/^Predis\\\Configuration\\\Option\\\Replication expects the supplied callable to return an instance of Predis\\\Connection\\\AggregateConnectionInterface, but .* was returned$/'
|
||||
);
|
||||
|
||||
$option = new Replication();
|
||||
$parameters = ['127.0.0.1:6379', '127.0.0.1:6380'];
|
||||
|
||||
/** @var OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
@@ -111,12 +283,12 @@ class ReplicationTest extends PredisTestCase
|
||||
$callable
|
||||
->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
|
||||
->with($parameters, $options, $option)
|
||||
->willReturn($connection);
|
||||
|
||||
$this->assertInstanceOf('Closure', $initializer = $option->filter($options, $callable));
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, $callable));
|
||||
|
||||
$initializer($parameters = array());
|
||||
$initializer($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,14 +301,14 @@ class ReplicationTest extends PredisTestCase
|
||||
/** @var OptionsInterface */
|
||||
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
|
||||
|
||||
$this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'predis'));
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, 'predis'));
|
||||
$this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $initializer($parameters = array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testAcceptsShortNameStringRedis(): void
|
||||
public function testAcceptsShortNameStringSentinel(): void
|
||||
{
|
||||
$option = new Replication();
|
||||
|
||||
@@ -158,7 +330,7 @@ class ReplicationTest extends PredisTestCase
|
||||
$this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock(),
|
||||
);
|
||||
|
||||
$this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'sentinel'));
|
||||
$this->assertInstanceOf('closure', $initializer = $option->filter($options, 'sentinel'));
|
||||
$this->assertInstanceOf('Predis\Connection\Replication\SentinelReplication', $connection = $initializer($parameters));
|
||||
|
||||
$this->assertSame($parameters[0], $connection->getSentinelConnection());
|
||||
@@ -170,7 +342,9 @@ class ReplicationTest extends PredisTestCase
|
||||
public function testThrowsExceptionOnInvalidShortNameString(): void
|
||||
{
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage('String value for the replication option must be either `predis` or `sentinel`');
|
||||
$this->expectExceptionMessage(
|
||||
'Predis\Configuration\Option\Replication expects either `predis`, `sentinel` or `redis-sentinel` as valid string values, `unknown` given'
|
||||
);
|
||||
|
||||
$option = new Replication();
|
||||
|
||||
@@ -186,7 +360,9 @@ class ReplicationTest extends PredisTestCase
|
||||
public function testThrowsExceptionOnBooleanValue(): void
|
||||
{
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage('Predis\Configuration\Option\Replication expects a valid callable');
|
||||
$this->expectExceptionMessage(
|
||||
'Predis\Configuration\Option\Replication expects either a string or a callable value, boolean given'
|
||||
);
|
||||
|
||||
$option = new Replication();
|
||||
|
||||
@@ -202,7 +378,9 @@ class ReplicationTest extends PredisTestCase
|
||||
public function testThrowsExceptionOnInstanceOfReplicationInterface(): void
|
||||
{
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage('Predis\Configuration\Option\Replication expects a valid callable');
|
||||
$this->expectExceptionMessageMatches(
|
||||
'/Predis\\\Configuration\\\Option\\\Replication expects either a string or a callable value, .* given/'
|
||||
);
|
||||
|
||||
$option = new Replication();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user