InstanceConfigTraitTest.php 14 KB

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