PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/cakephp/tests/TestCase/Core/StaticConfigTraitTest.php

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