InstanceConfigTraitTest.php 13 KB

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