InstanceConfigTraitTest.php 14 KB

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