From f5823406b4e4cc3d98acd20a47682300af03e5e2 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 9 Aug 2017 19:32:22 -0400 Subject: [PATCH] Move ConnectionManager tests with the other tests for that class Remove the duplicate stub code and move the tests for ConnectionManager code with the rest of that class' tests. --- tests/TestCase/Core/StaticConfigTraitTest.php | 183 ------------------ .../Datasource/ConnectionManagerTest.php | 136 +++++++++++++ 2 files changed, 136 insertions(+), 183 deletions(-) diff --git a/tests/TestCase/Core/StaticConfigTraitTest.php b/tests/TestCase/Core/StaticConfigTraitTest.php index 1e3abbe3825..33249635f2e 100644 --- a/tests/TestCase/Core/StaticConfigTraitTest.php +++ b/tests/TestCase/Core/StaticConfigTraitTest.php @@ -16,53 +16,6 @@ use Cake\Core\StaticConfigTrait; use Cake\TestSuite\TestCase; -/** - * TestConnectionManagerStaticConfig - */ -class TestConnectionManagerStaticConfig -{ - - use StaticConfigTrait { - parseDsn as protected _parseDsn; - } - - /** - * Parse a DSN - * - * @param string $config The config to parse. - * @return array - */ - public static function parseDsn($config = null) - { - $config = static::_parseDsn($config); - - if (isset($config['path']) && empty($config['database'])) { - $config['database'] = substr($config['path'], 1); - } - - if (empty($config['driver'])) { - $config['driver'] = $config['className']; - $config['className'] = 'Cake\Database\Connection'; - } - - unset($config['path']); - - return $config; - } - - /** - * Database driver class map. - * - * @var array - */ - protected static $_dsnClassMap = [ - 'mysql' => 'Cake\Database\Driver\Mysql', - 'postgres' => 'Cake\Database\Driver\Postgres', - 'sqlite' => 'Cake\Database\Driver\Sqlite', - 'sqlserver' => 'Cake\Database\Driver\Sqlserver', - ]; -} - /** * TestCacheStaticConfig */ @@ -178,142 +131,6 @@ public function testParseBadType() $className::parseDsn(['url' => 'http://:80']); } - /** - * Tests parsing different DSNs - * - * @return void - */ - public function testCustomParseDsn() - { - $dsn = 'mysql://localhost:3306/database'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'driver' => 'Cake\Database\Driver\Mysql', - 'host' => 'localhost', - 'database' => 'database', - 'port' => 3306, - 'scheme' => 'mysql', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'mysql://user:password@localhost:3306/database'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'driver' => 'Cake\Database\Driver\Mysql', - 'host' => 'localhost', - 'password' => 'password', - 'database' => 'database', - 'port' => 3306, - 'scheme' => 'mysql', - 'username' => 'user', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'sqlite:///:memory:'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'driver' => 'Cake\Database\Driver\Sqlite', - 'database' => ':memory:', - 'scheme' => 'sqlite', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'sqlite:////absolute/path'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'driver' => 'Cake\Database\Driver\Sqlite', - 'database' => '/absolute/path', - 'scheme' => 'sqlite', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'sqlite:///?database=:memory:'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'driver' => 'Cake\Database\Driver\Sqlite', - 'database' => ':memory:', - 'scheme' => 'sqlite', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'driver' => 'Cake\Database\Driver\Sqlserver', - 'host' => '.\SQL2012SP1', - 'MultipleActiveResultSets' => false, - 'password' => 'Password12!', - 'database' => 'cakephp', - 'scheme' => 'sqlserver', - 'username' => 'sa', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - } - - /** - * Tests className/driver value setting - * - * @return void - */ - public function testParseDsnClassnameDriver() - { - $dsn = 'mysql://localhost:3306/database'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'database' => 'database', - 'driver' => 'Cake\Database\Driver\Mysql', - 'host' => 'localhost', - 'port' => 3306, - 'scheme' => 'mysql', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'mysql://user:password@localhost:3306/database'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'database' => 'database', - 'driver' => 'Cake\Database\Driver\Mysql', - 'host' => 'localhost', - 'password' => 'password', - 'port' => 3306, - 'scheme' => 'mysql', - 'username' => 'user', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'mysql://localhost/database?className=Custom\Driver'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'database' => 'database', - 'driver' => 'Custom\Driver', - 'host' => 'localhost', - 'scheme' => 'mysql', - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'mysql://localhost:3306/database?className=Custom\Driver'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'database' => 'database', - 'driver' => 'Custom\Driver', - 'host' => 'localhost', - 'scheme' => 'mysql', - 'port' => 3306, - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - - $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql'; - $expected = [ - 'className' => 'Cake\Database\Connection', - 'database' => 'database', - 'driver' => 'Cake\Database\Driver\Mysql', - 'host' => 'localhost', - 'scheme' => 'Cake\Database\Connection', - 'port' => 3306, - ]; - $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn)); - } - /** * Tests parsing querystring values * diff --git a/tests/TestCase/Datasource/ConnectionManagerTest.php b/tests/TestCase/Datasource/ConnectionManagerTest.php index 0c9ede99798..e07fcc19610 100644 --- a/tests/TestCase/Datasource/ConnectionManagerTest.php +++ b/tests/TestCase/Datasource/ConnectionManagerTest.php @@ -284,6 +284,142 @@ public function testParseDsn() $this->assertEquals($expected, $result); } + /** + * Tests parsing different DSNs + * + * @return void + */ + public function testParseDsnCustom() + { + $dsn = 'mysql://localhost:3306/database'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'driver' => 'Cake\Database\Driver\Mysql', + 'host' => 'localhost', + 'database' => 'database', + 'port' => 3306, + 'scheme' => 'mysql', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'mysql://user:password@localhost:3306/database'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'driver' => 'Cake\Database\Driver\Mysql', + 'host' => 'localhost', + 'password' => 'password', + 'database' => 'database', + 'port' => 3306, + 'scheme' => 'mysql', + 'username' => 'user', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'sqlite:///:memory:'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'driver' => 'Cake\Database\Driver\Sqlite', + 'database' => ':memory:', + 'scheme' => 'sqlite', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'sqlite:////absolute/path'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'driver' => 'Cake\Database\Driver\Sqlite', + 'database' => '/absolute/path', + 'scheme' => 'sqlite', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'sqlite:///?database=:memory:'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'driver' => 'Cake\Database\Driver\Sqlite', + 'database' => ':memory:', + 'scheme' => 'sqlite', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'driver' => 'Cake\Database\Driver\Sqlserver', + 'host' => '.\SQL2012SP1', + 'MultipleActiveResultSets' => false, + 'password' => 'Password12!', + 'database' => 'cakephp', + 'scheme' => 'sqlserver', + 'username' => 'sa', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + } + + /** + * Tests className/driver value setting + * + * @return void + */ + public function testParseDsnClassnameDriver() + { + $dsn = 'mysql://localhost:3306/database'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'database' => 'database', + 'driver' => 'Cake\Database\Driver\Mysql', + 'host' => 'localhost', + 'port' => 3306, + 'scheme' => 'mysql', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'mysql://user:password@localhost:3306/database'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'database' => 'database', + 'driver' => 'Cake\Database\Driver\Mysql', + 'host' => 'localhost', + 'password' => 'password', + 'port' => 3306, + 'scheme' => 'mysql', + 'username' => 'user', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'mysql://localhost/database?className=Custom\Driver'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'database' => 'database', + 'driver' => 'Custom\Driver', + 'host' => 'localhost', + 'scheme' => 'mysql', + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'mysql://localhost:3306/database?className=Custom\Driver'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'database' => 'database', + 'driver' => 'Custom\Driver', + 'host' => 'localhost', + 'scheme' => 'mysql', + 'port' => 3306, + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + + $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql'; + $expected = [ + 'className' => 'Cake\Database\Connection', + 'database' => 'database', + 'driver' => 'Cake\Database\Driver\Mysql', + 'host' => 'localhost', + 'scheme' => 'Cake\Database\Connection', + 'port' => 3306, + ]; + $this->assertEquals($expected, ConnectionManager::parseDsn($dsn)); + } + /** * Tests that directly setting an instance in a config, will not return a different * instance later on