Skip to content

Commit

Permalink
Move ConnectionManager tests with the other tests for that class
Browse files Browse the repository at this point in the history
Remove the duplicate stub code and move the tests for ConnectionManager
code with the rest of that class' tests.
  • Loading branch information
markstory committed Aug 9, 2017
1 parent 2b73b0a commit f582340
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 183 deletions.
183 changes: 0 additions & 183 deletions tests/TestCase/Core/StaticConfigTraitTest.php
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down
136 changes: 136 additions & 0 deletions tests/TestCase/Datasource/ConnectionManagerTest.php
Expand Up @@ -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
Expand Down

0 comments on commit f582340

Please sign in to comment.