InstanceConfigTraitTest.php 14 KB

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