StaticConfigTraitTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core;
  15. use Cake\Core\StaticConfigTrait;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * TestConnectionManagerStaticConfig
  19. */
  20. class TestConnectionManagerStaticConfig
  21. {
  22. use StaticConfigTrait {
  23. parseDsn as protected _parseDsn;
  24. }
  25. /**
  26. * Parse a DSN
  27. *
  28. * @param string $config The config to parse.
  29. * @return array
  30. */
  31. public static function parseDsn($config = null)
  32. {
  33. $config = static::_parseDsn($config);
  34. if (isset($config['path']) && empty($config['database'])) {
  35. $config['database'] = substr($config['path'], 1);
  36. }
  37. if (empty($config['driver'])) {
  38. $config['driver'] = $config['className'];
  39. $config['className'] = 'Cake\Database\Connection';
  40. }
  41. unset($config['path']);
  42. return $config;
  43. }
  44. /**
  45. * Database driver class map.
  46. *
  47. * @var array
  48. */
  49. protected static $_dsnClassMap = [
  50. 'mysql' => 'Cake\Database\Driver\Mysql',
  51. 'postgres' => 'Cake\Database\Driver\Postgres',
  52. 'sqlite' => 'Cake\Database\Driver\Sqlite',
  53. 'sqlserver' => 'Cake\Database\Driver\Sqlserver',
  54. ];
  55. }
  56. /**
  57. * TestCacheStaticConfig
  58. */
  59. class TestCacheStaticConfig
  60. {
  61. use StaticConfigTrait;
  62. /**
  63. * Cache driver class map.
  64. *
  65. * @var array
  66. */
  67. protected static $_dsnClassMap = [
  68. 'apc' => 'Cake\Cache\Engine\ApcEngine',
  69. 'file' => 'Cake\Cache\Engine\FileEngine',
  70. 'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
  71. 'null' => 'Cake\Cache\Engine\NullEngine',
  72. 'redis' => 'Cake\Cache\Engine\RedisEngine',
  73. 'wincache' => 'Cake\Cache\Engine\WincacheEngine',
  74. 'xcache' => 'Cake\Cache\Engine\XcacheEngine',
  75. ];
  76. }
  77. /**
  78. * TestEmailStaticConfig
  79. */
  80. class TestEmailStaticConfig
  81. {
  82. use StaticConfigTrait;
  83. /**
  84. * Email driver class map.
  85. *
  86. * @var array
  87. */
  88. protected static $_dsnClassMap = [
  89. 'debug' => 'Cake\Mailer\Transport\DebugTransport',
  90. 'mail' => 'Cake\Mailer\Transport\MailTransport',
  91. 'smtp' => 'Cake\Mailer\Transport\SmtpTransport',
  92. ];
  93. }
  94. /**
  95. * TestLogStaticConfig
  96. */
  97. class TestLogStaticConfig
  98. {
  99. use StaticConfigTrait;
  100. /**
  101. * Log engine class map.
  102. *
  103. * @var array
  104. */
  105. protected static $_dsnClassMap = [
  106. 'console' => 'Cake\Log\Engine\ConsoleLog',
  107. 'file' => 'Cake\Log\Engine\FileLog',
  108. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  109. ];
  110. }
  111. /**
  112. * StaticConfigTraitTest class
  113. *
  114. */
  115. class StaticConfigTraitTest extends TestCase
  116. {
  117. /**
  118. * setup method
  119. *
  120. * @return void
  121. */
  122. public function setUp()
  123. {
  124. parent::setUp();
  125. $this->subject = $this->getObjectForTrait('Cake\Core\StaticConfigTrait');
  126. }
  127. /**
  128. * teardown method
  129. *
  130. * @return void
  131. */
  132. public function tearDown()
  133. {
  134. unset($this->subject);
  135. parent::tearDown();
  136. }
  137. /**
  138. * Tests simple usage of parseDsn
  139. *
  140. * @return void
  141. */
  142. public function testSimpleParseDsn()
  143. {
  144. $className = get_class($this->subject);
  145. $this->assertSame([], $className::parseDsn(''));
  146. }
  147. /**
  148. * Tests that failing to pass a string to parseDsn will throw an exception
  149. *
  150. * @expectedException \InvalidArgumentException
  151. * @return void
  152. */
  153. public function testParseBadType()
  154. {
  155. $className = get_class($this->subject);
  156. $className::parseDsn(['url' => 'http://:80']);
  157. }
  158. /**
  159. * Tests parsing different DSNs
  160. *
  161. * @return void
  162. */
  163. public function testCustomParseDsn()
  164. {
  165. $dsn = 'mysql://localhost:3306/database';
  166. $expected = [
  167. 'className' => 'Cake\Database\Connection',
  168. 'driver' => 'Cake\Database\Driver\Mysql',
  169. 'host' => 'localhost',
  170. 'database' => 'database',
  171. 'port' => 3306,
  172. 'scheme' => 'mysql',
  173. ];
  174. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  175. $dsn = 'mysql://user:password@localhost:3306/database';
  176. $expected = [
  177. 'className' => 'Cake\Database\Connection',
  178. 'driver' => 'Cake\Database\Driver\Mysql',
  179. 'host' => 'localhost',
  180. 'password' => 'password',
  181. 'database' => 'database',
  182. 'port' => 3306,
  183. 'scheme' => 'mysql',
  184. 'username' => 'user',
  185. ];
  186. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  187. $dsn = 'sqlite:///:memory:';
  188. $expected = [
  189. 'className' => 'Cake\Database\Connection',
  190. 'driver' => 'Cake\Database\Driver\Sqlite',
  191. 'database' => ':memory:',
  192. 'scheme' => 'sqlite',
  193. ];
  194. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  195. $dsn = 'sqlite:////absolute/path';
  196. $expected = [
  197. 'className' => 'Cake\Database\Connection',
  198. 'driver' => 'Cake\Database\Driver\Sqlite',
  199. 'database' => '/absolute/path',
  200. 'scheme' => 'sqlite',
  201. ];
  202. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  203. $dsn = 'sqlite:///?database=:memory:';
  204. $expected = [
  205. 'className' => 'Cake\Database\Connection',
  206. 'driver' => 'Cake\Database\Driver\Sqlite',
  207. 'database' => ':memory:',
  208. 'scheme' => 'sqlite',
  209. ];
  210. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  211. $dsn = 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false';
  212. $expected = [
  213. 'className' => 'Cake\Database\Connection',
  214. 'driver' => 'Cake\Database\Driver\Sqlserver',
  215. 'host' => '.\SQL2012SP1',
  216. 'MultipleActiveResultSets' => false,
  217. 'password' => 'Password12!',
  218. 'database' => 'cakephp',
  219. 'scheme' => 'sqlserver',
  220. 'username' => 'sa',
  221. ];
  222. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  223. }
  224. /**
  225. * Tests className/driver value setting
  226. *
  227. * @return void
  228. */
  229. public function testParseDsnClassnameDriver()
  230. {
  231. $dsn = 'mysql://localhost:3306/database';
  232. $expected = [
  233. 'className' => 'Cake\Database\Connection',
  234. 'database' => 'database',
  235. 'driver' => 'Cake\Database\Driver\Mysql',
  236. 'host' => 'localhost',
  237. 'port' => 3306,
  238. 'scheme' => 'mysql',
  239. ];
  240. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  241. $dsn = 'mysql://user:password@localhost:3306/database';
  242. $expected = [
  243. 'className' => 'Cake\Database\Connection',
  244. 'database' => 'database',
  245. 'driver' => 'Cake\Database\Driver\Mysql',
  246. 'host' => 'localhost',
  247. 'password' => 'password',
  248. 'port' => 3306,
  249. 'scheme' => 'mysql',
  250. 'username' => 'user',
  251. ];
  252. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  253. $dsn = 'mysql://localhost/database?className=Custom\Driver';
  254. $expected = [
  255. 'className' => 'Cake\Database\Connection',
  256. 'database' => 'database',
  257. 'driver' => 'Custom\Driver',
  258. 'host' => 'localhost',
  259. 'scheme' => 'mysql',
  260. ];
  261. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  262. $dsn = 'mysql://localhost:3306/database?className=Custom\Driver';
  263. $expected = [
  264. 'className' => 'Cake\Database\Connection',
  265. 'database' => 'database',
  266. 'driver' => 'Custom\Driver',
  267. 'host' => 'localhost',
  268. 'scheme' => 'mysql',
  269. 'port' => 3306,
  270. ];
  271. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  272. $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  273. $expected = [
  274. 'className' => 'Cake\Database\Connection',
  275. 'database' => 'database',
  276. 'driver' => 'Cake\Database\Driver\Mysql',
  277. 'host' => 'localhost',
  278. 'scheme' => 'Cake\Database\Connection',
  279. 'port' => 3306,
  280. ];
  281. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  282. }
  283. /**
  284. * Tests parsing querystring values
  285. *
  286. * @return void
  287. */
  288. public function testParseDsnQuerystring()
  289. {
  290. $dsn = 'file:///?url=test';
  291. $expected = [
  292. 'className' => 'Cake\Log\Engine\FileLog',
  293. 'path' => '/',
  294. 'scheme' => 'file',
  295. 'url' => 'test',
  296. ];
  297. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  298. $dsn = 'file:///?file=debug&key=value';
  299. $expected = [
  300. 'className' => 'Cake\Log\Engine\FileLog',
  301. 'file' => 'debug',
  302. 'key' => 'value',
  303. 'path' => '/',
  304. 'scheme' => 'file',
  305. ];
  306. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  307. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  308. $expected = [
  309. 'className' => 'Cake\Log\Engine\FileLog',
  310. 'file' => 'debug',
  311. 'path' => '/tmp',
  312. 'scheme' => 'file',
  313. 'types' => ['notice', 'info', 'debug'],
  314. ];
  315. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  316. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  317. $expected = [
  318. 'className' => 'Cake\Mailer\Transport\MailTransport',
  319. 'client' => null,
  320. 'key' => true,
  321. 'key2' => false,
  322. 'path' => '/',
  323. 'scheme' => 'mail',
  324. 'timeout' => '30',
  325. 'tls' => null,
  326. ];
  327. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  328. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  329. $expected = [
  330. 'className' => 'Cake\Mailer\Transport\MailTransport',
  331. 'client' => null,
  332. 'host' => 'null',
  333. 'key' => true,
  334. 'key2' => false,
  335. 'password' => 'false',
  336. 'path' => '/1',
  337. 'scheme' => 'mail',
  338. 'timeout' => '30',
  339. 'tls' => null,
  340. 'username' => 'true',
  341. ];
  342. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  343. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
  344. $expected = [
  345. 'className' => 'Cake\Mailer\Transport\MailTransport',
  346. 'client' => null,
  347. 'host' => 'localhost',
  348. 'password' => 'secret',
  349. 'port' => 25,
  350. 'scheme' => 'mail',
  351. 'timeout' => '30',
  352. 'tls' => null,
  353. 'username' => 'user',
  354. ];
  355. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  356. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  357. $expected = [
  358. 'className' => 'Cake\Log\Engine\FileLog',
  359. 'duration' => '+2 minutes',
  360. 'path' => '/',
  361. 'prefix' => 'myapp_cake_core_',
  362. 'scheme' => 'file',
  363. 'serialize' => true,
  364. ];
  365. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  366. }
  367. /**
  368. * Tests loading a single plugin
  369. *
  370. * @return void
  371. */
  372. public function testParseDsnPathSetting()
  373. {
  374. $dsn = 'file:///?path=/tmp/persistent/';
  375. $expected = [
  376. 'className' => 'Cake\Log\Engine\FileLog',
  377. 'path' => '/tmp/persistent/',
  378. 'scheme' => 'file',
  379. ];
  380. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  381. }
  382. /**
  383. * Test that the dsn map can be updated/append to
  384. *
  385. * @return void
  386. */
  387. public function testCanUpdateClassMap()
  388. {
  389. $expected = [
  390. 'console' => 'Cake\Log\Engine\ConsoleLog',
  391. 'file' => 'Cake\Log\Engine\FileLog',
  392. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  393. ];
  394. $result = TestLogStaticConfig::dsnClassMap();
  395. $this->assertEquals($expected, $result, "The class map should match the class property");
  396. $expected = [
  397. 'console' => 'Special\EngineLog',
  398. 'file' => 'Cake\Log\Engine\FileLog',
  399. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  400. ];
  401. $result = TestLogStaticConfig::dsnClassMap(['console' => 'Special\EngineLog']);
  402. $this->assertEquals($expected, $result, "Should be possible to change the map");
  403. $expected = [
  404. 'console' => 'Special\EngineLog',
  405. 'file' => 'Cake\Log\Engine\FileLog',
  406. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  407. 'my' => 'Special\OtherLog'
  408. ];
  409. $result = TestLogStaticConfig::dsnClassMap(['my' => 'Special\OtherLog']);
  410. $this->assertEquals($expected, $result, "Should be possible to add to the map");
  411. }
  412. }