InstanceConfigTraitTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. * testSetClobber
  245. *
  246. * @expectedException \Exception
  247. * @expectedExceptionMessage Cannot set a.nested.value
  248. * @return void
  249. */
  250. public function testSetClobber()
  251. {
  252. $this->object->config(['a.nested.value' => 'not possible'], null, false);
  253. $result = $this->object->config();
  254. }
  255. /**
  256. * testMerge
  257. *
  258. * @return void
  259. */
  260. public function testMerge()
  261. {
  262. $this->object->config(['a' => ['nother' => 'value']]);
  263. $this->assertSame(
  264. [
  265. 'some' => 'string',
  266. 'a' => [
  267. 'nested' => 'value',
  268. 'nother' => 'value'
  269. ]
  270. ],
  271. $this->object->config(),
  272. 'Merging should not delete untouched array values'
  273. );
  274. }
  275. /**
  276. * testMergeDotKey
  277. *
  278. * @return void
  279. */
  280. public function testMergeDotKey()
  281. {
  282. $this->object->config('a.nother', 'value');
  283. $this->assertSame(
  284. [
  285. 'some' => 'string',
  286. 'a' => [
  287. 'nested' => 'value',
  288. 'nother' => 'value'
  289. ]
  290. ],
  291. $this->object->config(),
  292. 'Should act the same as having passed the equivalent array to the config function'
  293. );
  294. $this->object->config(['a.nextra' => 'value']);
  295. $this->assertSame(
  296. [
  297. 'some' => 'string',
  298. 'a' => [
  299. 'nested' => 'value',
  300. 'nother' => 'value',
  301. 'nextra' => 'value'
  302. ]
  303. ],
  304. $this->object->config(),
  305. 'Merging should not delete untouched array values'
  306. );
  307. }
  308. /**
  309. * testSetDefaultsMerge
  310. *
  311. * @return void
  312. */
  313. public function testSetDefaultsMerge()
  314. {
  315. $this->object->config(['a' => ['nother' => 'value']]);
  316. $this->assertSame(
  317. [
  318. 'some' => 'string',
  319. 'a' => [
  320. 'nested' => 'value',
  321. 'nother' => 'value'
  322. ]
  323. ],
  324. $this->object->config(),
  325. 'First access should act like any subsequent access'
  326. );
  327. }
  328. /**
  329. * testSetDefaultsNoMerge
  330. *
  331. * @return void
  332. */
  333. public function testSetDefaultsNoMerge()
  334. {
  335. $this->object->config(['a' => ['nother' => 'value']], null, false);
  336. $this->assertSame(
  337. [
  338. 'some' => 'string',
  339. 'a' => [
  340. 'nother' => 'value'
  341. ]
  342. ],
  343. $this->object->config(),
  344. 'If explicitly no-merge, array values should be overwritten'
  345. );
  346. }
  347. /**
  348. * testSetMergeNoClobber
  349. *
  350. * Merging offers no such protection of clobbering a value whilst implemented
  351. * using the Hash class
  352. *
  353. * @return void
  354. */
  355. public function testSetMergeNoClobber()
  356. {
  357. $this->object->config(['a.nested.value' => 'it is possible']);
  358. $this->assertSame(
  359. [
  360. 'some' => 'string',
  361. 'a' => [
  362. 'nested' => [
  363. 'value' => 'it is possible'
  364. ]
  365. ]
  366. ],
  367. $this->object->config(),
  368. 'When merging a scalar property will be overwritten with an array'
  369. );
  370. }
  371. /**
  372. * testReadOnlyConfig
  373. *
  374. * @expectedException \Exception
  375. * @expectedExceptionMessage This Instance is readonly
  376. * @return void
  377. */
  378. public function testReadOnlyConfig()
  379. {
  380. $object = new ReadOnlyTestInstanceConfig();
  381. $this->assertSame(
  382. [
  383. 'some' => 'string',
  384. 'a' => ['nested' => 'value']
  385. ],
  386. $object->config(),
  387. 'default config should be returned'
  388. );
  389. $object->config('throw.me', 'an exception');
  390. }
  391. /**
  392. * testDeleteSimple
  393. *
  394. * @return void
  395. */
  396. public function testDeleteSimple()
  397. {
  398. $this->object->config('foo', null);
  399. $this->assertNull(
  400. $this->object->config('foo'),
  401. 'setting a new key to null should have no effect'
  402. );
  403. $this->object->config('some', null);
  404. $this->assertNull(
  405. $this->object->config('some'),
  406. 'should delete the existing value'
  407. );
  408. $this->assertSame(
  409. [
  410. 'a' => ['nested' => 'value'],
  411. ],
  412. $this->object->config(),
  413. 'deleted keys should not be present'
  414. );
  415. }
  416. /**
  417. * testDeleteNested
  418. *
  419. * @return void
  420. */
  421. public function testDeleteNested()
  422. {
  423. $this->object->config('new.foo', null);
  424. $this->assertNull(
  425. $this->object->config('new.foo'),
  426. 'setting a new key to null should have no effect'
  427. );
  428. $this->object->config('a.nested', null);
  429. $this->assertNull(
  430. $this->object->config('a.nested'),
  431. 'should delete the existing value'
  432. );
  433. $this->assertSame(
  434. [
  435. 'some' => 'string',
  436. ],
  437. $this->object->config(),
  438. 'deleted keys should not be present'
  439. );
  440. }
  441. /**
  442. * testDeleteClobber
  443. *
  444. * @expectedException \Exception
  445. * @expectedExceptionMessage Cannot unset a.nested.value.whoops
  446. * @return void
  447. */
  448. public function testDeleteClobber()
  449. {
  450. $this->object->config('a.nested.value.whoops', null);
  451. }
  452. }