InstanceConfigTraitTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Core\InstanceConfigTrait;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * TestInstanceConfig
  20. */
  21. class TestInstanceConfig {
  22. use InstanceConfigTrait;
  23. /**
  24. * _defaultConfig
  25. *
  26. * Some default config
  27. *
  28. * @var array
  29. */
  30. protected $_defaultConfig = [
  31. 'some' => 'string',
  32. 'a' => ['nested' => 'value']
  33. ];
  34. }
  35. /**
  36. * ReadOnlyTestInstanceConfig
  37. */
  38. class ReadOnlyTestInstanceConfig {
  39. use InstanceConfigTrait;
  40. /**
  41. * _defaultConfig
  42. *
  43. * Some default config
  44. *
  45. * @var array
  46. */
  47. protected $_defaultConfig = [
  48. 'some' => 'string',
  49. 'a' => ['nested' => 'value']
  50. ];
  51. /**
  52. * Example of how to prevent modifying config at run time
  53. *
  54. * @throws \Exception
  55. * @param mixed $key
  56. * @param mixed $value
  57. * @return void
  58. */
  59. protected function _configWrite($key, $value = null) {
  60. throw new \Exception('This Instance is readonly');
  61. }
  62. }
  63. /**
  64. * InstanceConfigTraitTest
  65. *
  66. */
  67. class InstanceConfigTraitTest extends TestCase {
  68. /**
  69. * setUp method
  70. *
  71. * @return void
  72. */
  73. public function setUp() {
  74. parent::setUp();
  75. $this->object = new TestInstanceConfig();
  76. }
  77. /**
  78. * testDefaultsAreSet
  79. *
  80. * @return void
  81. */
  82. public function testDefaultsAreSet() {
  83. $this->assertSame(
  84. [
  85. 'some' => 'string',
  86. 'a' => ['nested' => 'value']
  87. ],
  88. $this->object->config(),
  89. 'runtime config should match the defaults if not overriden'
  90. );
  91. }
  92. /**
  93. * testGetSimple
  94. *
  95. * @return void
  96. */
  97. public function testGetSimple() {
  98. $this->assertSame(
  99. 'string',
  100. $this->object->config('some'),
  101. 'should return the key value only'
  102. );
  103. $this->assertSame(
  104. ['nested' => 'value'],
  105. $this->object->config('a'),
  106. 'should return the key value only'
  107. );
  108. }
  109. /**
  110. * testGetDot
  111. *
  112. * @return void
  113. */
  114. public function testGetDot() {
  115. $this->assertSame(
  116. 'value',
  117. $this->object->config('a.nested'),
  118. 'should return the nested value only'
  119. );
  120. }
  121. /**
  122. * testSetSimple
  123. *
  124. * @return void
  125. */
  126. public function testSetSimple() {
  127. $this->object->config('foo', 'bar');
  128. $this->assertSame(
  129. 'bar',
  130. $this->object->config('foo'),
  131. 'should return the same value just set'
  132. );
  133. $this->object->config('some', 'zum');
  134. $this->assertSame(
  135. 'zum',
  136. $this->object->config('some'),
  137. 'should return the overritten value'
  138. );
  139. $this->assertSame(
  140. [
  141. 'some' => 'zum',
  142. 'a' => ['nested' => 'value'],
  143. 'foo' => 'bar',
  144. ],
  145. $this->object->config(),
  146. 'updates should be merged with existing config'
  147. );
  148. }
  149. /**
  150. * testSetNested
  151. *
  152. * @return void
  153. */
  154. public function testSetNested() {
  155. $this->object->config('new.foo', 'bar');
  156. $this->assertSame(
  157. 'bar',
  158. $this->object->config('new.foo'),
  159. 'should return the same value just set'
  160. );
  161. $this->object->config('a.nested', 'zum');
  162. $this->assertSame(
  163. 'zum',
  164. $this->object->config('a.nested'),
  165. 'should return the overritten value'
  166. );
  167. $this->assertSame(
  168. [
  169. 'some' => 'string',
  170. 'a' => ['nested' => 'zum'],
  171. 'new' => ['foo' => 'bar']
  172. ],
  173. $this->object->config(),
  174. 'updates should be merged with existing config'
  175. );
  176. }
  177. /**
  178. * testSetNested
  179. *
  180. * @return void
  181. */
  182. public function testSetArray() {
  183. $this->object->config(['foo' => 'bar']);
  184. $this->assertSame(
  185. 'bar',
  186. $this->object->config('foo'),
  187. 'should return the same value just set'
  188. );
  189. $this->assertSame(
  190. [
  191. 'some' => 'string',
  192. 'a' => ['nested' => 'value'],
  193. 'foo' => 'bar',
  194. ],
  195. $this->object->config(),
  196. 'updates should be merged with existing config'
  197. );
  198. $this->object->config(['new.foo' => 'bar']);
  199. $this->assertSame(
  200. 'bar',
  201. $this->object->config('new.foo'),
  202. 'should return the same value just set'
  203. );
  204. $this->assertSame(
  205. [
  206. 'some' => 'string',
  207. 'a' => ['nested' => 'value'],
  208. 'foo' => 'bar',
  209. 'new' => ['foo' => 'bar']
  210. ],
  211. $this->object->config(),
  212. 'updates should be merged with existing config'
  213. );
  214. $this->object->config(['multiple' => 'different', 'a.values.to' => 'set']);
  215. $this->assertSame(
  216. [
  217. 'some' => 'string',
  218. 'a' => ['nested' => 'value', 'values' => ['to' => 'set']],
  219. 'foo' => 'bar',
  220. 'new' => ['foo' => 'bar'],
  221. 'multiple' => 'different'
  222. ],
  223. $this->object->config(),
  224. 'updates should be merged with existing config'
  225. );
  226. }
  227. /**
  228. * testSetClobber
  229. *
  230. * @expectedException \Exception
  231. * @expectedExceptionMessage Cannot set a.nested.value
  232. * @return void
  233. */
  234. public function testSetClobber() {
  235. $this->object->config(['a.nested.value' => 'not possible'], null, false);
  236. $result = $this->object->config();
  237. }
  238. /**
  239. * testMerge
  240. *
  241. * @return void
  242. */
  243. public function testMerge() {
  244. $this->object->config(['a' => ['nother' => 'value']]);
  245. $this->assertSame(
  246. [
  247. 'some' => 'string',
  248. 'a' => [
  249. 'nested' => 'value',
  250. 'nother' => 'value'
  251. ]
  252. ],
  253. $this->object->config(),
  254. 'Merging should not delete untouched array values'
  255. );
  256. }
  257. /**
  258. * testMergeDotKey
  259. *
  260. * @return void
  261. */
  262. public function testMergeDotKey() {
  263. $this->object->config('a.nother', 'value');
  264. $this->assertSame(
  265. [
  266. 'some' => 'string',
  267. 'a' => [
  268. 'nested' => 'value',
  269. 'nother' => 'value'
  270. ]
  271. ],
  272. $this->object->config(),
  273. 'Should act the same as having passed the equivalent array to the config function'
  274. );
  275. $this->object->config(['a.nextra' => 'value']);
  276. $this->assertSame(
  277. [
  278. 'some' => 'string',
  279. 'a' => [
  280. 'nested' => 'value',
  281. 'nother' => 'value',
  282. 'nextra' => 'value'
  283. ]
  284. ],
  285. $this->object->config(),
  286. 'Merging should not delete untouched array values'
  287. );
  288. }
  289. /**
  290. * testSetDefaultsMerge
  291. *
  292. * @return void
  293. */
  294. public function testSetDefaultsMerge() {
  295. $this->object->config(['a' => ['nother' => 'value']]);
  296. $this->assertSame(
  297. [
  298. 'some' => 'string',
  299. 'a' => [
  300. 'nested' => 'value',
  301. 'nother' => 'value'
  302. ]
  303. ],
  304. $this->object->config(),
  305. 'First access should act like any subsequent access'
  306. );
  307. }
  308. /**
  309. * testSetDefaultsNoMerge
  310. *
  311. * @return void
  312. */
  313. public function testSetDefaultsNoMerge() {
  314. $this->object->config(['a' => ['nother' => 'value']], null, false);
  315. $this->assertSame(
  316. [
  317. 'some' => 'string',
  318. 'a' => [
  319. 'nother' => 'value'
  320. ]
  321. ],
  322. $this->object->config(),
  323. 'If explicitly no-merge, array values should be overwritten'
  324. );
  325. }
  326. /**
  327. * testSetMergeNoClobber
  328. *
  329. * Merging offers no such protection of clobbering a value whilst implemented
  330. * using the Hash class
  331. *
  332. * @return void
  333. */
  334. public function testSetMergeNoClobber() {
  335. $this->object->config(['a.nested.value' => 'it is possible']);
  336. $this->assertSame(
  337. [
  338. 'some' => 'string',
  339. 'a' => [
  340. 'nested' => [
  341. 'value' => 'it is possible'
  342. ]
  343. ]
  344. ],
  345. $this->object->config(),
  346. 'When merging a scalar property will be overwritten with an array'
  347. );
  348. }
  349. /**
  350. * testReadOnlyConfig
  351. *
  352. * @expectedException \Exception
  353. * @expectedExceptionMessage This Instance is readonly
  354. * @return void
  355. */
  356. public function testReadOnlyConfig() {
  357. $object = new ReadOnlyTestInstanceConfig();
  358. $this->assertSame(
  359. [
  360. 'some' => 'string',
  361. 'a' => ['nested' => 'value']
  362. ],
  363. $object->config(),
  364. 'default config should be returned'
  365. );
  366. $object->config('throw.me', 'an exception');
  367. }
  368. /**
  369. * testDeleteSimple
  370. *
  371. * @return void
  372. */
  373. public function testDeleteSimple() {
  374. $this->object->config('foo', null);
  375. $this->assertNull(
  376. $this->object->config('foo'),
  377. 'setting a new key to null should have no effect'
  378. );
  379. $this->object->config('some', null);
  380. $this->assertNull(
  381. $this->object->config('some'),
  382. 'should delete the existing value'
  383. );
  384. $this->assertSame(
  385. [
  386. 'a' => ['nested' => 'value'],
  387. ],
  388. $this->object->config(),
  389. 'deleted keys should not be present'
  390. );
  391. }
  392. /**
  393. * testDeleteNested
  394. *
  395. * @return void
  396. */
  397. public function testDeleteNested() {
  398. $this->object->config('new.foo', null);
  399. $this->assertNull(
  400. $this->object->config('new.foo'),
  401. 'setting a new key to null should have no effect'
  402. );
  403. $this->object->config('a.nested', null);
  404. $this->assertNull(
  405. $this->object->config('a.nested'),
  406. 'should delete the existing value'
  407. );
  408. $this->assertSame(
  409. [
  410. 'some' => 'string',
  411. ],
  412. $this->object->config(),
  413. 'deleted keys should not be present'
  414. );
  415. }
  416. /**
  417. * testDeleteClobber
  418. *
  419. * @expectedException \Exception
  420. * @expectedExceptionMessage Cannot unset a.nested.value.whoops
  421. * @return void
  422. */
  423. public function testDeleteClobber() {
  424. $this->object->config('a.nested.value.whoops', null);
  425. }
  426. }