| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- namespace Cake\Test\TestCase\Core;
- use Cake\Core\InstanceConfigTrait;
- use Cake\TestSuite\TestCase;
- /**
- * TestInstanceConfig
- */
- class TestInstanceConfig {
- use InstanceConfigTrait;
- /**
- * _defaultConfig
- *
- * Some default config
- *
- * @var array
- */
- protected $_defaultConfig = [
- 'some' => 'string',
- 'a' => ['nested' => 'value']
- ];
- }
- /**
- * ReadOnlyTestInstanceConfig
- */
- class ReadOnlyTestInstanceConfig {
- use InstanceConfigTrait;
- /**
- * _defaultConfig
- *
- * Some default config
- *
- * @var array
- */
- protected $_defaultConfig = [
- 'some' => 'string',
- 'a' => ['nested' => 'value']
- ];
- /**
- * Example of how to prevent modifying config at run time
- *
- * @throws \Exception
- * @param mixed $key
- * @param mixed $value
- * @return void
- */
- protected function _configWrite($key, $value = null) {
- throw new \Exception('This Instance is readonly');
- }
- }
- /**
- * InstanceConfigTraitTest
- *
- */
- class InstanceConfigTraitTest extends TestCase {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->object = new TestInstanceConfig();
- }
- /**
- * testDefaultsAreSet
- *
- * @return void
- */
- public function testDefaultsAreSet() {
- $this->assertSame(
- [
- 'some' => 'string',
- 'a' => ['nested' => 'value']
- ],
- $this->object->config(),
- 'runtime config should match the defaults if not overriden'
- );
- }
- /**
- * testGetSimple
- *
- * @return void
- */
- public function testGetSimple() {
- $this->assertSame(
- 'string',
- $this->object->config('some'),
- 'should return the key value only'
- );
- $this->assertSame(
- ['nested' => 'value'],
- $this->object->config('a'),
- 'should return the key value only'
- );
- }
- /**
- * testGetDot
- *
- * @return void
- */
- public function testGetDot() {
- $this->assertSame(
- 'value',
- $this->object->config('a.nested'),
- 'should return the nested value only'
- );
- }
- /**
- * testSetSimple
- *
- * @return void
- */
- public function testSetSimple() {
- $this->object->config('foo', 'bar');
- $this->assertSame(
- 'bar',
- $this->object->config('foo'),
- 'should return the same value just set'
- );
- $this->object->config('some', 'zum');
- $this->assertSame(
- 'zum',
- $this->object->config('some'),
- 'should return the overritten value'
- );
- $this->assertSame(
- [
- 'some' => 'zum',
- 'a' => ['nested' => 'value'],
- 'foo' => 'bar',
- ],
- $this->object->config(),
- 'updates should be merged with existing config'
- );
- }
- /**
- * testSetNested
- *
- * @return void
- */
- public function testSetNested() {
- $this->object->config('new.foo', 'bar');
- $this->assertSame(
- 'bar',
- $this->object->config('new.foo'),
- 'should return the same value just set'
- );
- $this->object->config('a.nested', 'zum');
- $this->assertSame(
- 'zum',
- $this->object->config('a.nested'),
- 'should return the overritten value'
- );
- $this->assertSame(
- [
- 'some' => 'string',
- 'a' => ['nested' => 'zum'],
- 'new' => ['foo' => 'bar']
- ],
- $this->object->config(),
- 'updates should be merged with existing config'
- );
- }
- /**
- * testSetNested
- *
- * @return void
- */
- public function testSetArray() {
- $this->object->config(['foo' => 'bar']);
- $this->assertSame(
- 'bar',
- $this->object->config('foo'),
- 'should return the same value just set'
- );
- $this->assertSame(
- [
- 'some' => 'string',
- 'a' => ['nested' => 'value'],
- 'foo' => 'bar',
- ],
- $this->object->config(),
- 'updates should be merged with existing config'
- );
- $this->object->config(['new.foo' => 'bar']);
- $this->assertSame(
- 'bar',
- $this->object->config('new.foo'),
- 'should return the same value just set'
- );
- $this->assertSame(
- [
- 'some' => 'string',
- 'a' => ['nested' => 'value'],
- 'foo' => 'bar',
- 'new' => ['foo' => 'bar']
- ],
- $this->object->config(),
- 'updates should be merged with existing config'
- );
- $this->object->config(['multiple' => 'different', 'a.values.to' => 'set']);
- $this->assertSame(
- [
- 'some' => 'string',
- 'a' => ['nested' => 'value', 'values' => ['to' => 'set']],
- 'foo' => 'bar',
- 'new' => ['foo' => 'bar'],
- 'multiple' => 'different'
- ],
- $this->object->config(),
- 'updates should be merged with existing config'
- );
- }
- /**
- * testSetClobber
- *
- * @expectedException \Exception
- * @expectedExceptionMessage Cannot set a.nested.value
- * @return void
- */
- public function testSetClobber() {
- $this->object->config(['a.nested.value' => 'not possible']);
- }
- /**
- * testReadOnlyConfig
- *
- * @expectedException \Exception
- * @expectedExceptionMessage This Instance is readonly
- * @return void
- */
- public function testReadOnlyConfig() {
- $object = new ReadOnlyTestInstanceConfig();
- $this->assertSame(
- [
- 'some' => 'string',
- 'a' => ['nested' => 'value']
- ],
- $object->config(),
- 'default config should be returned'
- );
- $object->config('throw.me', 'an exception');
- }
- /**
- * testDeleteSimple
- *
- * @return void
- */
- public function testDeleteSimple() {
- $this->object->config('foo', null);
- $this->assertNull(
- $this->object->config('foo'),
- 'setting a new key to null should have no effect'
- );
- $this->object->config('some', null);
- $this->assertNull(
- $this->object->config('some'),
- 'should delete the existing value'
- );
- $this->assertSame(
- [
- 'a' => ['nested' => 'value'],
- ],
- $this->object->config(),
- 'deleted keys should not be present'
- );
- }
- /**
- * testDeleteNested
- *
- * @return void
- */
- public function testDeleteNested() {
- $this->object->config('new.foo', null);
- $this->assertNull(
- $this->object->config('new.foo'),
- 'setting a new key to null should have no effect'
- );
- $this->object->config('a.nested', null);
- $this->assertNull(
- $this->object->config('a.nested'),
- 'should delete the existing value'
- );
- $this->assertSame(
- [
- 'some' => 'string',
- ],
- $this->object->config(),
- 'deleted keys should not be present'
- );
- }
- /**
- * testSetClobber
- *
- * @expectedException \Exception
- * @expectedExceptionMessage Cannot unset a.nested.value.whoops
- * @return void
- */
- public function testDeleteClobber() {
- $this->object->config('a.nested.value.whoops', null);
- }
- }
|