InstanceConfigTraitTest.php 14 KB

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