InstanceConfigTraitTest.php 15 KB

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