StaticConfigTraitTest.php 8.9 KB

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