| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Core;
- use Cake\Core\StaticConfigTrait;
- use Cake\TestSuite\TestCase;
- use PHPUnit_Framework_Test;
- /**
- * 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
- */
- class TestCacheStaticConfig {
- use StaticConfigTrait;
- /**
- * Cache driver class map.
- *
- * @var array
- */
- protected static $_dsnClassMap = [
- 'apc' => 'Cake\Cache\Engine\ApcEngine',
- 'file' => 'Cake\Cache\Engine\FileEngine',
- 'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
- 'null' => 'Cake\Cache\Engine\NullEngine',
- 'redis' => 'Cake\Cache\Engine\RedisEngine',
- 'wincache' => 'Cake\Cache\Engine\WincacheEngine',
- 'xcache' => 'Cake\Cache\Engine\XcacheEngine',
- ];
- }
- /**
- * TestEmailStaticConfig
- */
- class TestEmailStaticConfig {
- use StaticConfigTrait;
- /**
- * Email driver class map.
- *
- * @var array
- */
- protected static $_dsnClassMap = [
- 'debug' => 'Cake\Network\Email\DebugTransport',
- 'mail' => 'Cake\Network\Email\MailTransport',
- 'smtp' => 'Cake\Network\Email\SmtpTransport',
- ];
- }
- /**
- * TestLogStaticConfig
- */
- class TestLogStaticConfig {
- use StaticConfigTrait;
- /**
- * Log engine class map.
- *
- * @var array
- */
- protected static $_dsnClassMap = [
- 'console' => 'Cake\Log\Engine\ConsoleLog',
- 'file' => 'Cake\Log\Engine\FileLog',
- 'syslog' => 'Cake\Log\Engine\SyslogLog',
- ];
- }
- /**
- * StaticConfigTraitTest class
- *
- */
- class StaticConfigTraitTest extends TestCase {
- /**
- * setup method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->subject = $this->getObjectForTrait('Cake\Core\StaticConfigTrait');
- }
- /**
- * teardown method
- *
- * @return void
- */
- public function tearDown() {
- unset($this->subject);
- parent::tearDown();
- }
- /**
- * Tests simple usage of parseDsn
- *
- * @return void
- */
- public function testSimpleParseDsn() {
- $className = get_class($this->subject);
- $this->assertSame([], $className::parseDsn(''));
- }
- /**
- * Tests that failing to pass a string to parseDsn will throw an exception
- *
- * @expectedException InvalidArgumentException
- * @return void
- */
- public function testParseBadType() {
- $className = get_class($this->subject);
- $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
- *
- * @return void
- */
- public function testParseDsnQuerystring() {
- $dsn = 'file:///?url=test';
- $expected = [
- 'className' => 'Cake\Log\Engine\FileLog',
- 'path' => '/',
- 'scheme' => 'file',
- 'url' => 'test',
- ];
- $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
- $dsn = 'file:///?file=debug&key=value';
- $expected = [
- 'className' => 'Cake\Log\Engine\FileLog',
- 'file' => 'debug',
- 'key' => 'value',
- 'path' => '/',
- 'scheme' => 'file',
- ];
- $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
- $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
- $expected = [
- 'className' => 'Cake\Log\Engine\FileLog',
- 'file' => 'debug',
- 'path' => '/tmp',
- 'scheme' => 'file',
- 'types' => ['notice', 'info', 'debug'],
- ];
- $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
- $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
- $expected = [
- 'className' => 'Cake\Network\Email\MailTransport',
- 'client' => null,
- 'key' => true,
- 'key2' => false,
- 'path' => '/',
- 'scheme' => 'mail',
- 'timeout' => '30',
- 'tls' => null,
- ];
- $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
- $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
- $expected = [
- 'className' => 'Cake\Network\Email\MailTransport',
- 'client' => null,
- 'host' => 'null',
- 'key' => true,
- 'key2' => false,
- 'password' => 'false',
- 'path' => '/1',
- 'scheme' => 'mail',
- 'timeout' => '30',
- 'tls' => null,
- 'username' => 'true',
- ];
- $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
- $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
- $expected = [
- 'className' => 'Cake\Network\Email\MailTransport',
- 'client' => null,
- 'host' => 'localhost',
- 'password' => 'secret',
- 'port' => 25,
- 'scheme' => 'mail',
- 'timeout' => '30',
- 'tls' => null,
- 'username' => 'user',
- ];
- $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
- $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
- $expected = [
- 'className' => 'Cake\Log\Engine\FileLog',
- 'duration' => '+2 minutes',
- 'path' => '/',
- 'prefix' => 'myapp_cake_core_',
- 'scheme' => 'file',
- 'serialize' => true,
- ];
- $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
- }
- /**
- * Tests loading a single plugin
- *
- * @return void
- */
- public function testParseDsnPathSetting() {
- $dsn = 'file:///';
- $expected = [
- 'className' => 'Cake\Log\Engine\FileLog',
- 'path' => '/',
- 'scheme' => 'file',
- ];
- $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
- $dsn = 'file:///?path=/tmp/persistent/';
- $expected = [
- 'className' => 'Cake\Log\Engine\FileLog',
- 'path' => '/tmp/persistent/',
- 'scheme' => 'file',
- ];
- $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
- }
- /**
- * Test that the dsn map can be updated/append to
- *
- * @return void
- */
- public function testCanUpdateClassMap() {
- $expected = [
- 'console' => 'Cake\Log\Engine\ConsoleLog',
- 'file' => 'Cake\Log\Engine\FileLog',
- 'syslog' => 'Cake\Log\Engine\SyslogLog',
- ];
- $result = TestLogStaticConfig::dsnClassMap();
- $this->assertEquals($expected, $result, "The class map should match the class property");
- $expected = [
- 'console' => 'Special\EngineLog',
- 'file' => 'Cake\Log\Engine\FileLog',
- 'syslog' => 'Cake\Log\Engine\SyslogLog',
- ];
- $result = TestLogStaticConfig::dsnClassMap(['console' => 'Special\EngineLog']);
- $this->assertEquals($expected, $result, "Should be possible to change the map");
- $expected = [
- 'console' => 'Special\EngineLog',
- 'file' => 'Cake\Log\Engine\FileLog',
- 'syslog' => 'Cake\Log\Engine\SyslogLog',
- 'my' => 'Special\OtherLog'
- ];
- $result = TestLogStaticConfig::dsnClassMap(['my' => 'Special\OtherLog']);
- $this->assertEquals($expected, $result, "Should be possible to add to the map");
- }
- }
|