2010-12-02 23:06:08 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PhpConfigReaderTest
|
|
|
|
*
|
2017-06-11 00:15:34 +02:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-11 00:10:52 +02:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-12-02 23:06:08 -05:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-12-02 23:06:08 -05:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-11 00:10:52 +02:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-11 00:15:34 +02:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Configure
|
2010-12-02 23:06:08 -05:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-11 00:23:14 +02:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-12-02 23:06:08 -05:00
|
|
|
*/
|
2013-05-31 00:11:14 +02:00
|
|
|
|
2010-12-09 00:43:11 -04:30
|
|
|
App::uses('PhpReader', 'Configure');
|
2010-12-02 23:06:08 -05:00
|
|
|
|
2013-05-31 00:11:14 +02:00
|
|
|
/**
|
2016-08-10 12:22:09 +02:00
|
|
|
* PhpReaderTest
|
2013-05-31 00:11:14 +02:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Configure
|
|
|
|
*/
|
2010-12-02 23:06:08 -05:00
|
|
|
class PhpReaderTest extends CakeTestCase {
|
2012-03-10 22:30:56 -05:00
|
|
|
|
2012-05-01 21:43:01 -04:00
|
|
|
/**
|
|
|
|
* Test data to serialize and unserialize.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $testData = array(
|
|
|
|
'One' => array(
|
|
|
|
'two' => 'value',
|
|
|
|
'three' => array(
|
|
|
|
'four' => 'value four'
|
|
|
|
),
|
|
|
|
'is_null' => null,
|
|
|
|
'bool_false' => false,
|
|
|
|
'bool_true' => true,
|
|
|
|
),
|
|
|
|
'Asset' => array(
|
|
|
|
'timestamp' => 'force'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2010-12-02 23:06:08 -05:00
|
|
|
/**
|
2012-08-10 10:46:18 -03:00
|
|
|
* Setup.
|
2010-12-02 23:06:08 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function setUp() {
|
2010-12-02 23:06:08 -05:00
|
|
|
parent::setUp();
|
2012-03-10 22:30:56 -05:00
|
|
|
$this->path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
|
2010-12-02 23:06:08 -05:00
|
|
|
}
|
2011-12-06 12:52:48 -08:00
|
|
|
|
2010-12-02 23:06:08 -05:00
|
|
|
/**
|
2012-08-10 10:46:18 -03:00
|
|
|
* Test reading files.
|
2010-12-02 23:06:08 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testRead() {
|
2010-12-02 23:06:08 -05:00
|
|
|
$reader = new PhpReader($this->path);
|
|
|
|
$values = $reader->read('var_test');
|
|
|
|
$this->assertEquals('value', $values['Read']);
|
2010-12-02 23:22:26 -05:00
|
|
|
$this->assertEquals('buried', $values['Deep']['Deeper']['Deepest']);
|
2011-01-23 20:27:46 -02:00
|
|
|
|
|
|
|
$values = $reader->read('var_test.php');
|
|
|
|
$this->assertEquals('value', $values['Read']);
|
2010-12-02 23:06:08 -05:00
|
|
|
}
|
|
|
|
|
2012-08-10 10:46:18 -03:00
|
|
|
/**
|
|
|
|
* Test an exception is thrown by reading files that exist without .php extension.
|
|
|
|
*
|
|
|
|
* @expectedException ConfigureException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReadWithExistentFileWithoutExtension() {
|
|
|
|
$reader = new PhpReader($this->path);
|
|
|
|
$reader->read('no_php_extension');
|
|
|
|
}
|
|
|
|
|
2010-12-02 23:06:08 -05:00
|
|
|
/**
|
|
|
|
* Test an exception is thrown by reading files that don't exist.
|
|
|
|
*
|
2010-12-11 19:01:07 -05:00
|
|
|
* @expectedException ConfigureException
|
2010-12-02 23:06:08 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-08-10 10:46:18 -03:00
|
|
|
public function testReadWithNonExistentFile() {
|
2010-12-02 23:06:08 -05:00
|
|
|
$reader = new PhpReader($this->path);
|
|
|
|
$reader->read('fake_values');
|
|
|
|
}
|
2010-12-02 23:14:26 -05:00
|
|
|
|
2010-12-02 23:22:26 -05:00
|
|
|
/**
|
2012-08-10 10:46:18 -03:00
|
|
|
* Test reading an empty file.
|
2010-12-02 23:22:26 -05:00
|
|
|
*
|
2012-08-10 10:46:18 -03:00
|
|
|
* @expectedException ConfigureException
|
2010-12-02 23:22:26 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testReadEmptyFile() {
|
2010-12-02 23:22:26 -05:00
|
|
|
$reader = new PhpReader($this->path);
|
|
|
|
$reader->read('empty');
|
|
|
|
}
|
|
|
|
|
2010-12-04 00:58:02 -05:00
|
|
|
/**
|
2012-08-10 10:46:18 -03:00
|
|
|
* Test reading keys with ../ doesn't work.
|
2010-12-04 00:58:02 -05:00
|
|
|
*
|
2010-12-11 19:01:07 -05:00
|
|
|
* @expectedException ConfigureException
|
2010-12-04 00:58:02 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testReadWithDots() {
|
2010-12-04 00:58:02 -05:00
|
|
|
$reader = new PhpReader($this->path);
|
|
|
|
$reader->read('../empty');
|
|
|
|
}
|
|
|
|
|
2010-12-02 23:14:26 -05:00
|
|
|
/**
|
2012-08-10 10:46:18 -03:00
|
|
|
* Test reading from plugins.
|
2010-12-02 23:14:26 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testReadPluginValue() {
|
2010-12-02 23:14:26 -05:00
|
|
|
App::build(array(
|
2012-02-18 04:31:29 -08:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2012-02-18 04:04:54 -08:00
|
|
|
), App::RESET);
|
2011-05-13 01:28:17 -04:30
|
|
|
CakePlugin::load('TestPlugin');
|
2010-12-02 23:14:26 -05:00
|
|
|
$reader = new PhpReader($this->path);
|
|
|
|
$result = $reader->read('TestPlugin.load');
|
2011-01-23 20:27:46 -02:00
|
|
|
$this->assertTrue(isset($result['plugin_load']));
|
2010-12-02 23:14:26 -05:00
|
|
|
|
2011-01-23 20:27:46 -02:00
|
|
|
$result = $reader->read('TestPlugin.load.php');
|
2010-12-02 23:14:26 -05:00
|
|
|
$this->assertTrue(isset($result['plugin_load']));
|
2011-05-13 01:28:17 -04:30
|
|
|
CakePlugin::unload();
|
2010-12-02 23:14:26 -05:00
|
|
|
}
|
2012-04-19 22:07:05 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test dumping data to PHP format.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDump() {
|
2012-05-01 21:43:01 -04:00
|
|
|
$reader = new PhpReader(TMP);
|
|
|
|
$result = $reader->dump('test.php', $this->testData);
|
2012-04-21 20:26:35 -04:00
|
|
|
$this->assertTrue($result > 0);
|
2012-04-19 22:07:05 -04:00
|
|
|
$expected = <<<PHP
|
|
|
|
<?php
|
|
|
|
\$config = array (
|
|
|
|
'One' =>
|
|
|
|
array (
|
|
|
|
'two' => 'value',
|
|
|
|
'three' =>
|
|
|
|
array (
|
|
|
|
'four' => 'value four',
|
|
|
|
),
|
2012-05-01 21:43:01 -04:00
|
|
|
'is_null' => NULL,
|
|
|
|
'bool_false' => false,
|
|
|
|
'bool_true' => true,
|
2012-04-19 22:07:05 -04:00
|
|
|
),
|
|
|
|
'Asset' =>
|
|
|
|
array (
|
|
|
|
'timestamp' => 'force',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
PHP;
|
2012-05-01 21:43:01 -04:00
|
|
|
$file = TMP . 'test.php';
|
2012-04-21 20:26:35 -04:00
|
|
|
$contents = file_get_contents($file);
|
2012-05-01 21:06:24 -04:00
|
|
|
|
|
|
|
unlink($file);
|
2012-05-01 21:43:01 -04:00
|
|
|
$this->assertTextEquals($expected, $contents);
|
2012-08-15 19:38:58 +05:30
|
|
|
|
|
|
|
$result = $reader->dump('test', $this->testData);
|
|
|
|
$this->assertTrue($result > 0);
|
|
|
|
|
|
|
|
$contents = file_get_contents($file);
|
|
|
|
$this->assertTextEquals($expected, $contents);
|
|
|
|
unlink($file);
|
2012-05-01 21:43:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that dump() makes files read() can read.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDumpRead() {
|
|
|
|
$reader = new PhpReader(TMP);
|
|
|
|
$reader->dump('test.php', $this->testData);
|
|
|
|
$result = $reader->read('test.php');
|
|
|
|
unlink(TMP . 'test.php');
|
|
|
|
|
2012-05-01 21:45:06 -04:00
|
|
|
$this->assertEquals($this->testData, $result);
|
2012-04-19 22:07:05 -04:00
|
|
|
}
|
|
|
|
|
2010-12-02 23:06:08 -05:00
|
|
|
}
|