InstanceConfigTraitTest.php 14 KB

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