InstanceConfigTrait.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. $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. [
  141. 'some' => 'zum',
  142. 'a' => ['nested' => 'value'],
  143. 'foo' => 'bar',
  144. ],
  145. $this->object->config(),
  146. 'updates should be merged with existing config'
  147. );
  148. }
  149. /**
  150. * testSetNested
  151. *
  152. * @return void
  153. */
  154. public function testSetNested() {
  155. $this->object->config('new.foo', 'bar');
  156. $this->assertSame(
  157. 'bar',
  158. $this->object->config('new.foo'),
  159. 'should return the same value just set'
  160. );
  161. $this->object->config('a.nested', 'zum');
  162. $this->assertSame(
  163. 'zum',
  164. $this->object->config('a.nested'),
  165. 'should return the overritten value'
  166. );
  167. $this->assertSame(
  168. [
  169. 'some' => 'string',
  170. 'a' => ['nested' => 'zum'],
  171. 'new' => ['foo' => 'bar']
  172. ],
  173. $this->object->config(),
  174. 'updates should be merged with existing config'
  175. );
  176. }
  177. /**
  178. * testSetNested
  179. *
  180. * @return void
  181. */
  182. public function testSetArray() {
  183. $this->object->config(['foo' => 'bar']);
  184. $this->assertSame(
  185. 'bar',
  186. $this->object->config('foo'),
  187. 'should return the same value just set'
  188. );
  189. $this->assertSame(
  190. [
  191. 'some' => 'string',
  192. 'a' => ['nested' => 'value'],
  193. 'foo' => 'bar',
  194. ],
  195. $this->object->config(),
  196. 'updates should be merged with existing config'
  197. );
  198. $this->object->config(['new.foo' => 'bar']);
  199. $this->assertSame(
  200. 'bar',
  201. $this->object->config('new.foo'),
  202. 'should return the same value just set'
  203. );
  204. $this->assertSame(
  205. [
  206. 'some' => 'string',
  207. 'a' => ['nested' => 'value'],
  208. 'foo' => 'bar',
  209. 'new' => ['foo' => 'bar']
  210. ],
  211. $this->object->config(),
  212. 'updates should be merged with existing config'
  213. );
  214. $this->object->config(['multiple' => 'different', 'a.values.to' => 'set']);
  215. $this->assertSame(
  216. [
  217. 'some' => 'string',
  218. 'a' => ['nested' => 'value', 'values' => ['to' => 'set']],
  219. 'foo' => 'bar',
  220. 'new' => ['foo' => 'bar'],
  221. 'multiple' => 'different'
  222. ],
  223. $this->object->config(),
  224. 'updates should be merged with existing config'
  225. );
  226. }
  227. /**
  228. * testSetClobber
  229. *
  230. * @expectedException \Exception
  231. * @expectedExceptionMessage Cannot set a.nested.value
  232. * @return void
  233. */
  234. public function testSetClobber() {
  235. $this->object->config(['a.nested.value' => 'not possible']);
  236. }
  237. /**
  238. * testReadOnlyConfig
  239. *
  240. * @expectedException \Exception
  241. * @expectedExceptionMessage This Instance is readonly
  242. * @return void
  243. */
  244. public function testReadOnlyConfig() {
  245. $object = new ReadOnlyTestInstanceConfig();
  246. $this->assertSame(
  247. [
  248. 'some' => 'string',
  249. 'a' => ['nested' => 'value']
  250. ],
  251. $object->config(),
  252. 'default config should be returned'
  253. );
  254. $object->config('throw.me', 'an exception');
  255. }
  256. /**
  257. * testDeleteSimple
  258. *
  259. * @return void
  260. */
  261. public function testDeleteSimple() {
  262. $this->object->config('foo', null);
  263. $this->assertNull(
  264. $this->object->config('foo'),
  265. 'setting a new key to null should have no effect'
  266. );
  267. $this->object->config('some', null);
  268. $this->assertNull(
  269. $this->object->config('some'),
  270. 'should delete the existing value'
  271. );
  272. $this->assertSame(
  273. [
  274. 'a' => ['nested' => 'value'],
  275. ],
  276. $this->object->config(),
  277. 'deleted keys should not be present'
  278. );
  279. }
  280. /**
  281. * testDeleteNested
  282. *
  283. * @return void
  284. */
  285. public function testDeleteNested() {
  286. $this->object->config('new.foo', null);
  287. $this->assertNull(
  288. $this->object->config('new.foo'),
  289. 'setting a new key to null should have no effect'
  290. );
  291. $this->object->config('a.nested', null);
  292. $this->assertNull(
  293. $this->object->config('a.nested'),
  294. 'should delete the existing value'
  295. );
  296. $this->assertSame(
  297. [
  298. 'some' => 'string',
  299. ],
  300. $this->object->config(),
  301. 'deleted keys should not be present'
  302. );
  303. }
  304. /**
  305. * testSetClobber
  306. *
  307. * @expectedException \Exception
  308. * @expectedExceptionMessage Cannot unset a.nested.value.whoops
  309. * @return void
  310. */
  311. public function testDeleteClobber() {
  312. $this->object->config('a.nested.value.whoops', null);
  313. }
  314. }