StaticConfigTraitTest.php 12 KB

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