InstanceConfigTraitTest.php 12 KB

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