InstanceConfigTraitTest.php 14 KB

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