StaticConfigTraitTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://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. * TestCacheStaticConfig
  19. */
  20. class TestCacheStaticConfig
  21. {
  22. use StaticConfigTrait;
  23. /**
  24. * Cache driver class map.
  25. *
  26. * @var array
  27. */
  28. protected static $_dsnClassMap = [
  29. 'apc' => 'Cake\Cache\Engine\ApcuEngine', // @deprecated in 3.6. Use apcu instead.
  30. 'apcu' => 'Cake\Cache\Engine\ApcuEngine',
  31. 'file' => 'Cake\Cache\Engine\FileEngine',
  32. 'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
  33. 'null' => 'Cake\Cache\Engine\NullEngine',
  34. 'redis' => 'Cake\Cache\Engine\RedisEngine',
  35. 'wincache' => 'Cake\Cache\Engine\WincacheEngine',
  36. 'xcache' => 'Cake\Cache\Engine\XcacheEngine',
  37. ];
  38. }
  39. /**
  40. * TestEmailStaticConfig
  41. */
  42. class TestEmailStaticConfig
  43. {
  44. use StaticConfigTrait;
  45. /**
  46. * Email driver class map.
  47. *
  48. * @var array
  49. */
  50. protected static $_dsnClassMap = [
  51. 'debug' => 'Cake\Mailer\Transport\DebugTransport',
  52. 'mail' => 'Cake\Mailer\Transport\MailTransport',
  53. 'smtp' => 'Cake\Mailer\Transport\SmtpTransport',
  54. ];
  55. }
  56. /**
  57. * TestLogStaticConfig
  58. */
  59. class TestLogStaticConfig
  60. {
  61. use StaticConfigTrait;
  62. /**
  63. * Log engine class map.
  64. *
  65. * @var array
  66. */
  67. protected static $_dsnClassMap = [
  68. 'console' => 'Cake\Log\Engine\ConsoleLog',
  69. 'file' => 'Cake\Log\Engine\FileLog',
  70. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  71. ];
  72. }
  73. /**
  74. * StaticConfigTraitTest class
  75. */
  76. class StaticConfigTraitTest extends TestCase
  77. {
  78. /**
  79. * setup method
  80. *
  81. * @return void
  82. */
  83. public function setUp()
  84. {
  85. parent::setUp();
  86. $this->subject = $this->getObjectForTrait('Cake\Core\StaticConfigTrait');
  87. }
  88. /**
  89. * teardown method
  90. *
  91. * @return void
  92. */
  93. public function tearDown()
  94. {
  95. unset($this->subject);
  96. parent::tearDown();
  97. }
  98. /**
  99. * Tests simple usage of parseDsn
  100. *
  101. * @return void
  102. */
  103. public function testSimpleParseDsn()
  104. {
  105. $className = get_class($this->subject);
  106. $this->assertSame([], $className::parseDsn(''));
  107. }
  108. /**
  109. * Tests that failing to pass a string to parseDsn will throw an exception
  110. *
  111. * @return void
  112. */
  113. public function testParseBadType()
  114. {
  115. $this->expectException(\InvalidArgumentException::class);
  116. $className = get_class($this->subject);
  117. $className::parseDsn(['url' => 'http://:80']);
  118. }
  119. /**
  120. * Tests parsing querystring values
  121. *
  122. * @return void
  123. */
  124. public function testParseDsnQuerystring()
  125. {
  126. $dsn = 'file:///?url=test';
  127. $expected = [
  128. 'className' => 'Cake\Log\Engine\FileLog',
  129. 'path' => '/',
  130. 'scheme' => 'file',
  131. 'url' => 'test',
  132. ];
  133. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  134. $dsn = 'file:///?file=debug&key=value';
  135. $expected = [
  136. 'className' => 'Cake\Log\Engine\FileLog',
  137. 'file' => 'debug',
  138. 'key' => 'value',
  139. 'path' => '/',
  140. 'scheme' => 'file',
  141. ];
  142. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  143. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  144. $expected = [
  145. 'className' => 'Cake\Log\Engine\FileLog',
  146. 'file' => 'debug',
  147. 'path' => '/tmp',
  148. 'scheme' => 'file',
  149. 'types' => ['notice', 'info', 'debug'],
  150. ];
  151. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  152. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  153. $expected = [
  154. 'className' => 'Cake\Mailer\Transport\MailTransport',
  155. 'client' => null,
  156. 'key' => true,
  157. 'key2' => false,
  158. 'path' => '/',
  159. 'scheme' => 'mail',
  160. 'timeout' => '30',
  161. 'tls' => null,
  162. ];
  163. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  164. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  165. $expected = [
  166. 'className' => 'Cake\Mailer\Transport\MailTransport',
  167. 'client' => null,
  168. 'host' => 'null',
  169. 'key' => true,
  170. 'key2' => false,
  171. 'password' => 'false',
  172. 'path' => '/1',
  173. 'scheme' => 'mail',
  174. 'timeout' => '30',
  175. 'tls' => null,
  176. 'username' => 'true',
  177. ];
  178. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  179. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null#fragment';
  180. $expected = [
  181. 'className' => 'Cake\Mailer\Transport\MailTransport',
  182. 'client' => null,
  183. 'host' => 'localhost',
  184. 'password' => 'secret',
  185. 'port' => 25,
  186. 'scheme' => 'mail',
  187. 'timeout' => '30',
  188. 'tls' => null,
  189. 'username' => 'user',
  190. 'fragment' => 'fragment',
  191. ];
  192. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  193. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  194. $expected = [
  195. 'className' => 'Cake\Log\Engine\FileLog',
  196. 'duration' => '+2 minutes',
  197. 'path' => '/',
  198. 'prefix' => 'myapp_cake_core_',
  199. 'scheme' => 'file',
  200. 'serialize' => true,
  201. ];
  202. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  203. }
  204. /**
  205. * Tests loading a single plugin
  206. *
  207. * @return void
  208. */
  209. public function testParseDsnPathSetting()
  210. {
  211. $dsn = 'file:///?path=/tmp/persistent/';
  212. $expected = [
  213. 'className' => 'Cake\Log\Engine\FileLog',
  214. 'path' => '/tmp/persistent/',
  215. 'scheme' => 'file',
  216. ];
  217. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  218. }
  219. /**
  220. * Test that the dsn map can be updated/append to
  221. *
  222. * @return void
  223. */
  224. public function testCanUpdateClassMap()
  225. {
  226. $this->deprecated(function () {
  227. $expected = [
  228. 'console' => 'Cake\Log\Engine\ConsoleLog',
  229. 'file' => 'Cake\Log\Engine\FileLog',
  230. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  231. ];
  232. $result = TestLogStaticConfig::getdsnClassMap();
  233. $this->assertEquals($expected, $result, 'The class map should match the class property');
  234. $expected = [
  235. 'console' => 'Special\EngineLog',
  236. 'file' => 'Cake\Log\Engine\FileLog',
  237. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  238. ];
  239. $result = TestLogStaticConfig::dsnClassMap(['console' => 'Special\EngineLog']);
  240. $this->assertEquals($expected, $result, 'Should be possible to change the map');
  241. $expected = [
  242. 'console' => 'Special\EngineLog',
  243. 'file' => 'Cake\Log\Engine\FileLog',
  244. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  245. 'my' => 'Special\OtherLog'
  246. ];
  247. $result = TestLogStaticConfig::dsnClassMap(['my' => 'Special\OtherLog']);
  248. $this->assertEquals($expected, $result, 'Should be possible to add to the map');
  249. });
  250. }
  251. /**
  252. * Tests that former handling of integer keys coming in from PHP internal conversions
  253. * won't break in 3.4.
  254. *
  255. * @return void
  256. */
  257. public function testConfigBC()
  258. {
  259. $this->deprecated(function () {
  260. $result = TestLogStaticConfig::config(404);
  261. $this->assertNull($result);
  262. });
  263. }
  264. }