StaticConfigTraitTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Core\StaticConfigTrait;
  17. use Cake\TestSuite\TestCase;
  18. use InvalidArgumentException;
  19. use TestApp\Config\TestEmailStaticConfig;
  20. use TestApp\Config\TestLogStaticConfig;
  21. use TypeError;
  22. /**
  23. * StaticConfigTraitTest class
  24. */
  25. class StaticConfigTraitTest extends TestCase
  26. {
  27. /**
  28. * @var object
  29. */
  30. protected $subject;
  31. /**
  32. * setup method
  33. *
  34. * @return void
  35. */
  36. public function setUp(): void
  37. {
  38. parent::setUp();
  39. $this->subject = $this->getObjectForTrait(StaticConfigTrait::class);
  40. }
  41. /**
  42. * teardown method
  43. *
  44. * @return void
  45. */
  46. public function tearDown(): void
  47. {
  48. unset($this->subject);
  49. parent::tearDown();
  50. }
  51. /**
  52. * Tests simple usage of parseDsn
  53. *
  54. * @return void
  55. */
  56. public function testSimpleParseDsn()
  57. {
  58. $className = get_class($this->subject);
  59. $this->assertSame([], $className::parseDsn(''));
  60. }
  61. /**
  62. * Tests that failing to pass a string to parseDsn will throw an exception
  63. *
  64. * @return void
  65. */
  66. public function testParseBadType()
  67. {
  68. $this->expectException(TypeError::class);
  69. $className = get_class($this->subject);
  70. $className::parseDsn(['url' => 'http://:80']);
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testGetConfigOrFail()
  76. {
  77. $className = get_class($this->subject);
  78. $className::setConfig('foo', 'bar');
  79. $result = $className::getConfigOrFail('foo');
  80. $this->assertSame('bar', $result);
  81. }
  82. /**
  83. * @return void
  84. */
  85. public function testGetConfigOrFailException()
  86. {
  87. $this->expectException(InvalidArgumentException::class);
  88. $this->expectExceptionMessage('Expected configuration `foo` not found.');
  89. $className = get_class($this->subject);
  90. $result = $className::getConfigOrFail('foo');
  91. $this->assertSame('bar', $result);
  92. }
  93. /**
  94. * Tests parsing querystring values
  95. *
  96. * @return void
  97. */
  98. public function testParseDsnQuerystring()
  99. {
  100. $dsn = 'file:///?url=test';
  101. $expected = [
  102. 'className' => 'Cake\Log\Engine\FileLog',
  103. 'path' => '/',
  104. 'scheme' => 'file',
  105. 'url' => 'test',
  106. ];
  107. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  108. $dsn = 'file:///?file=debug&key=value';
  109. $expected = [
  110. 'className' => 'Cake\Log\Engine\FileLog',
  111. 'file' => 'debug',
  112. 'key' => 'value',
  113. 'path' => '/',
  114. 'scheme' => 'file',
  115. ];
  116. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  117. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  118. $expected = [
  119. 'className' => 'Cake\Log\Engine\FileLog',
  120. 'file' => 'debug',
  121. 'path' => '/tmp',
  122. 'scheme' => 'file',
  123. 'types' => ['notice', 'info', 'debug'],
  124. ];
  125. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  126. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  127. $expected = [
  128. 'className' => 'Cake\Mailer\Transport\MailTransport',
  129. 'client' => null,
  130. 'key' => true,
  131. 'key2' => false,
  132. 'path' => '/',
  133. 'scheme' => 'mail',
  134. 'timeout' => '30',
  135. 'tls' => null,
  136. ];
  137. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  138. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  139. $expected = [
  140. 'className' => 'Cake\Mailer\Transport\MailTransport',
  141. 'client' => null,
  142. 'host' => 'null',
  143. 'key' => true,
  144. 'key2' => false,
  145. 'password' => 'false',
  146. 'path' => '/1',
  147. 'scheme' => 'mail',
  148. 'timeout' => '30',
  149. 'tls' => null,
  150. 'username' => 'true',
  151. ];
  152. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  153. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null#fragment';
  154. $expected = [
  155. 'className' => 'Cake\Mailer\Transport\MailTransport',
  156. 'client' => null,
  157. 'host' => 'localhost',
  158. 'password' => 'secret',
  159. 'port' => 25,
  160. 'scheme' => 'mail',
  161. 'timeout' => '30',
  162. 'tls' => null,
  163. 'username' => 'user',
  164. 'fragment' => 'fragment',
  165. ];
  166. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  167. $dsn = 'file:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  168. $expected = [
  169. 'className' => 'Cake\Log\Engine\FileLog',
  170. 'duration' => '+2 minutes',
  171. 'path' => '/',
  172. 'prefix' => 'myapp_cake_core_',
  173. 'scheme' => 'file',
  174. 'serialize' => true,
  175. ];
  176. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  177. }
  178. /**
  179. * Tests loading a single plugin
  180. *
  181. * @return void
  182. */
  183. public function testParseDsnPathSetting()
  184. {
  185. $dsn = 'file:///?path=/tmp/persistent/';
  186. $expected = [
  187. 'className' => 'Cake\Log\Engine\FileLog',
  188. 'path' => '/tmp/persistent/',
  189. 'scheme' => 'file',
  190. ];
  191. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  192. }
  193. }