123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace Predis\Configuration;
- use stdClass;
- use PredisTestCase;
- class OptionsTest extends PredisTestCase
- {
-
- public function testConstructorWithoutArguments()
- {
- $options = new Options();
- $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $options->connections);
- $this->assertInstanceOf('Predis\Profile\ProfileInterface', $options->profile);
- $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $options->cluster);
- $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $options->replication);
- $this->assertTrue($options->exceptions);
- $this->assertNull($options->prefix);
- }
-
- public function testConstructorWithArrayArgument()
- {
- $options = new Options(array(
- 'exceptions' => false,
- 'profile' => '2.0',
- 'prefix' => 'prefix:',
- 'connections' => $this->getMock('Predis\Connection\ConnectionFactoryInterface'),
- 'cluster' => $this->getMock('Predis\Connection\ClusterConnectionInterface'),
- 'replication' => $this->getMock('Predis\Connection\ReplicationConnectionInterface'),
- ));
- $this->assertInternalType('bool', $options->exceptions);
- $this->assertInstanceOf('Predis\Profile\ProfileInterface', $options->profile);
- $this->assertInstanceOf('Predis\Command\Processor\CommandProcessorInterface', $options->prefix);
- $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $options->connections);
- $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $options->cluster);
- $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $options->replication);
- }
-
- public function testSupportsCustomOptions()
- {
- $options = new Options(array(
- 'custom' => 'foobar',
- ));
- $this->assertSame('foobar', $options->custom);
- }
-
- public function testUndefinedOptionsReturnNull()
- {
- $options = new Options();
- $this->assertFalse($options->defined('unknown'));
- $this->assertFalse(isset($options->unknown));
- $this->assertNull($options->unknown);
- }
-
- public function testCanCheckOptionsIfDefinedByUser()
- {
- $options = new Options(array(
- 'prefix' => 'prefix:',
- 'custom' => 'foobar',
- 'void' => null,
- ));
- $this->assertTrue($options->defined('prefix'));
- $this->assertTrue($options->defined('custom'));
- $this->assertTrue($options->defined('void'));
- $this->assertFalse($options->defined('profile'));
- }
-
- public function testIsSetReplicatesPHPBehavior()
- {
- $options = new Options(array(
- 'prefix' => 'prefix:',
- 'custom' => 'foobar',
- 'void' => null,
- ));
- $this->assertTrue(isset($options->prefix));
- $this->assertTrue(isset($options->custom));
- $this->assertFalse(isset($options->void));
- $this->assertFalse(isset($options->profile));
- }
-
- public function testReturnsDefaultValueOfSpecifiedOption()
- {
- $options = new Options();
- $this->assertInstanceOf('Predis\Profile\ProfileInterface', $options->getDefault('profile'));
- }
-
- public function testReturnsNullAsDefaultValueForUndefinedOption()
- {
- $options = new Options();
- $this->assertNull($options->getDefault('unknown'));
- }
-
- public function testLazilyInitializesOptionValueUsingObjectWithInvokeMagicMethod()
- {
- $profile = $this->getMock('Predis\Profile\ProfileInterface');
-
- $callable = $this->getMock('stdClass', array('__invoke'));
- $callable->expects($this->once())
- ->method('__invoke')
- ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), 'profile')
- ->will($this->returnValue($profile));
- $options = new Options(array(
- 'profile' => $callable,
- ));
- $this->assertSame($profile, $options->profile);
- $this->assertSame($profile, $options->profile);
- }
-
- public function testLazilyInitializesCustomOptionValueUsingObjectWithInvokeMagicMethod()
- {
- $custom = new stdClass;
-
- $callable = $this->getMock('stdClass', array('__invoke'));
- $callable->expects($this->once())
- ->method('__invoke')
- ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), 'custom')
- ->will($this->returnValue($custom));
- $options = new Options(array(
- 'custom' => $callable,
- ));
- $this->assertSame($custom, $options->custom);
- $this->assertSame($custom, $options->custom);
- }
- }
|