StaticConfigTraitTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. class StaticConfigTraitTest extends TestCase
  115. {
  116. /**
  117. * setup method
  118. *
  119. * @return void
  120. */
  121. public function setUp()
  122. {
  123. parent::setUp();
  124. $this->subject = $this->getObjectForTrait('Cake\Core\StaticConfigTrait');
  125. }
  126. /**
  127. * teardown method
  128. *
  129. * @return void
  130. */
  131. public function tearDown()
  132. {
  133. unset($this->subject);
  134. parent::tearDown();
  135. }
  136. /**
  137. * Tests simple usage of parseDsn
  138. *
  139. * @return void
  140. */
  141. public function testSimpleParseDsn()
  142. {
  143. $className = get_class($this->subject);
  144. $this->assertSame([], $className::parseDsn(''));
  145. }
  146. /**
  147. * Tests that failing to pass a string to parseDsn will throw an exception
  148. *
  149. * @expectedException \InvalidArgumentException
  150. * @return void
  151. */
  152. public function testParseBadType()
  153. {
  154. $className = get_class($this->subject);
  155. $className::parseDsn(['url' => 'http://:80']);
  156. }
  157. /**
  158. * Tests parsing different DSNs
  159. *
  160. * @return void
  161. */
  162. public function testCustomParseDsn()
  163. {
  164. $dsn = 'mysql://localhost:3306/database';
  165. $expected = [
  166. 'className' => 'Cake\Database\Connection',
  167. 'driver' => 'Cake\Database\Driver\Mysql',
  168. 'host' => 'localhost',
  169. 'database' => 'database',
  170. 'port' => 3306,
  171. 'scheme' => 'mysql',
  172. ];
  173. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  174. $dsn = 'mysql://user:password@localhost:3306/database';
  175. $expected = [
  176. 'className' => 'Cake\Database\Connection',
  177. 'driver' => 'Cake\Database\Driver\Mysql',
  178. 'host' => 'localhost',
  179. 'password' => 'password',
  180. 'database' => 'database',
  181. 'port' => 3306,
  182. 'scheme' => 'mysql',
  183. 'username' => 'user',
  184. ];
  185. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  186. $dsn = 'sqlite:///:memory:';
  187. $expected = [
  188. 'className' => 'Cake\Database\Connection',
  189. 'driver' => 'Cake\Database\Driver\Sqlite',
  190. 'database' => ':memory:',
  191. 'scheme' => 'sqlite',
  192. ];
  193. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  194. $dsn = 'sqlite:////absolute/path';
  195. $expected = [
  196. 'className' => 'Cake\Database\Connection',
  197. 'driver' => 'Cake\Database\Driver\Sqlite',
  198. 'database' => '/absolute/path',
  199. 'scheme' => 'sqlite',
  200. ];
  201. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  202. $dsn = 'sqlite:///?database=:memory:';
  203. $expected = [
  204. 'className' => 'Cake\Database\Connection',
  205. 'driver' => 'Cake\Database\Driver\Sqlite',
  206. 'database' => ':memory:',
  207. 'scheme' => 'sqlite',
  208. ];
  209. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  210. $dsn = 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false';
  211. $expected = [
  212. 'className' => 'Cake\Database\Connection',
  213. 'driver' => 'Cake\Database\Driver\Sqlserver',
  214. 'host' => '.\SQL2012SP1',
  215. 'MultipleActiveResultSets' => false,
  216. 'password' => 'Password12!',
  217. 'database' => 'cakephp',
  218. 'scheme' => 'sqlserver',
  219. 'username' => 'sa',
  220. ];
  221. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  222. }
  223. /**
  224. * Tests className/driver value setting
  225. *
  226. * @return void
  227. */
  228. public function testParseDsnClassnameDriver()
  229. {
  230. $dsn = 'mysql://localhost:3306/database';
  231. $expected = [
  232. 'className' => 'Cake\Database\Connection',
  233. 'database' => 'database',
  234. 'driver' => 'Cake\Database\Driver\Mysql',
  235. 'host' => 'localhost',
  236. 'port' => 3306,
  237. 'scheme' => 'mysql',
  238. ];
  239. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  240. $dsn = 'mysql://user:password@localhost:3306/database';
  241. $expected = [
  242. 'className' => 'Cake\Database\Connection',
  243. 'database' => 'database',
  244. 'driver' => 'Cake\Database\Driver\Mysql',
  245. 'host' => 'localhost',
  246. 'password' => 'password',
  247. 'port' => 3306,
  248. 'scheme' => 'mysql',
  249. 'username' => 'user',
  250. ];
  251. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  252. $dsn = 'mysql://localhost/database?className=Custom\Driver';
  253. $expected = [
  254. 'className' => 'Cake\Database\Connection',
  255. 'database' => 'database',
  256. 'driver' => 'Custom\Driver',
  257. 'host' => 'localhost',
  258. 'scheme' => 'mysql',
  259. ];
  260. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  261. $dsn = 'mysql://localhost:3306/database?className=Custom\Driver';
  262. $expected = [
  263. 'className' => 'Cake\Database\Connection',
  264. 'database' => 'database',
  265. 'driver' => 'Custom\Driver',
  266. 'host' => 'localhost',
  267. 'scheme' => 'mysql',
  268. 'port' => 3306,
  269. ];
  270. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  271. $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  272. $expected = [
  273. 'className' => 'Cake\Database\Connection',
  274. 'database' => 'database',
  275. 'driver' => 'Cake\Database\Driver\Mysql',
  276. 'host' => 'localhost',
  277. 'scheme' => 'Cake\Database\Connection',
  278. 'port' => 3306,
  279. ];
  280. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  281. }
  282. /**
  283. * Tests parsing querystring values
  284. *
  285. * @return void
  286. */
  287. public function testParseDsnQuerystring()
  288. {
  289. $dsn = 'file:///?url=test';
  290. $expected = [
  291. 'className' => 'Cake\Log\Engine\FileLog',
  292. 'path' => '/',
  293. 'scheme' => 'file',
  294. 'url' => 'test',
  295. ];
  296. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  297. $dsn = 'file:///?file=debug&key=value';
  298. $expected = [
  299. 'className' => 'Cake\Log\Engine\FileLog',
  300. 'file' => 'debug',
  301. 'key' => 'value',
  302. 'path' => '/',
  303. 'scheme' => 'file',
  304. ];
  305. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  306. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  307. $expected = [
  308. 'className' => 'Cake\Log\Engine\FileLog',
  309. 'file' => 'debug',
  310. 'path' => '/tmp',
  311. 'scheme' => 'file',
  312. 'types' => ['notice', 'info', 'debug'],
  313. ];
  314. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  315. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  316. $expected = [
  317. 'className' => 'Cake\Mailer\Transport\MailTransport',
  318. 'client' => null,
  319. 'key' => true,
  320. 'key2' => false,
  321. 'path' => '/',
  322. 'scheme' => 'mail',
  323. 'timeout' => '30',
  324. 'tls' => null,
  325. ];
  326. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  327. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  328. $expected = [
  329. 'className' => 'Cake\Mailer\Transport\MailTransport',
  330. 'client' => null,
  331. 'host' => 'null',
  332. 'key' => true,
  333. 'key2' => false,
  334. 'password' => 'false',
  335. 'path' => '/1',
  336. 'scheme' => 'mail',
  337. 'timeout' => '30',
  338. 'tls' => null,
  339. 'username' => 'true',
  340. ];
  341. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  342. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
  343. $expected = [
  344. 'className' => 'Cake\Mailer\Transport\MailTransport',
  345. 'client' => null,
  346. 'host' => 'localhost',
  347. 'password' => 'secret',
  348. 'port' => 25,
  349. 'scheme' => 'mail',
  350. 'timeout' => '30',
  351. 'tls' => null,
  352. 'username' => 'user',
  353. ];
  354. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  355. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  356. $expected = [
  357. 'className' => 'Cake\Log\Engine\FileLog',
  358. 'duration' => '+2 minutes',
  359. 'path' => '/',
  360. 'prefix' => 'myapp_cake_core_',
  361. 'scheme' => 'file',
  362. 'serialize' => true,
  363. ];
  364. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  365. }
  366. /**
  367. * Tests loading a single plugin
  368. *
  369. * @return void
  370. */
  371. public function testParseDsnPathSetting()
  372. {
  373. $dsn = 'file:///?path=/tmp/persistent/';
  374. $expected = [
  375. 'className' => 'Cake\Log\Engine\FileLog',
  376. 'path' => '/tmp/persistent/',
  377. 'scheme' => 'file',
  378. ];
  379. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  380. }
  381. /**
  382. * Test that the dsn map can be updated/append to
  383. *
  384. * @return void
  385. */
  386. public function testCanUpdateClassMap()
  387. {
  388. $expected = [
  389. 'console' => 'Cake\Log\Engine\ConsoleLog',
  390. 'file' => 'Cake\Log\Engine\FileLog',
  391. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  392. ];
  393. $result = TestLogStaticConfig::dsnClassMap();
  394. $this->assertEquals($expected, $result, "The class map should match the class property");
  395. $expected = [
  396. 'console' => 'Special\EngineLog',
  397. 'file' => 'Cake\Log\Engine\FileLog',
  398. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  399. ];
  400. $result = TestLogStaticConfig::dsnClassMap(['console' => 'Special\EngineLog']);
  401. $this->assertEquals($expected, $result, "Should be possible to change the map");
  402. $expected = [
  403. 'console' => 'Special\EngineLog',
  404. 'file' => 'Cake\Log\Engine\FileLog',
  405. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  406. 'my' => 'Special\OtherLog'
  407. ];
  408. $result = TestLogStaticConfig::dsnClassMap(['my' => 'Special\OtherLog']);
  409. $this->assertEquals($expected, $result, "Should be possible to add to the map");
  410. }
  411. /**
  412. * Tests that former handling of integer keys coming in from PHP internal conversions
  413. * won't break in 3.4.
  414. *
  415. * @return void
  416. */
  417. public function testConfigBC()
  418. {
  419. $result = TestLogStaticConfig::config(404);
  420. $this->assertNull($result);
  421. }
  422. }