StaticConfigTraitTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. $dsn = 'Cake\Database\Driver\Sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false';
  82. $expected = [
  83. 'className' => 'Cake\Database\Driver\Sqlserver',
  84. 'driver' => 'Cake\Database\Driver\Sqlserver',
  85. 'host' => '.\SQL2012SP1',
  86. 'MultipleActiveResultSets' => false,
  87. 'password' => 'Password12!',
  88. 'path' => '/cakephp',
  89. 'username' => 'sa',
  90. ];
  91. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  92. }
  93. /**
  94. * Tests className/driver value setting
  95. *
  96. * @return void
  97. */
  98. public function testParseDsnClassnameDriver() {
  99. $klassName = get_class($this->subject);
  100. $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database';
  101. $expected = [
  102. 'className' => 'Cake\Database\Driver\Mysql',
  103. 'driver' => 'Cake\Database\Driver\Mysql',
  104. 'host' => 'localhost',
  105. 'path' => '/database',
  106. 'port' => 3306,
  107. ];
  108. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  109. $dsn = 'Cake\Database\Driver\Mysql://user:password@localhost:3306/database';
  110. $expected = [
  111. 'className' => 'Cake\Database\Driver\Mysql',
  112. 'driver' => 'Cake\Database\Driver\Mysql',
  113. 'host' => 'localhost',
  114. 'password' => 'password',
  115. 'path' => '/database',
  116. 'port' => 3306,
  117. 'username' => 'user',
  118. ];
  119. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  120. $dsn = 'Cake\Database\Driver\Mysql://localhost/database?className=Cake\Database\Connection';
  121. $expected = [
  122. 'className' => 'Cake\Database\Connection',
  123. 'driver' => 'Cake\Database\Driver\Mysql',
  124. 'host' => 'localhost',
  125. 'path' => '/database',
  126. ];
  127. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  128. $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
  129. $expected = [
  130. 'className' => 'Cake\Database\Connection',
  131. 'driver' => 'Cake\Database\Driver\Mysql',
  132. 'host' => 'localhost',
  133. 'path' => '/database',
  134. 'port' => 3306,
  135. ];
  136. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  137. $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  138. $expected = [
  139. 'className' => 'Cake\Database\Connection',
  140. 'driver' => 'Cake\Database\Driver\Mysql',
  141. 'host' => 'localhost',
  142. 'path' => '/database',
  143. 'port' => 3306,
  144. ];
  145. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  146. }
  147. /**
  148. * Tests parsing querystring values
  149. *
  150. * @return void
  151. */
  152. public function testParseDsnQuerystring() {
  153. $klassName = get_class($this->subject);
  154. $expected = [
  155. 'className' => 'Cake\Log\Engine\FileLog',
  156. 'driver' => 'Cake\Log\Engine\FileLog',
  157. 'url' => 'test',
  158. 'path' => '/',
  159. ];
  160. $dsn = 'Cake\Log\Engine\FileLog:///?url=test';
  161. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  162. $expected = [
  163. 'className' => 'Cake\Log\Engine\FileLog',
  164. 'driver' => 'Cake\Log\Engine\FileLog',
  165. 'file' => 'debug',
  166. 'path' => '/',
  167. 'key' => 'value',
  168. ];
  169. $dsn = 'Cake\Log\Engine\FileLog:///?file=debug&key=value';
  170. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  171. $expected = [
  172. 'className' => 'Cake\Log\Engine\FileLog',
  173. 'driver' => 'Cake\Log\Engine\FileLog',
  174. 'file' => 'debug',
  175. 'path' => '/tmp',
  176. 'types' => ['notice', 'info', 'debug'],
  177. ];
  178. $dsn = 'Cake\Log\Engine\FileLog:///tmp?file=debug&types[]=notice&types[]=info&types[]=debug';
  179. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  180. $expected = [
  181. 'className' => 'Mail',
  182. 'client' => null,
  183. 'driver' => 'Mail',
  184. 'key' => true,
  185. 'key2' => false,
  186. 'path' => '/',
  187. 'timeout' => '30',
  188. 'tls' => null,
  189. ];
  190. $dsn = 'Mail:///?timeout=30&key=true&key2=false&client=null&tls=null';
  191. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  192. $expected = [
  193. 'className' => 'Mail',
  194. 'client' => null,
  195. 'driver' => 'Mail',
  196. 'host' => 'null',
  197. 'key' => true,
  198. 'key2' => false,
  199. 'password' => 'false',
  200. 'path' => '/1',
  201. 'timeout' => '30',
  202. 'tls' => null,
  203. 'username' => 'true',
  204. ];
  205. $dsn = 'Mail://true:false@null/1?timeout=30&key=true&key2=false&client=null&tls=null';
  206. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  207. $expected = [
  208. 'className' => 'Mail',
  209. 'client' => null,
  210. 'driver' => 'Mail',
  211. 'host' => 'localhost',
  212. 'password' => 'secret',
  213. 'port' => 25,
  214. 'timeout' => '30',
  215. 'tls' => null,
  216. 'username' => 'user',
  217. ];
  218. $dsn = 'Mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
  219. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  220. $dsn = 'File:///?prefix=myapp_cake_core_&serialize=true&duration=%2B2 minutes';
  221. $expected = [
  222. 'className' => 'File',
  223. 'driver' => 'File',
  224. 'duration' => '+2 minutes',
  225. 'path' => '/',
  226. 'prefix' => 'myapp_cake_core_',
  227. 'serialize' => true,
  228. ];
  229. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  230. }
  231. /**
  232. * Tests loading a single plugin
  233. *
  234. * @return void
  235. */
  236. public function testParseDsnPathSetting() {
  237. $klassName = get_class($this->subject);
  238. $dsn = 'File:///';
  239. $expected = [
  240. 'className' => 'File',
  241. 'driver' => 'File',
  242. 'path' => '/',
  243. ];
  244. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  245. $dsn = 'File:///?path=/tmp/persistent/';
  246. $expected = [
  247. 'className' => 'File',
  248. 'driver' => 'File',
  249. 'path' => '/tmp/persistent/',
  250. ];
  251. $this->assertEquals($expected, $klassName::parseDsn(['url' => $dsn]));
  252. }
  253. }