InstanceConfigTraitTest.php 14 KB

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