StaticConfigTraitTest.php 8.0 KB

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