StaticConfigTraitTest.php 5.2 KB

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