InstanceConfigTraitTest.php 12 KB

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