123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /*
- * This file is part of the Predis package.
- *
- * (c) Daniele Alessandri <suppakilla@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Predis\Connection;
- use PredisTestCase;
- /**
- * @todo Parameters::define();
- * @todo Parameters::undefine();
- */
- class ParametersTest extends PredisTestCase
- {
- /**
- * @group disconnected
- */
- public function testDefaultValues()
- {
- $defaults = $this->getDefaultParametersArray();
- $parameters = new Parameters();
- $this->assertEquals($defaults['scheme'], $parameters->scheme);
- $this->assertEquals($defaults['host'], $parameters->host);
- $this->assertEquals($defaults['port'], $parameters->port);
- }
- /**
- * @group disconnected
- */
- public function testIsSet()
- {
- $parameters = new Parameters();
- $this->assertTrue(isset($parameters->scheme));
- $this->assertFalse(isset($parameters->unknown));
- }
- public function sharedTestsWithArrayParameters(Parameters $parameters)
- {
- $this->assertTrue(isset($parameters->scheme));
- $this->assertSame('tcp', $parameters->scheme);
- $this->assertTrue(isset($parameters->port));
- $this->assertSame(7000, $parameters->port);
- $this->assertTrue(isset($parameters->custom));
- $this->assertSame('foobar', $parameters->custom);
- $this->assertFalse(isset($parameters->unknown));
- $this->assertNull($parameters->unknown);
- }
- /**
- * @group disconnected
- */
- public function testConstructWithArrayParameters()
- {
- $parameters = new Parameters(array(
- 'port' => 7000,
- 'custom' => 'foobar',
- ));
- $this->sharedTestsWithArrayParameters($parameters);
- }
- /**
- * @group disconnected
- */
- public function testCreateWithArrayParameters()
- {
- $parameters = new Parameters(array(
- 'port' => 7000,
- 'custom' => 'foobar',
- ));
- $this->sharedTestsWithArrayParameters($parameters);
- }
- /**
- * @group disconnected
- */
- public function testCreateWithUriString()
- {
- $overrides = array(
- 'port' => 7000,
- 'database' => 5,
- 'custom' => 'foobar',
- );
- $uriString = $this->getParametersString($overrides);
- $parameters = Parameters::create($uriString);
- $this->sharedTestsWithArrayParameters($parameters);
- $this->assertEquals($overrides['database'], $parameters->database);
- }
- /**
- * @group disconnected
- */
- public function testToArray()
- {
- $additional = array('port' => 7000, 'custom' => 'foobar');
- $parameters = new Parameters($additional);
- $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
- }
- /**
- * @group disconnected
- */
- public function testSerialization()
- {
- $parameters = new Parameters(array('port' => 7000, 'custom' => 'foobar'));
- $unserialized = unserialize(serialize($parameters));
- $this->assertEquals($parameters->scheme, $unserialized->scheme);
- $this->assertEquals($parameters->port, $unserialized->port);
- $this->assertTrue(isset($unserialized->custom));
- $this->assertEquals($parameters->custom, $unserialized->custom);
- $this->assertFalse(isset($unserialized->unknown));
- $this->assertNull($unserialized->unknown);
- }
- /**
- * @group disconnected
- */
- public function testParsingURI()
- {
- $uri = 'tcp://10.10.10.10:6400?timeout=0.5&persistent=1&database=5&password=secret';
- $expected = array(
- 'scheme' => 'tcp',
- 'host' => '10.10.10.10',
- 'port' => 6400,
- 'timeout' => '0.5',
- 'persistent' => '1',
- 'database' => '5',
- 'password' => 'secret',
- );
- $this->assertSame($expected, Parameters::parse($uri));
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithRedisScheme()
- {
- $uri = 'redis://:secret@10.10.10.10:6400/5?timeout=0.5&persistent=1';
- $expected = array(
- 'scheme' => 'redis',
- 'host' => '10.10.10.10',
- 'port' => 6400,
- 'timeout' => '0.5',
- 'persistent' => '1',
- 'password' => 'secret',
- 'database' => '5',
- );
- $parameters = Parameters::parse($uri);
- // TODO: parse_url() in PHP >= 5.6 returns an empty "user" entry in the
- // dictionary when no username has been provided in the URI string. This
- // actually makes sense, but let's keep the test ugly & simple for now.
- unset($parameters['user']);
- $this->assertSame($expected, $parameters);
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithRedisSchemeMustPreserveRemainderOfPath()
- {
- $uri = 'redis://10.10.10.10/5/rest/of/path';
- $expected = array(
- 'scheme' => 'redis',
- 'host' => '10.10.10.10',
- 'path' => '/rest/of/path',
- 'database' => '5',
- );
- $parameters = Parameters::parse($uri);
- $this->assertSame($expected, $parameters);
- }
- /**
- * @group disconnected
- */
- public function testRedisSchemeOverridesPasswordAndDatabaseInQueryString()
- {
- $parameters = Parameters::parse('redis://:secret@10.10.10.10/5?password=ignored&database=4');
- $this->assertSame('secret', $parameters['password']);
- $this->assertSame('5', $parameters['database']);
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithUnixDomainSocket()
- {
- $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
- $expected = array(
- 'scheme' => 'unix',
- 'path' => '/tmp/redis.sock',
- 'timeout' => '0.5',
- 'persistent' => '1',
- );
- $this->assertSame($expected, Parameters::parse($uri));
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithUnixDomainSocketOldWay()
- {
- $uri = 'unix:/tmp/redis.sock?timeout=0.5&persistent=1';
- $expected = array(
- 'scheme' => 'unix',
- 'path' => '/tmp/redis.sock',
- 'timeout' => '0.5',
- 'persistent' => '1',
- );
- $this->assertSame($expected, Parameters::parse($uri));
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithIncompletePairInQueryString()
- {
- $uri = 'tcp://10.10.10.10?persistent=1&foo=&bar';
- $expected = array(
- 'scheme' => 'tcp',
- 'host' => '10.10.10.10',
- 'persistent' => '1',
- 'foo' => '',
- 'bar' => '',
- );
- $this->assertSame($expected, Parameters::parse($uri));
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithMoreThanOneEqualSignInQueryStringPairValue()
- {
- $uri = 'tcp://10.10.10.10?foobar=a=b=c&persistent=1';
- $expected = array(
- 'scheme' => 'tcp',
- 'host' => '10.10.10.10',
- 'foobar' => 'a=b=c',
- 'persistent' => '1',
- );
- $this->assertSame($expected, Parameters::parse($uri));
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWhenQueryStringHasBracketsInFieldnames()
- {
- $uri = 'tcp://10.10.10.10?persistent=1&metavars[]=foo&metavars[]=hoge';
- $expected = array(
- 'scheme' => 'tcp',
- 'host' => '10.10.10.10',
- 'persistent' => '1',
- 'metavars' => array('foo', 'hoge'),
- );
- $this->assertSame($expected, Parameters::parse($uri));
- }
- /**
- * @group disconnected
- */
- public function testParsingURIWithEmbeddedIPV6AddressShouldStripBracketsFromHost()
- {
- $expected = array('scheme' => 'tcp', 'host' => '::1', 'port' => 7000);
- $this->assertSame($expected, Parameters::parse('tcp://[::1]:7000'));
- $expected = array('scheme' => 'tcp', 'host' => '2001:db8:0:f101::1', 'port' => 7000);
- $this->assertSame($expected, Parameters::parse('tcp://[2001:db8:0:f101::1]:7000'));
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid parameters URI: tcp://invalid:uri
- */
- public function testParsingURIThrowOnInvalidURI()
- {
- Parameters::parse('tcp://invalid:uri');
- }
- /**
- * @group disconnected
- */
- public function testToStringWithDefaultParameters()
- {
- $parameters = new Parameters();
- $this->assertSame('tcp://127.0.0.1:6379', (string) $parameters);
- }
- /**
- * @group disconnected
- */
- public function testToStringWithUnixScheme()
- {
- $uri = 'unix:/path/to/redis.sock';
- $parameters = Parameters::create("$uri?foo=bar");
- $this->assertSame($uri, (string) $parameters);
- }
- /**
- * @group disconnected
- */
- public function testToStringWithIPv4()
- {
- $uri = 'tcp://127.0.0.1:6379';
- $parameters = Parameters::create("$uri?foo=bar");
- $this->assertSame($uri, (string) $parameters);
- }
- /**
- * @group disconnected
- */
- public function testToStringWithIPv6()
- {
- $uri = 'tcp://[::1]:6379';
- $parameters = Parameters::create("$uri?foo=bar");
- $this->assertSame($uri, (string) $parameters);
- }
- // ******************************************************************** //
- // ---- HELPER METHODS ------------------------------------------------ //
- // ******************************************************************** //
- /**
- * Returns a named array with the default connection parameters and their values.
- *
- * @return array Default connection parameters.
- */
- protected function getDefaultParametersArray()
- {
- return array(
- 'scheme' => 'tcp',
- 'host' => '127.0.0.1',
- 'port' => 6379,
- );
- }
- /**
- * Returns an URI string representation of the specified connection parameters.
- *
- * @param array $parameters array of connection parameters.
- *
- * @return string URI string.
- */
- protected function getParametersString(array $parameters)
- {
- $defaults = $this->getDefaultParametersArray();
- $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
- $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
- $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
- unset($parameters['scheme'], $parameters['host'], $parameters['port']);
- $uriString = "$scheme://$host:$port/?";
- foreach ($parameters as $k => $v) {
- $uriString .= "$k=$v&";
- }
- return $uriString;
- }
- }
|