StaticConfigTraitTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. * @expectedException \InvalidArgumentException
  111. * @return void
  112. */
  113. public function testParseBadType()
  114. {
  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';
  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. ];
  190. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  191. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  192. $expected = [
  193. 'className' => 'Cake\Log\Engine\FileLog',
  194. 'duration' => '+2 minutes',
  195. 'path' => '/',
  196. 'prefix' => 'myapp_cake_core_',
  197. 'scheme' => 'file',
  198. 'serialize' => true,
  199. ];
  200. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  201. }
  202. /**
  203. * Tests loading a single plugin
  204. *
  205. * @return void
  206. */
  207. public function testParseDsnPathSetting()
  208. {
  209. $dsn = 'file:///?path=/tmp/persistent/';
  210. $expected = [
  211. 'className' => 'Cake\Log\Engine\FileLog',
  212. 'path' => '/tmp/persistent/',
  213. 'scheme' => 'file',
  214. ];
  215. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  216. }
  217. /**
  218. * Test that the dsn map can be updated/append to
  219. *
  220. * @return void
  221. */
  222. public function testCanUpdateClassMap()
  223. {
  224. $expected = [
  225. 'console' => 'Cake\Log\Engine\ConsoleLog',
  226. 'file' => 'Cake\Log\Engine\FileLog',
  227. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  228. ];
  229. $result = TestLogStaticConfig::dsnClassMap();
  230. $this->assertEquals($expected, $result, 'The class map should match the class property');
  231. $expected = [
  232. 'console' => 'Special\EngineLog',
  233. 'file' => 'Cake\Log\Engine\FileLog',
  234. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  235. ];
  236. $result = TestLogStaticConfig::dsnClassMap(['console' => 'Special\EngineLog']);
  237. $this->assertEquals($expected, $result, 'Should be possible to change the map');
  238. $expected = [
  239. 'console' => 'Special\EngineLog',
  240. 'file' => 'Cake\Log\Engine\FileLog',
  241. 'syslog' => 'Cake\Log\Engine\SyslogLog',
  242. 'my' => 'Special\OtherLog'
  243. ];
  244. $result = TestLogStaticConfig::dsnClassMap(['my' => 'Special\OtherLog']);
  245. $this->assertEquals($expected, $result, 'Should be possible to add to the map');
  246. }
  247. /**
  248. * Tests that former handling of integer keys coming in from PHP internal conversions
  249. * won't break in 3.4.
  250. *
  251. * @return void
  252. */
  253. public function testConfigBC()
  254. {
  255. $result = TestLogStaticConfig::config(404);
  256. $this->assertNull($result);
  257. }
  258. }