Skip to content

Commit

Permalink
Refactor tests to use a data provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 11, 2017
1 parent 519b0ce commit f13b003
Showing 1 changed file with 121 additions and 167 deletions.
288 changes: 121 additions & 167 deletions tests/TestCase/Datasource/ConnectionManagerTest.php
Expand Up @@ -263,184 +263,138 @@ public function testAliasError()
}

/**
* Test parseDsn method.
*
* @return void
*/
public function testParseDsn()
{
$result = ConnectionManager::parseDsn('mysql://root:secret@localhost:3306/database?log=1');
$expected = [
'scheme' => 'mysql',
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'port' => 3306,
'database' => 'database',
'log' => '1'
];
$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
* provider for DSN strings.
*
* @return void
* @return array
*/
public function testParseDsnClassnameDriver()
public function dsnProvider()
{
$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,
return [
'no user' => [
'mysql://localhost:3306/database',
[
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'database' => 'database',
'port' => 3306,
'scheme' => 'mysql',
]
],
'user & pass' => [
'mysql://root:secret@localhost:3306/database?log=1',
[
'scheme' => 'mysql',
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'port' => 3306,
'database' => 'database',
'log' => '1'
]
],
'sqlite memory' => [
'sqlite:///:memory:',
[
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlite',
'database' => ':memory:',
'scheme' => 'sqlite',
]
],
'sqlite path' => [
'sqlite:////absolute/path',
[
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlite',
'database' => '/absolute/path',
'scheme' => 'sqlite',
]
],
'sqlite database query' => [
'sqlite:///?database=:memory:',
[
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlite',
'database' => ':memory:',
'scheme' => 'sqlite',
]
],
'sqlserver' => [
'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false',
[
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlserver',
'host' => '.\SQL2012SP1',
'MultipleActiveResultSets' => false,
'password' => 'Password12!',
'database' => 'cakephp',
'scheme' => 'sqlserver',
'username' => 'sa',
]
],
'classname query arg' => [
'mysql://localhost/database?className=Custom\Driver',
[
'className' => 'Cake\Database\Connection',
'database' => 'database',
'driver' => 'Custom\Driver',
'host' => 'localhost',
'scheme' => 'mysql',
]
],
'classname and port' => [
'mysql://localhost:3306/database?className=Custom\Driver',
[
'className' => 'Cake\Database\Connection',
'database' => 'database',
'driver' => 'Custom\Driver',
'host' => 'localhost',
'scheme' => 'mysql',
'port' => 3306,
]
],
'custom connection class' => [
'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql',
[
'className' => 'Cake\Database\Connection',
'database' => 'database',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'scheme' => 'Cake\Database\Connection',
'port' => 3306,
]
],
'complex password' => [
'mysql://user:pas#][{}$%20@!@localhost:3306/database?log=1&quoteIdentifiers=1',
[
'className' => 'Cake\Database\Connection',
'database' => 'database',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'password' => 'pas#][{}$%20@!',
'port' => 3306,
'scheme' => 'mysql',
'username' => 'user',
'log' => 1,
'quoteIdentifiers' => 1,
]
]
];
$this->assertEquals($expected, ConnectionManager::parseDsn($dsn));
}

/**
* Test parseDsn with special characters in the password.
* Test parseDsn method.
*
* @dataProvider dsnProvider
* @return void
*/
public function testParseDsnSpecialPassword()
public function testParseDsn($dsn, $expected)
{
$dsn = 'mysql://user:pas#][{}$%20@!@localhost:3306/database?log=1&quoteIdentifiers=1';
$expected = [
'className' => 'Cake\Database\Connection',
'database' => 'database',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'password' => 'pas#][{}$%20@!',
'port' => 3306,
'scheme' => 'mysql',
'username' => 'user',
'log' => 1,
'quoteIdentifiers' => 1
];
$this->assertEquals($expected, ConnectionManager::parseDsn($dsn));
$result = ConnectionManager::parseDsn($dsn);
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit f13b003

Please sign in to comment.