cakephp2-php8/lib/Cake/Test/Case/Core/ConfigureTest.php

528 lines
14 KiB
PHP
Raw Normal View History

<?php
/**
* ConfigureTest file
*
* Holds several tests
*
2012-04-27 02:49:18 +00:00
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* 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)
2012-04-27 02:49:18 +00:00
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 1.2.0.5432
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('PhpReader', 'Configure');
/**
* ConfigureTest
*
* @package Cake.Test.Case.Core
*/
class ConfigureTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', true);
App::build();
App::objects('plugin', null, true);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
}
Configure::drop('test');
}
/**
* Test to ensure bootrapping doesn't overwrite prior configs set under 'App' key
*
* @return void
*/
public function testBootstrap() {
$expected = array(
'foo' => 'bar'
);
Configure::write('App', $expected);
Configure::bootstrap(true);
$result = Configure::read('App');
$this->assertEquals($expected['foo'], $result['foo']);
$this->assertFalse($result['base']);
}
/**
* testRead method
*
* @return void
*/
public function testRead() {
$expected = 'ok';
Configure::write('level1.level2.level3_1', $expected);
Configure::write('level1.level2.level3_2', 'something_else');
$result = Configure::read('level1.level2.level3_1');
$this->assertEquals($expected, $result);
$result = Configure::read('level1.level2.level3_2');
$this->assertEquals('something_else', $result);
$result = Configure::read('debug');
$this->assertTrue($result >= 0);
$result = Configure::read();
$this->assertTrue(is_array($result));
$this->assertTrue(isset($result['debug']));
$this->assertTrue(isset($result['level1']));
$result = Configure::read('something_I_just_made_up_now');
$this->assertEquals(null, $result, 'Missing key should return null.');
}
/**
* testWrite method
*
* @return void
*/
public function testWrite() {
$writeResult = Configure::write('SomeName.someKey', 'myvalue');
$this->assertTrue($writeResult);
$result = Configure::read('SomeName.someKey');
$this->assertEquals('myvalue', $result);
$writeResult = Configure::write('SomeName.someKey', null);
$this->assertTrue($writeResult);
$result = Configure::read('SomeName.someKey');
$this->assertEquals(null, $result);
2009-07-24 20:29:46 +00:00
$expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
$writeResult = Configure::write('Key', $expected);
$this->assertTrue($writeResult);
2009-07-24 20:29:46 +00:00
$result = Configure::read('Key');
$this->assertEquals($expected, $result);
2009-07-24 20:29:46 +00:00
$result = Configure::read('Key.One');
$this->assertEquals($expected['One'], $result);
2009-07-24 20:29:46 +00:00
$result = Configure::read('Key.One.Two');
$this->assertEquals($expected['One']['Two'], $result);
2009-07-24 20:29:46 +00:00
$result = Configure::read('Key.One.Two.Three.Four.Five');
$this->assertEquals('cool', $result);
2011-05-21 19:49:28 +00:00
Configure::write('one.two.three.four', '4');
$result = Configure::read('one.two.three.four');
$this->assertEquals('4', $result);
}
2014-12-23 02:26:03 +00:00
/**
* Test the consume method.
*
* @return void
*/
public function testConsume() {
$this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
2014-12-23 02:29:52 +00:00
Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
2014-12-23 02:26:03 +00:00
$result = Configure::consume('Test.key');
$this->assertEquals('value', $result);
$result = Configure::read('Test.key2');
$this->assertEquals('value2', $result, 'Other values should remain.');
$result = Configure::consume('Test');
2014-12-23 02:29:52 +00:00
$expected = array('key2' => 'value2');
2014-12-23 02:26:03 +00:00
$this->assertEquals($expected, $result);
}
2015-01-04 01:27:40 +00:00
/**
* testConsumeEmpty
*
* @return void
*/
2015-01-04 00:57:06 +00:00
public function testConsumeEmpty() {
Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
$result = Configure::consume('');
$this->assertNull($result);
$result = Configure::consume(null);
$this->assertNull($result);
}
/**
* test setting display_errors with debug.
*
* @return void
*/
public function testDebugSettingDisplayErrors() {
Configure::write('debug', 0);
$result = ini_get('display_errors');
$this->assertEquals(0, $result);
Configure::write('debug', 2);
$result = ini_get('display_errors');
$this->assertEquals(1, $result);
}
/**
* testDelete method
*
* @return void
*/
public function testDelete() {
Configure::write('SomeName.someKey', 'myvalue');
$result = Configure::read('SomeName.someKey');
$this->assertEquals('myvalue', $result);
Configure::delete('SomeName.someKey');
$result = Configure::read('SomeName.someKey');
2015-01-04 00:57:06 +00:00
$this->assertNull($result);
Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
$result = Configure::read('SomeName.someKey');
$this->assertEquals('myvalue', $result);
$result = Configure::read('SomeName.otherKey');
$this->assertEquals('otherValue', $result);
Configure::delete('SomeName');
$result = Configure::read('SomeName.someKey');
2015-01-04 00:57:06 +00:00
$this->assertNull($result);
$result = Configure::read('SomeName.otherKey');
2015-01-04 00:57:06 +00:00
$this->assertNull($result);
}
/**
* testCheck method
*
* @return void
*/
public function testCheck() {
Configure::write('ConfigureTestCase', 'value');
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
}
/**
* testCheckingSavedEmpty method
*
* @return void
*/
public function testCheckingSavedEmpty() {
$this->assertTrue(Configure::write('ConfigureTestCase', 0));
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertTrue(Configure::write('ConfigureTestCase', '0'));
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertTrue(Configure::write('ConfigureTestCase', false));
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertTrue(Configure::write('ConfigureTestCase', null));
$this->assertFalse(Configure::check('ConfigureTestCase'));
}
/**
* testCheckKeyWithSpaces method
*
* @return void
*/
public function testCheckKeyWithSpaces() {
$this->assertTrue(Configure::write('Configure Test', "test"));
$this->assertTrue(Configure::check('Configure Test'));
Configure::delete('Configure Test');
$this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
$this->assertTrue(Configure::check('Configure Test.Test Case'));
}
/**
* testCheckEmpty
*
* @return void
*/
public function testCheckEmpty() {
2015-01-04 00:57:06 +00:00
$this->assertFalse(Configure::check(''));
$this->assertFalse(Configure::check(null));
}
/**
* testLoad method
*
* @expectedException RuntimeException
* @return void
*/
public function testLoadExceptionOnNonExistantFile() {
Configure::config('test', new PhpReader());
2013-01-23 12:45:50 +00:00
Configure::load('non_existing_configuration_file', 'test');
}
/**
* test load method for default config creation
*
* @return void
*/
public function testLoadDefaultConfig() {
try {
Configure::load('non_existing_configuration_file');
} catch (Exception $e) {
$result = Configure::configured('default');
$this->assertTrue($result);
}
}
/**
* test load with merging
*
* @return void
*/
public function testLoadWithMerge() {
Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
$result = Configure::load('var_test', 'test');
$this->assertTrue($result);
$this->assertEquals('value', Configure::read('Read'));
$result = Configure::load('var_test2', 'test', true);
$this->assertTrue($result);
$this->assertEquals('value2', Configure::read('Read'));
$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
$this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
$this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
$this->assertEquals('one', Configure::read('TestAcl.custom'));
}
/**
* test loading with overwrite
*
* @return void
*/
public function testLoadNoMerge() {
Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
$result = Configure::load('var_test', 'test');
$this->assertTrue($result);
$this->assertEquals('value', Configure::read('Read'));
$result = Configure::load('var_test2', 'test', false);
$this->assertTrue($result);
$this->assertEquals('value2', Configure::read('Read'));
$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
$this->assertNull(Configure::read('Deep.Deeper.Deepest'));
}
/**
* testLoad method
*
* @return void
*/
public function testLoadPlugin() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
Configure::config('test', new PhpReader());
CakePlugin::load('TestPlugin');
$result = Configure::load('TestPlugin.load', 'test');
$this->assertTrue($result);
$expected = '/test_app/plugins/test_plugin/config/load.php';
$config = Configure::read('plugin_load');
$this->assertEquals($expected, $config);
$result = Configure::load('TestPlugin.more.load', 'test');
$this->assertTrue($result);
$expected = '/test_app/plugins/test_plugin/config/more.load.php';
$config = Configure::read('plugin_more_load');
$this->assertEquals($expected, $config);
CakePlugin::unload();
}
/**
* testStore method
*
* @return void
*/
public function testStoreAndRestore() {
Configure::write('Cache.disable', false);
Configure::write('Testing', 'yummy');
$this->assertTrue(Configure::store('store_test', 'default'));
Configure::delete('Testing');
$this->assertNull(Configure::read('Testing'));
Configure::restore('store_test', 'default');
$this->assertEquals('yummy', Configure::read('Testing'));
Cache::delete('store_test', 'default');
}
/**
* test that store and restore only store/restore the provided data.
*
* @return void
*/
public function testStoreAndRestoreWithData() {
Configure::write('Cache.disable', false);
Configure::write('testing', 'value');
Configure::store('store_test', 'default', array('store_test' => 'one'));
Configure::delete('testing');
2012-02-23 23:29:53 +00:00
$this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
Configure::restore('store_test', 'default');
$this->assertEquals('one', Configure::read('store_test'));
$this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
Cache::delete('store_test', 'default');
}
/**
* testVersion method
*
* @return void
*/
public function testVersion() {
$result = Configure::version();
$this->assertTrue(version_compare($result, '1.2', '>='));
}
/**
* test adding new readers.
*
* @return void
*/
public function testReaderSetup() {
$reader = new PhpReader();
Configure::config('test', $reader);
$configured = Configure::configured();
$this->assertTrue(in_array('test', $configured));
$this->assertTrue(Configure::configured('test'));
$this->assertFalse(Configure::configured('fake_garbage'));
2010-12-04 05:14:55 +00:00
$this->assertTrue(Configure::drop('test'));
$this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
}
/**
* test reader() throwing exceptions on missing interface.
*
* @expectedException PHPUnit_Framework_Error
* @return void
2016-12-07 05:38:55 +00:00
* @throws PHPUnit_Framework_Error
*/
public function testReaderExceptionOnIncorrectClass() {
$reader = new StdClass();
2015-12-13 20:12:31 +00:00
try {
Configure::config('test', $reader);
} catch (TypeError $e) {
throw new PHPUnit_Framework_Error('Raised an error', 100, __FILE__, __LINE__);
2015-12-13 20:12:31 +00:00
}
}
2012-04-15 03:31:26 +00:00
/**
* Test that clear wipes all values.
*
* @return void
*/
public function testClear() {
Configure::write('test', 'value');
$this->assertTrue(Configure::clear());
$this->assertNull(Configure::read('debug'));
$this->assertNull(Configure::read('test'));
}
2012-04-20 02:45:01 +00:00
/**
* testDumpNoAdapter
*
2012-04-20 02:45:01 +00:00
* @expectedException ConfigureException
2014-04-02 01:02:37 +00:00
* @return void
2012-04-20 02:45:01 +00:00
*/
public function testDumpNoAdapter() {
Configure::dump(TMP . 'test.php', 'does_not_exist');
}
/**
* test dump integrated with the PhpReader.
*
* @return void
*/
public function testDump() {
Configure::config('test_reader', new PhpReader(TMP));
$result = Configure::dump('config_test.php', 'test_reader');
2012-04-20 02:45:01 +00:00
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('$config = ', $result);
2012-11-14 09:00:15 +00:00
if (file_exists(TMP . 'config_test.php')) {
unlink(TMP . 'config_test.php');
}
2012-04-20 02:45:01 +00:00
}
2012-05-01 01:07:32 +00:00
/**
* Test dumping only some of the data.
*
2013-07-05 15:19:22 +00:00
* @return void
2012-05-01 01:07:32 +00:00
*/
public function testDumpPartial() {
Configure::config('test_reader', new PhpReader(TMP));
$result = Configure::dump('config_test.php', 'test_reader', array('Error'));
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('$config = ', $result);
$this->assertContains('Error', $result);
$this->assertNotContains('debug', $result);
2012-11-14 09:00:15 +00:00
if (file_exists(TMP . 'config_test.php')) {
unlink(TMP . 'config_test.php');
}
2012-05-01 01:07:32 +00:00
}
}