StaticConfigTraitTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. 'host' => ':memory:',
  182. 'scheme' => 'sqlite',
  183. ];
  184. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  185. $dsn = 'sqlite:///?database=:memory:';
  186. $expected = [
  187. 'className' => 'Cake\Database\Connection',
  188. 'driver' => 'Cake\Database\Driver\Sqlite',
  189. 'database' => ':memory:',
  190. 'scheme' => 'sqlite',
  191. ];
  192. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  193. $dsn = 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false';
  194. $expected = [
  195. 'className' => 'Cake\Database\Connection',
  196. 'driver' => 'Cake\Database\Driver\Sqlserver',
  197. 'host' => '.\SQL2012SP1',
  198. 'MultipleActiveResultSets' => false,
  199. 'password' => 'Password12!',
  200. 'database' => 'cakephp',
  201. 'scheme' => 'sqlserver',
  202. 'username' => 'sa',
  203. ];
  204. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  205. }
  206. /**
  207. * Tests className/driver value setting
  208. *
  209. * @return void
  210. */
  211. public function testParseDsnClassnameDriver() {
  212. $dsn = 'mysql://localhost:3306/database';
  213. $expected = [
  214. 'className' => 'Cake\Database\Connection',
  215. 'database' => 'database',
  216. 'driver' => 'Cake\Database\Driver\Mysql',
  217. 'host' => 'localhost',
  218. 'port' => 3306,
  219. 'scheme' => 'mysql',
  220. ];
  221. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  222. $dsn = 'mysql://user:password@localhost:3306/database';
  223. $expected = [
  224. 'className' => 'Cake\Database\Connection',
  225. 'database' => 'database',
  226. 'driver' => 'Cake\Database\Driver\Mysql',
  227. 'host' => 'localhost',
  228. 'password' => 'password',
  229. 'port' => 3306,
  230. 'scheme' => 'mysql',
  231. 'username' => 'user',
  232. ];
  233. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  234. $dsn = 'mysql://localhost/database?className=Custom\Driver';
  235. $expected = [
  236. 'className' => 'Cake\Database\Connection',
  237. 'database' => 'database',
  238. 'driver' => 'Custom\Driver',
  239. 'host' => 'localhost',
  240. 'scheme' => 'mysql',
  241. ];
  242. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  243. $dsn = 'mysql://localhost:3306/database?className=Custom\Driver';
  244. $expected = [
  245. 'className' => 'Cake\Database\Connection',
  246. 'database' => 'database',
  247. 'driver' => 'Custom\Driver',
  248. 'host' => 'localhost',
  249. 'scheme' => 'mysql',
  250. 'port' => 3306,
  251. ];
  252. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  253. $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  254. $expected = [
  255. 'className' => 'Cake\Database\Connection',
  256. 'database' => 'database',
  257. 'driver' => 'Cake\Database\Driver\Mysql',
  258. 'host' => 'localhost',
  259. 'scheme' => 'Cake\Database\Connection',
  260. 'port' => 3306,
  261. ];
  262. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  263. }
  264. /**
  265. * Tests parsing querystring values
  266. *
  267. * @return void
  268. */
  269. public function testParseDsnQuerystring() {
  270. $dsn = 'file:///?url=test';
  271. $expected = [
  272. 'className' => 'Cake\Log\Engine\FileLog',
  273. 'path' => '/',
  274. 'scheme' => 'file',
  275. 'url' => 'test',
  276. ];
  277. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  278. $dsn = 'file:///?file=debug&key=value';
  279. $expected = [
  280. 'className' => 'Cake\Log\Engine\FileLog',
  281. 'file' => 'debug',
  282. 'key' => 'value',
  283. 'path' => '/',
  284. 'scheme' => 'file',
  285. ];
  286. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  287. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  288. $expected = [
  289. 'className' => 'Cake\Log\Engine\FileLog',
  290. 'file' => 'debug',
  291. 'path' => '/tmp',
  292. 'scheme' => 'file',
  293. 'types' => ['notice', 'info', 'debug'],
  294. ];
  295. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  296. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  297. $expected = [
  298. 'className' => 'Cake\Network\Email\MailTransport',
  299. 'client' => null,
  300. 'key' => true,
  301. 'key2' => false,
  302. 'path' => '/',
  303. 'scheme' => 'mail',
  304. 'timeout' => '30',
  305. 'tls' => null,
  306. ];
  307. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  308. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  309. $expected = [
  310. 'className' => 'Cake\Network\Email\MailTransport',
  311. 'client' => null,
  312. 'host' => 'null',
  313. 'key' => true,
  314. 'key2' => false,
  315. 'password' => 'false',
  316. 'path' => '/1',
  317. 'scheme' => 'mail',
  318. 'timeout' => '30',
  319. 'tls' => null,
  320. 'username' => 'true',
  321. ];
  322. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  323. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
  324. $expected = [
  325. 'className' => 'Cake\Network\Email\MailTransport',
  326. 'client' => null,
  327. 'host' => 'localhost',
  328. 'password' => 'secret',
  329. 'port' => 25,
  330. 'scheme' => 'mail',
  331. 'timeout' => '30',
  332. 'tls' => null,
  333. 'username' => 'user',
  334. ];
  335. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  336. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  337. $expected = [
  338. 'className' => 'Cake\Log\Engine\FileLog',
  339. 'duration' => '+2 minutes',
  340. 'path' => '/',
  341. 'prefix' => 'myapp_cake_core_',
  342. 'scheme' => 'file',
  343. 'serialize' => true,
  344. ];
  345. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  346. }
  347. /**
  348. * Tests loading a single plugin
  349. *
  350. * @return void
  351. */
  352. public function testParseDsnPathSetting() {
  353. $dsn = 'file:///';
  354. $expected = [
  355. 'className' => 'Cake\Log\Engine\FileLog',
  356. 'path' => '/',
  357. 'scheme' => 'file',
  358. ];
  359. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  360. $dsn = 'file:///?path=/tmp/persistent/';
  361. $expected = [
  362. 'className' => 'Cake\Log\Engine\FileLog',
  363. 'path' => '/tmp/persistent/',
  364. 'scheme' => 'file',
  365. ];
  366. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  367. }
  368. /**
  369. * Test that the dsn map can be updated/append to
  370. *
  371. * @return void
  372. */
  373. public function testCanUpdateClassMap() {
  374. $expected = [
  375. 'console' => 'Cake\Log\Engine\ConsoleLog',
  376. 'file' => 'Cake\Log\Engine\FileLog',
  377. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  378. ];
  379. $result = TestLogStaticConfig::dsnClassMap();
  380. $this->assertEquals($expected, $result, "The class map should match the class property");
  381. $expected = [
  382. 'console' => 'Special\EngineLog',
  383. 'file' => 'Cake\Log\Engine\FileLog',
  384. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  385. ];
  386. $result = TestLogStaticConfig::dsnClassMap(['console' => 'Special\EngineLog']);
  387. $this->assertEquals($expected, $result, "Should be possible to change the map");
  388. $expected = [
  389. 'console' => 'Special\EngineLog',
  390. 'file' => 'Cake\Log\Engine\FileLog',
  391. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  392. 'my' => 'Special\OtherLog'
  393. ];
  394. $result = TestLogStaticConfig::dsnClassMap(['my' => 'Special\OtherLog']);
  395. $this->assertEquals($expected, $result, "Should be possible to add to the map");
  396. }
  397. }