StaticConfigTraitTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.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. use PHPUnit_Framework_Test;
  18. /**
  19. * StaticConfigTraitTest class
  20. *
  21. */
  22. class StaticConfigTraitTest extends TestCase {
  23. public function setUp() {
  24. parent::setUp();
  25. $this->subject = $this->getObjectForTrait('Cake\Core\StaticConfigTrait');
  26. }
  27. public function tearDown() {
  28. unset($this->subject);
  29. parent::tearDown();
  30. }
  31. /**
  32. * Tests simple usage of parseDsn
  33. *
  34. * @return void
  35. */
  36. public function testSimpleParseDsn() {
  37. $klassName = get_class($this->subject);
  38. $this->assertInternalType('string', $klassName::parseDsn(''));
  39. $this->assertEquals('', $klassName::parseDsn(''));
  40. $this->assertInternalType('array', $klassName::parseDsn(['key' => 'value']));
  41. $this->assertEquals(['key' => 'value'], $klassName::parseDsn(['key' => 'value']));
  42. $this->assertInternalType('array', $klassName::parseDsn(['url' => 'http://:80']));
  43. $this->assertEquals(['url' => 'http://:80'], $klassName::parseDsn(['url' => 'http://:80']));
  44. $this->assertInternalType('array', $klassName::parseDsn(['url' => 'http://user@:80']));
  45. $this->assertEquals(['url' => 'http://user@:80'], $klassName::parseDsn(['url' => 'http://user@:80']));
  46. $dsn = 'mysql://localhost:3306/database';
  47. $expected = [
  48. 'className' => 'mysql',
  49. 'driver' => 'mysql',
  50. 'host' => 'localhost',
  51. 'path' => '/database',
  52. 'port' => 3306,
  53. ];
  54. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  55. $dsn = 'mysql://user:password@localhost:3306/database';
  56. $expected = [
  57. 'className' => 'mysql',
  58. 'driver' => 'mysql',
  59. 'host' => 'localhost',
  60. 'password' => 'password',
  61. 'path' => '/database',
  62. 'port' => 3306,
  63. 'username' => 'user',
  64. ];
  65. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  66. $dsn = 'Cake\Database\Driver\Sqlite:///memory:';
  67. $expected = [
  68. 'className' => 'Cake\Database\Driver\Sqlite',
  69. 'driver' => 'Cake\Database\Driver\Sqlite',
  70. 'path' => '/memory:',
  71. ];
  72. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  73. $dsn = 'Cake\Database\Driver\Sqlite:///?database=memory:';
  74. $expected = [
  75. 'className' => 'Cake\Database\Driver\Sqlite',
  76. 'driver' => 'Cake\Database\Driver\Sqlite',
  77. 'database' => 'memory:',
  78. 'path' => '/',
  79. ];
  80. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  81. }
  82. /**
  83. * Tests className/driver value setting
  84. *
  85. * @return void
  86. */
  87. public function testParseDsnClassnameDriver() {
  88. $klassName = get_class($this->subject);
  89. $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database';
  90. $expected = [
  91. 'className' => 'Cake\Database\Driver\Mysql',
  92. 'driver' => 'Cake\Database\Driver\Mysql',
  93. 'host' => 'localhost',
  94. 'path' => '/database',
  95. 'port' => 3306,
  96. ];
  97. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  98. $dsn = 'Cake\Database\Driver\Mysql://user:password@localhost:3306/database';
  99. $expected = [
  100. 'className' => 'Cake\Database\Driver\Mysql',
  101. 'driver' => 'Cake\Database\Driver\Mysql',
  102. 'host' => 'localhost',
  103. 'password' => 'password',
  104. 'path' => '/database',
  105. 'port' => 3306,
  106. 'username' => 'user',
  107. ];
  108. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  109. $dsn = 'Cake\Database\Driver\Mysql://localhost/database?className=Cake\Database\Connection';
  110. $expected = [
  111. 'className' => 'Cake\Database\Connection',
  112. 'driver' => 'Cake\Database\Driver\Mysql',
  113. 'host' => 'localhost',
  114. 'path' => '/database',
  115. ];
  116. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  117. $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
  118. $expected = [
  119. 'className' => 'Cake\Database\Connection',
  120. 'driver' => 'Cake\Database\Driver\Mysql',
  121. 'host' => 'localhost',
  122. 'path' => '/database',
  123. 'port' => 3306,
  124. ];
  125. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  126. $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  127. $expected = [
  128. 'className' => 'Cake\Database\Connection',
  129. 'driver' => 'Cake\Database\Driver\Mysql',
  130. 'host' => 'localhost',
  131. 'path' => '/database',
  132. 'port' => 3306,
  133. ];
  134. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  135. }
  136. /**
  137. * Tests parsing querystring values
  138. *
  139. * @return void
  140. */
  141. public function testParseDsnQuerystring() {
  142. $klassName = get_class($this->subject);
  143. $expected = [
  144. 'className' => 'Cake\Log\Engine\FileLog',
  145. 'driver' => 'Cake\Log\Engine\FileLog',
  146. 'url' => 'test',
  147. 'path' => '/',
  148. ];
  149. $dsn = 'Cake\Log\Engine\FileLog:///?url=test';
  150. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  151. $expected = [
  152. 'className' => 'Cake\Log\Engine\FileLog',
  153. 'driver' => 'Cake\Log\Engine\FileLog',
  154. 'file' => 'debug',
  155. 'path' => '/',
  156. 'key' => 'value',
  157. ];
  158. $dsn = 'Cake\Log\Engine\FileLog:///?file=debug&key=value';
  159. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  160. $expected = [
  161. 'className' => 'Cake\Log\Engine\FileLog',
  162. 'driver' => 'Cake\Log\Engine\FileLog',
  163. 'file' => 'debug',
  164. 'path' => '/tmp',
  165. 'types' => ['notice', 'info', 'debug'],
  166. ];
  167. $dsn = 'Cake\Log\Engine\FileLog:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  168. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  169. $expected = [
  170. 'className' => 'Mail',
  171. 'client' => null,
  172. 'driver' => 'Mail',
  173. 'key' => true,
  174. 'key2' => false,
  175. 'path' => '/',
  176. 'timeout' =>'30',
  177. 'tls' => null,
  178. ];
  179. $dsn = 'Mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  180. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  181. $expected = [
  182. 'className' => 'Mail',
  183. 'client' => null,
  184. 'driver' => 'Mail',
  185. 'host' => 'null',
  186. 'key' => true,
  187. 'key2' => false,
  188. 'password' => 'false',
  189. 'path' => '/1',
  190. 'timeout' =>'30',
  191. 'tls' => null,
  192. 'username' => 'true',
  193. ];
  194. $dsn = 'Mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  195. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  196. $expected = [
  197. 'className' => 'Mail',
  198. 'client' => null,
  199. 'driver' => 'Mail',
  200. 'host' => 'localhost',
  201. 'password' => 'secret',
  202. 'port' => 25,
  203. 'timeout' =>'30',
  204. 'tls' => null,
  205. 'username' => 'user',
  206. ];
  207. $dsn = 'Mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
  208. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  209. $dsn = 'File:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  210. $expected = [
  211. 'className' => 'File',
  212. 'driver' => 'File',
  213. 'duration' => '+2 minutes',
  214. 'path' => '/',
  215. 'prefix' => 'myapp_cake_core_',
  216. 'serialize' => true,
  217. ];
  218. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  219. }
  220. /**
  221. * Tests loading a single plugin
  222. *
  223. * @return void
  224. */
  225. public function testParseDsnPathSetting() {
  226. $klassName = get_class($this->subject);
  227. $dsn = 'File:///';
  228. $expected = [
  229. 'className' => 'File',
  230. 'driver' => 'File',
  231. 'path' => '/',
  232. ];
  233. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  234. $dsn = 'File:///?path=/tmp/persistent/';
  235. $expected = [
  236. 'className' => 'File',
  237. 'driver' => 'File',
  238. 'path' => '/tmp/persistent/',
  239. ];
  240. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  241. }
  242. }