StaticConfigTraitTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. public static function parseDsn($config = null) {
  26. $config = static::_parseDsn($config);
  27. if (isset($config['path']) && empty($config['database'])) {
  28. $config['database'] = substr($config['path'], 1);
  29. }
  30. if (empty($config['driver'])) {
  31. $config['driver'] = $config['className'];
  32. $config['className'] = 'Cake\Database\Connection';
  33. }
  34. unset($config['path']);
  35. return $config;
  36. }
  37. public static function getClassMap() {
  38. return [
  39. 'mysql' => 'Cake\Database\Driver\Mysql',
  40. 'postgres' => 'Cake\Database\Driver\Postgres',
  41. 'sqlite' => 'Cake\Database\Driver\Sqlite',
  42. 'sqlserver' => 'Cake\Database\Driver\Sqlserver',
  43. ];
  44. }
  45. }
  46. /**
  47. * TestCacheStaticConfig
  48. */
  49. class TestCacheStaticConfig {
  50. use StaticConfigTrait;
  51. public static function getClassMap() {
  52. return [
  53. 'apc' => 'Cake\Cache\Engine\ApcEngine',
  54. 'file' => 'Cake\Cache\Engine\FileEngine',
  55. 'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
  56. 'null' => 'Cake\Cache\Engine\NullEngine',
  57. 'redis' => 'Cake\Cache\Engine\RedisEngine',
  58. 'wincache' => 'Cake\Cache\Engine\WincacheEngine',
  59. 'xcache' => 'Cake\Cache\Engine\XcacheEngine',
  60. ];
  61. }
  62. }
  63. /**
  64. * TestEmailStaticConfig
  65. */
  66. class TestEmailStaticConfig {
  67. use StaticConfigTrait;
  68. public static function getClassMap() {
  69. return [
  70. 'debug' => 'Cake\Network\Email\DebugTransport',
  71. 'mail' => 'Cake\Network\Email\MailTransport',
  72. 'smtp' => 'Cake\Network\Email\SmtpTransport',
  73. ];
  74. }
  75. }
  76. /**
  77. * TestLogStaticConfig
  78. */
  79. class TestLogStaticConfig {
  80. use StaticConfigTrait;
  81. public static function getClassMap() {
  82. return [
  83. 'console' => 'Cake\Log\Engine\ConsoleLog',
  84. 'file' => 'Cake\Log\Engine\FileLog',
  85. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  86. ];
  87. }
  88. }
  89. /**
  90. * StaticConfigTraitTest class
  91. *
  92. */
  93. class StaticConfigTraitTest extends TestCase {
  94. public function setUp() {
  95. parent::setUp();
  96. $this->subject = $this->getObjectForTrait('Cake\Core\StaticConfigTrait');
  97. }
  98. public function tearDown() {
  99. unset($this->subject);
  100. parent::tearDown();
  101. }
  102. /**
  103. * Tests simple usage of parseDsn
  104. *
  105. * @return void
  106. */
  107. public function testSimpleParseDsn() {
  108. $klassName = get_class($this->subject);
  109. $this->assertSame([], $klassName::parseDsn(''));
  110. $this->assertInternalType('array', $klassName::parseDsn(['url' => 'http://:80']));
  111. $this->assertEquals(['url' => 'http://:80'], $klassName::parseDsn(['url' => 'http://:80']));
  112. $this->assertInternalType('array', $klassName::parseDsn(['url' => 'http://user@:80']));
  113. $this->assertEquals(['url' => 'http://user@:80'], $klassName::parseDsn(['url' => 'http://user@:80']));
  114. }
  115. public function testCustomParseDsn() {
  116. $dsn = 'mysql://localhost:3306/database';
  117. $expected = [
  118. 'className' => 'Cake\Database\Connection',
  119. 'driver' => 'Cake\Database\Driver\Mysql',
  120. 'host' => 'localhost',
  121. 'database' => 'database',
  122. 'port' => 3306,
  123. 'scheme' => 'mysql',
  124. ];
  125. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  126. $dsn = 'mysql://user:password@localhost:3306/database';
  127. $expected = [
  128. 'className' => 'Cake\Database\Connection',
  129. 'driver' => 'Cake\Database\Driver\Mysql',
  130. 'host' => 'localhost',
  131. 'password' => 'password',
  132. 'database' => 'database',
  133. 'port' => 3306,
  134. 'scheme' => 'mysql',
  135. 'username' => 'user',
  136. ];
  137. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  138. $dsn = 'sqlite:///memory:';
  139. $expected = [
  140. 'className' => 'Cake\Database\Connection',
  141. 'driver' => 'Cake\Database\Driver\Sqlite',
  142. 'database' => 'memory:',
  143. 'scheme' => 'sqlite',
  144. ];
  145. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  146. $dsn = 'sqlite:///?database=memory:';
  147. $expected = [
  148. 'className' => 'Cake\Database\Connection',
  149. 'driver' => 'Cake\Database\Driver\Sqlite',
  150. 'database' => 'memory:',
  151. 'scheme' => 'sqlite',
  152. ];
  153. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  154. $dsn = 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false';
  155. $expected = [
  156. 'className' => 'Cake\Database\Connection',
  157. 'driver' => 'Cake\Database\Driver\Sqlserver',
  158. 'host' => '.\SQL2012SP1',
  159. 'MultipleActiveResultSets' => false,
  160. 'password' => 'Password12!',
  161. 'database' => 'cakephp',
  162. 'scheme' => 'sqlserver',
  163. 'username' => 'sa',
  164. ];
  165. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  166. }
  167. /**
  168. * Tests className/driver value setting
  169. *
  170. * @return void
  171. */
  172. public function testParseDsnClassnameDriver() {
  173. $dsn = 'mysql://localhost:3306/database';
  174. $expected = [
  175. 'className' => 'Cake\Database\Connection',
  176. 'database' => 'database',
  177. 'driver' => 'Cake\Database\Driver\Mysql',
  178. 'host' => 'localhost',
  179. 'port' => 3306,
  180. 'scheme' => 'mysql',
  181. ];
  182. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  183. $dsn = 'mysql://user:password@localhost:3306/database';
  184. $expected = [
  185. 'className' => 'Cake\Database\Connection',
  186. 'database' => 'database',
  187. 'driver' => 'Cake\Database\Driver\Mysql',
  188. 'host' => 'localhost',
  189. 'password' => 'password',
  190. 'port' => 3306,
  191. 'scheme' => 'mysql',
  192. 'username' => 'user',
  193. ];
  194. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  195. $dsn = 'mysql://localhost/database?className=Custom\Driver';
  196. $expected = [
  197. 'className' => 'Cake\Database\Connection',
  198. 'database' => 'database',
  199. 'driver' => 'Custom\Driver',
  200. 'host' => 'localhost',
  201. 'scheme' => 'mysql',
  202. ];
  203. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  204. $dsn = 'mysql://localhost:3306/database?className=Custom\Driver';
  205. $expected = [
  206. 'className' => 'Cake\Database\Connection',
  207. 'database' => 'database',
  208. 'driver' => 'Custom\Driver',
  209. 'host' => 'localhost',
  210. 'scheme' => 'mysql',
  211. 'port' => 3306,
  212. ];
  213. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  214. $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  215. $expected = [
  216. 'className' => 'Cake\Database\Connection',
  217. 'database' => 'database',
  218. 'driver' => 'Cake\Database\Driver\Mysql',
  219. 'host' => 'localhost',
  220. 'scheme' => 'Cake\Database\Connection',
  221. 'port' => 3306,
  222. ];
  223. $this->assertEquals($expected, TestConnectionManagerStaticConfig::parseDsn($dsn));
  224. }
  225. /**
  226. * Tests parsing querystring values
  227. *
  228. * @return void
  229. */
  230. public function testParseDsnQuerystring() {
  231. $dsn = 'file:///?url=test';
  232. $expected = [
  233. 'className' => 'Cake\Log\Engine\FileLog',
  234. 'path' => '/',
  235. 'scheme' => 'file',
  236. 'url' => 'test',
  237. ];
  238. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  239. $dsn = 'file:///?file=debug&key=value';
  240. $expected = [
  241. 'className' => 'Cake\Log\Engine\FileLog',
  242. 'file' => 'debug',
  243. 'key' => 'value',
  244. 'path' => '/',
  245. 'scheme' => 'file',
  246. ];
  247. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  248. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  249. $expected = [
  250. 'className' => 'Cake\Log\Engine\FileLog',
  251. 'file' => 'debug',
  252. 'path' => '/tmp',
  253. 'scheme' => 'file',
  254. 'types' => ['notice', 'info', 'debug'],
  255. ];
  256. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  257. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  258. $expected = [
  259. 'className' => 'Cake\Network\Email\MailTransport',
  260. 'client' => null,
  261. 'key' => true,
  262. 'key2' => false,
  263. 'path' => '/',
  264. 'scheme' => 'mail',
  265. 'timeout' => '30',
  266. 'tls' => null,
  267. ];
  268. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  269. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  270. $expected = [
  271. 'className' => 'Cake\Network\Email\MailTransport',
  272. 'client' => null,
  273. 'host' => 'null',
  274. 'key' => true,
  275. 'key2' => false,
  276. 'password' => 'false',
  277. 'path' => '/1',
  278. 'scheme' => 'mail',
  279. 'timeout' => '30',
  280. 'tls' => null,
  281. 'username' => 'true',
  282. ];
  283. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  284. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
  285. $expected = [
  286. 'className' => 'Cake\Network\Email\MailTransport',
  287. 'client' => null,
  288. 'host' => 'localhost',
  289. 'password' => 'secret',
  290. 'port' => 25,
  291. 'scheme' => 'mail',
  292. 'timeout' => '30',
  293. 'tls' => null,
  294. 'username' => 'user',
  295. ];
  296. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  297. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  298. $expected = [
  299. 'className' => 'Cake\Log\Engine\FileLog',
  300. 'duration' => '+2 minutes',
  301. 'path' => '/',
  302. 'prefix' => 'myapp_cake_core_',
  303. 'scheme' => 'file',
  304. 'serialize' => true,
  305. ];
  306. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  307. }
  308. /**
  309. * Tests loading a single plugin
  310. *
  311. * @return void
  312. */
  313. public function testParseDsnPathSetting() {
  314. $dsn = 'file:///';
  315. $expected = [
  316. 'className' => 'Cake\Log\Engine\FileLog',
  317. 'path' => '/',
  318. 'scheme' => 'file',
  319. ];
  320. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  321. $dsn = 'file:///?path=/tmp/persistent/';
  322. $expected = [
  323. 'className' => 'Cake\Log\Engine\FileLog',
  324. 'path' => '/tmp/persistent/',
  325. 'scheme' => 'file',
  326. ];
  327. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  328. }
  329. }