InstanceConfigTraitTest.php 9.0 KB

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