StaticConfigTraitTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->subject = new class {
  38. use StaticConfigTrait;
  39. };
  40. }
  41. /**
  42. * teardown method
  43. */
  44. public function tearDown(): void
  45. {
  46. unset($this->subject);
  47. parent::tearDown();
  48. }
  49. /**
  50. * Tests simple usage of parseDsn
  51. */
  52. public function testSimpleParseDsn(): void
  53. {
  54. $className = get_class($this->subject);
  55. $this->assertSame([], $className::parseDsn(''));
  56. }
  57. /**
  58. * Tests that failing to pass a string to parseDsn will throw an exception
  59. */
  60. public function testParseBadType(): void
  61. {
  62. $this->expectException(TypeError::class);
  63. $className = get_class($this->subject);
  64. $className::parseDsn(['url' => 'http://:80']);
  65. }
  66. public function testSetConfigValues(): void
  67. {
  68. $className = get_class($this->subject);
  69. $className::setConfig('foo', true);
  70. $result = $className::getConfigOrFail('foo');
  71. $this->assertTrue($result);
  72. $className::setConfig('bar', 'value');
  73. $result = $className::getConfigOrFail('bar');
  74. $this->assertSame('value', $result);
  75. }
  76. public function testGetConfigOrFail(): void
  77. {
  78. $className = get_class($this->subject);
  79. $className::setConfig('foo2', ['bar' => true]);
  80. $result = $className::getConfigOrFail('foo2');
  81. $this->assertSame(['bar' => true], $result);
  82. }
  83. public function testGetConfigOrFailException(): void
  84. {
  85. $this->expectException(InvalidArgumentException::class);
  86. $this->expectExceptionMessage('Expected configuration `unknown` not found.');
  87. $className = get_class($this->subject);
  88. $className::getConfigOrFail('unknown');
  89. }
  90. /**
  91. * Tests parsing querystring values
  92. */
  93. public function testParseDsnQuerystring(): void
  94. {
  95. $dsn = 'file:///?url=test';
  96. $expected = [
  97. 'className' => 'Cake\Log\Engine\FileLog',
  98. 'path' => '/',
  99. 'scheme' => 'file',
  100. 'url' => 'test',
  101. ];
  102. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  103. $dsn = 'file:///?file=debug&key=value';
  104. $expected = [
  105. 'className' => 'Cake\Log\Engine\FileLog',
  106. 'file' => 'debug',
  107. 'key' => 'value',
  108. 'path' => '/',
  109. 'scheme' => 'file',
  110. ];
  111. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  112. $dsn = 'file:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  113. $expected = [
  114. 'className' => 'Cake\Log\Engine\FileLog',
  115. 'file' => 'debug',
  116. 'path' => '/tmp',
  117. 'scheme' => 'file',
  118. 'types' => ['notice', 'info', 'debug'],
  119. ];
  120. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  121. $dsn = 'mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  122. $expected = [
  123. 'className' => 'Cake\Mailer\Transport\MailTransport',
  124. 'client' => null,
  125. 'key' => true,
  126. 'key2' => false,
  127. 'path' => '/',
  128. 'scheme' => 'mail',
  129. 'timeout' => '30',
  130. 'tls' => null,
  131. ];
  132. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  133. $dsn = 'mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  134. $expected = [
  135. 'className' => 'Cake\Mailer\Transport\MailTransport',
  136. 'client' => null,
  137. 'host' => 'null',
  138. 'key' => true,
  139. 'key2' => false,
  140. 'password' => 'false',
  141. 'path' => '/1',
  142. 'scheme' => 'mail',
  143. 'timeout' => '30',
  144. 'tls' => null,
  145. 'username' => 'true',
  146. ];
  147. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  148. $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null#fragment';
  149. $expected = [
  150. 'className' => 'Cake\Mailer\Transport\MailTransport',
  151. 'client' => null,
  152. 'host' => 'localhost',
  153. 'password' => 'secret',
  154. 'port' => 25,
  155. 'scheme' => 'mail',
  156. 'timeout' => '30',
  157. 'tls' => null,
  158. 'username' => 'user',
  159. 'fragment' => 'fragment',
  160. ];
  161. $this->assertEquals($expected, TestEmailStaticConfig::parseDsn($dsn));
  162. $dsn = 'file:///?prefix=myapp_cake_translations_&serialize=true&duration=%2B2 minutes';
  163. $expected = [
  164. 'className' => 'Cake\Log\Engine\FileLog',
  165. 'duration' => '+2 minutes',
  166. 'path' => '/',
  167. 'prefix' => 'myapp_cake_translations_',
  168. 'scheme' => 'file',
  169. 'serialize' => true,
  170. ];
  171. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  172. }
  173. /**
  174. * Tests loading a single plugin
  175. */
  176. public function testParseDsnPathSetting(): void
  177. {
  178. $dsn = 'file:///?path=/tmp/persistent/';
  179. $expected = [
  180. 'className' => 'Cake\Log\Engine\FileLog',
  181. 'path' => '/tmp/persistent/',
  182. 'scheme' => 'file',
  183. ];
  184. $this->assertEquals($expected, TestLogStaticConfig::parseDsn($dsn));
  185. }
  186. }