diff --git a/README.md b/README.md index 117b0cb..cfc68a8 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ composer require "jelix/inifile" # Usage The ```\Jelix\IniFile\IniModifier``` class allows to read an ini file, to modify its -content, and save it by preserving its comments and empty lines. +content including comments, and save it by preserving empty lines. Don't use this class to just read content. Use instead ```\Jelix\IniFile\Util``` or ```parse_ini_file()``` for this purpose, it's more efficient and performant. @@ -32,9 +32,17 @@ $val = $ini->getValue('parameter_name', 'section_name'); // remove a parameter $ini->removeValue('parameter_name', 'section_name'); +// setting a comment (in the line preceding the parameter) - the leading ';' +// can be omitted or included in the comment line, if missing will be added +$ini->setComments('parameter_name', '; single-line comment text', 'section_name'); -// save into file +// setting a multi-line comment (in the lines preceding the parameter) +$ini->setComments('parameter_name', [ 'first line', 'second line' ], 'section_name'); + +// remove all comment lines preceding a parameter +$ini->removeComments('parameter_name', 'section_name'); +// save into file $ini->save(); $ini->saveAs('otherfile.ini'); @@ -49,6 +57,8 @@ $ini->mergeSection('sectionSource', 'sectionTarget'); ``` +It support parsing of parameter values into PHP types (string, numeric, boolean) as well as + It supports also array values (indexed or associative) like : ``` @@ -60,7 +70,7 @@ assoc[otherkey]=bus Then in PHP: -``` +```php $ini = new \Jelix\IniFile\IniModifier('myfile.ini'); $val = $ini->getValue('foo'); // array('bar', 'baz'); @@ -72,14 +82,13 @@ $val = $ini->getValue('foo'); // array('bar', 'baz', 'other value'); $ini->setValue('foo', 'five', 0, 5); $val = $ini->getValue('foo'); // array('bar', 'baz', 'other value', 5 => 'five'); - $ini->setValue('assoc', 'other value', 0, 'ov'); $val = $ini->getValue('assoc'); // array('key1'=>'car', 'otherkey'=>'bus', 'ov'=>'other value'); ``` After saving, the ini content is: -``` +```ini foo[]=bar foo[]=baz assoc[key1]=car diff --git a/composer.json b/composer.json index 4371bb3..59b9779 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "classes to read and modify ini files by preserving comments and empty lines", "keywords": ["ini", "files"], - "homepage": "http://jelix.org", + "homepage": "https://github.com/jelix/inifile", "license": "LGPL-2.1", "authors": [ { @@ -12,6 +12,9 @@ }, { "name": "Loic Mathaud" + }, + { + "name": "Riccardo Mazzei" } ], "require": { diff --git a/lib/IniModifier.php b/lib/IniModifier.php index 7e57721..ee0016e 100644 --- a/lib/IniModifier.php +++ b/lib/IniModifier.php @@ -30,8 +30,9 @@ class IniModifier extends IniReader implements IniModifierInterface * @param string $initialContent if the file does not exists, it takes the given content * as initial content. */ - public function __construct($filename, $initialContent = '') + public function __construct($filename, $initialContent = '', $parsemode = self::PR_NORMAL) { + $this->parsemode = $parsemode; if (!$filename) { throw new IniInvalidArgumentException('Filename should not be empty'); } @@ -132,7 +133,16 @@ protected function _setValue($name, $value, $section = 0, $key = null) } if (!$foundValue) { if ($key === null) { - $this->content[$section][] = array(self::TK_VALUE, $name, $value); + if ($this->content[$section]) { + for ($pos = count($this->content[$section]) - 1; $pos >= 0; $pos--) { + if ($this->content[$section][$pos][0] !== self::TK_WS && $this->content[$section][$pos][0] !== self::TK_COMMENT) { + array_splice($this->content[$section], $pos + 1, 0, array(array(self::TK_VALUE, $name, $value))); + break; + } + } + } else { + $this->content[$section][] = array(self::TK_VALUE, $name, $value); + } } else { if ($key === '') { if ($lastKey != -1) { @@ -141,7 +151,16 @@ protected function _setValue($name, $value, $section = 0, $key = null) $key = 0; } } - $this->content[$section][] = array(self::TK_ARR_VALUE, $name, $value, $key); + if ($this->content[$section]) { + for ($pos = count($this->content[$section]) - 1; $pos >= 0; $pos--) { + if ($this->content[$section][$pos][0] !== self::TK_WS && $this->content[$section][$pos][0] !== self::TK_COMMENT) { + array_splice($this->content[$section], $pos + 1, 0, array(array(self::TK_ARR_VALUE, $name, $value, $key))); + break; + } + } + } else { + $this->content[$section][] = array(self::TK_ARR_VALUE, $name, $value, $key); + } } $this->modified = true; } @@ -185,7 +204,16 @@ protected function _setArrayValue($name, $value, $section = 0) foreach ($value as $k => $v) { if (!$foundKeys[$k]) { - $this->content[$section][] = array(self::TK_ARR_VALUE, $name, $v, $k); + if ($this->content[$section]) { + for ($pos = count($this->content[$section]) - 1; $pos >= 0; $pos--) { + if ($this->content[$section][$pos][0] !== self::TK_WS && $this->content[$section][$pos][0] !== self::TK_COMMENT) { + array_splice($this->content[$section], $pos + 1, 0, array(array(self::TK_ARR_VALUE, $name, $v, $k))); + break; + } + } + } else { + $this->content[$section][] = array(self::TK_ARR_VALUE, $name, $v, $k); + } } } $this->modified = true; @@ -194,7 +222,7 @@ protected function _setArrayValue($name, $value, $section = 0) /** * modify several options in the ini file. * - * @param array $value associated array with key=>value + * @param array $values associated array with key=>value * @param string $section the section where to set the item. 0 is the global section */ public function setValues($values, $section = 0) @@ -297,6 +325,129 @@ public function removeValue($name, $section = 0, $key = null, $removePreviousCom } } + /** + * create or replace comment lines preceding an option. + * + * @param string $name the name of the option to find + * @param mixed $comments comment line (if string) or array of lines. + * @param string $section the section where to find the item to add/set the comments to. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function setComments($name, $comments, $section = 0, $key = null) + { + $this->_setOrRemoveComments($name, $section, $key, $comments); + } + + /** + * remove comment lines preceding an option. + * + * @param string $name the name of the option to find + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function removeComments($name, $section = 0, $key = null) + { + $this->_setOrRemoveComments($name, $section, $key, $comments = null); + } + + protected function _setOrRemoveComments($name, $section = 0, $key = null, $comments = null) + { + if (is_string($key) && !preg_match('/^[^\\[\\]]*$/', $key)) { + throw new IniInvalidArgumentException("Invalid key $key for the value $name"); + } + + if ($comments) { + if (!is_array($comments)) { + $comments = array($comments); + } + + foreach ($comments as $i => $comment) { + if (substr($comment, 0, 1) !== ';') { + $comments[$i] = ';' . $comments[$i]; + } + $comments[$i] = array(self::TK_COMMENT, $comments[$i]); + } + } + + if ($name == '') { + // retrieve the previous section + $previousSection = -1; + foreach ($this->content as $s => $c) { + if ($s === $section) { + break; + } else { + $previousSection = $s; + } + } + + if ($previousSection != -1) { + //retrieve the last comment + $s = $this->content[$previousSection]; + end($s); + $tok = current($s); + while ($tok !== false) { + if ($tok[0] != self::TK_COMMENT) { + break; + } + if ($tok[0] == self::TK_COMMENT && strpos($tok[1], 'content[$previousSection][key($s)] = array(self::TK_WS, '--'); + } + $tok = prev($s); + } + if ($comments) { + foreach ($comments as $comment) { + $this->content[$previousSection][] = $comment; + } + } + } + return; + } + + if (isset($this->content[$section])) { + // boolean to erase array values if the option to remove is an array + $previousComment = array(); + foreach ($this->content[$section] as $k => $item) { + if ($item[0] == self::TK_COMMENT) { + $previousComment[] = $k; + continue; + } + + if ($item[0] == self::TK_WS) { + continue; + } + + // if the item is not a value or an array value, or not the same name + if ($item[1] != $name) { + $previousComment = array(); + continue; + } + + // if it is an array value, and if the key doesn't correspond + if ($item[0] == self::TK_ARR_VALUE && $key !== null) { + if ($item[3] != $key) { + $previousComment = array(); + continue; + } + } + $this->modified = true; + if (count($previousComment)) { + $kc = array_pop($previousComment); + + $lastKc = -1; + while ($kc !== null) { + if (strpos($this->content[$section][$kc][1], 'content[$section][$kc] = array(self::TK_WS, '--'); + } + $kc = array_pop($previousComment); + } + } + if ($comments) { + array_splice($this->content[$section], $k, 0, $comments); + } + } + } + } + /** * remove a section from the ini file. * @@ -431,14 +582,25 @@ protected function getIniValue($value) return "on"; } } - if ($value === '' || - is_numeric(trim($value)) || - (is_string($value) && preg_match('/^[\\w\\-\\.]*$/u', $value) && - strpos("\n", $value) === false) - ) { - return $value; - } else { - $value = '"'.$value.'"'; + if ($this->parsemode === self::PR_NORMAL) { + if ($value === '' || + is_numeric(trim($value)) || + (is_string($value) && preg_match('/^[\\w\\-\\.]*$/u', $value) && + strpos("\n", $value) === false) + ) { + return trim($value); + } else { + $value = '"'.$value.'"'; + } + } + else { + if ($value === '' || + is_numeric(trim($value)) + ) { + return trim($value); + } else { + $value = '"'.$value.'"'; + } } return $value; diff --git a/lib/IniModifierArray.php b/lib/IniModifierArray.php index f3c15ee..8de6828 100644 --- a/lib/IniModifierArray.php +++ b/lib/IniModifierArray.php @@ -112,7 +112,7 @@ public function setValue($name, $value, $section = 0, $key = null) /** * modify several options in the latest ini file. * - * @param array $value associated array with key=>value + * @param array $values associated array with key=>value * @param string $section the section where to set the item. 0 is the global section */ public function setValues($values, $section = 0) @@ -201,6 +201,40 @@ public function removeValue($name, $section = 0, $key = null, $removePreviousCom } } + /** + * create or replace comment lines preceding an option from all ini file. + * + * @param string $name the name of the option to find + * @param mixed $comments comment line content (if string) or array of lines contents. Each line + * will be prepended with ";" if needed + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function setComments($name, $comments, $section = 0, $key = null) + { + foreach ($this->modifiers as $mod) { + if ($mod instanceof IniModifierInterface) { + $mod->setComments($name, $comments, $section, $key); + } + } + } + + /** + * remove comment lines preceding an option from all ini file. + * + * @param string $name the name of the option to find + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function removeComments($name, $section = 0, $key = null) + { + foreach ($this->modifiers as $mod) { + if ($mod instanceof IniModifierInterface) { + $mod->removeComments($name, $section, $key); + } + } + } + /** * remove a section from all ini file. * @param int $section diff --git a/lib/IniModifierInterface.php b/lib/IniModifierInterface.php index 820bf73..929b124 100644 --- a/lib/IniModifierInterface.php +++ b/lib/IniModifierInterface.php @@ -29,7 +29,7 @@ public function setValue($name, $value, $section = 0, $key = null); /** * modify several options in the ini file. * - * @param array $value associated array with key=>value + * @param array $values associated array with key=>value * @param string $section the section where to set the item. 0 is the global section */ public function setValues($values, $section = 0); @@ -48,6 +48,26 @@ public function setValues($values, $section = 0); */ public function removeValue($name, $section = 0, $key = null, $removePreviousComment = true); + /** + * create or replace comment lines preceding an option. + * + * @param string $name the name of the option to find + * @param mixed $comments comment line content (if string) or array of lines contents. Each line + * will be prepended with ";" if needed + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function setComments($name, $comments, $section = 0, $key = null); + + /** + * remove comment lines preceding an option. + * + * @param string $name the name of the option to find + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function removeComments($name, $section = 0, $key = null); + /** * remove a section from the ini file. * diff --git a/lib/IniReader.php b/lib/IniReader.php index b334611..1a65bee 100644 --- a/lib/IniReader.php +++ b/lib/IniReader.php @@ -20,7 +20,25 @@ * read ini content, use parse_ini_file, it's better in term of performance. */ class IniReader implements IniReaderInterface -{ +{ + /** + * @const integer normal parse mode + * all numeric and boolean values are parsed + */ + const PR_NORMAL = 0; + + /** + * @const integer numeric parse mode + * only numeric values are parsed + */ + const PR_NUMERIC = 1; + + /** + * @const integer raw parse mode + * all values are left as strings + */ + const PR_RAW = 2; + /** * @const integer token type for whitespaces */ @@ -42,6 +60,14 @@ class IniReader implements IniReaderInterface */ const TK_ARR_VALUE = 4; + /** + * the parse mode: PR_NORMAL: normal, typed parsing (numeric types and boolean) + * PR_NUMERIC: only numeric fields are parsed into corresponding types + * PR_RAW: any value is left as string when read from ini file + * @var int + */ + protected $parsemode; + /** * each item of this array contains data for a section. the key of the item * is the section name. There is a section with the key "0", and which contains @@ -69,8 +95,9 @@ class IniReader implements IniReaderInterface * * @param string $filename the file to load */ - public function __construct($filename) + public function __construct($filename, $parsemode = self::PR_NORMAL) { + $this->parsemode = $parsemode; if (!file_exists($filename) || !is_file($filename)) { throw new IniInvalidArgumentException("The file $filename does not exists"); } @@ -229,20 +256,46 @@ public function getValues($section = 0) } protected function convertValue($value) { - if (!is_string($value)) { - // values that are set after the parsing, may be PHP raw values... - return $value; - } - if (preg_match('/^-?[0-9]$/', $value)) { - return intval($value); - } elseif (preg_match('/^-?[0-9\.]$/', $value)) { - return floatval($value); - } elseif (strtolower($value) === 'true' || strtolower($value) === 'on' || strtolower($value) === 'yes') { - return true; - } elseif (strtolower($value) === 'false' || strtolower($value) === 'off' || strtolower($value) === 'no' || strtolower($value) === 'none') { - return false; + switch ($this->parsemode) { + + case self::PR_RAW: { + return strval($value); + } + + case self::PR_NUMERIC: { + if (is_numeric($value)) { + // values that are set after the parsing, may be PHP numeric values... + return $value; + } + + if (preg_match('/^-?[0-9]+$/', $value)) { + return intval($value); + } elseif (preg_match('/^-?[0-9]*\.[0-9]+$/', $value)) { + return floatval($value); + } + return strval($value); + } + + case self::PR_NORMAL: + default: { + + if (!is_string($value)) { + // values that are set after the parsing, may be PHP raw values... + return $value; + } + + if (preg_match('/^-?[0-9]+$/', $value)) { + return intval($value); + } elseif (preg_match('/^-?[0-9]*\.[0-9]+$/', $value)) { + return floatval($value); + } elseif (strtolower($value) === 'true' || strtolower($value) === 'on' || strtolower($value) === 'yes') { + return true; + } elseif (strtolower($value) === 'false' || strtolower($value) === 'off' || strtolower($value) === 'no' || strtolower($value) === 'none') { + return false; + } + return $value; + } } - return $value; } diff --git a/lib/MultiIniModifier.php b/lib/MultiIniModifier.php index 97e9a82..8ce2a16 100644 --- a/lib/MultiIniModifier.php +++ b/lib/MultiIniModifier.php @@ -91,7 +91,7 @@ public function setValueOnMaster($name, $value, $section = 0, $key = null) /** * modify several options in the overrider ini file. * - * @param array $value associated array with key=>value + * @param array $values associated array with key=>value * @param string $section the section where to set the item. 0 is the global section */ public function setValues($values, $section = 0) @@ -102,7 +102,7 @@ public function setValues($values, $section = 0) /** * modify several options in the master ini file. * - * @param array $value associated array with key=>value + * @param array $values associated array with key=>value * @param string $section the section where to set the item. 0 is the global section */ public function setValuesOnMaster($values, $section = 0) @@ -214,6 +214,67 @@ public function removeValueOnMaster($name, $section = 0, $key = null, $removePre $this->master->removeValue($name, $section, $key, $removePreviousComment); } + /** + * create or replace comment lines preceding an option in the overrider ini file. + * + * @param string $name the name of the option to find + * @param mixed $comments comment line content (if string) or array of lines contents. Each line + * will be prepended with ";" if needed + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function setComments($name, $comments, $section = 0, $key = null) + { + $this->overrider->setComments($name, $comments, $section, $key); + } + + /** + * create or replace comment lines preceding an option in the overrider ini file. + * + * @param string $name the name of the option to find + * @param mixed $comments comment line content (if string) or array of lines contents. Each line + * will be prepended with ";" if needed + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function setCommentsOnMaster($name, $comments, $section = 0, $key = null) + { + if (!$this->master instanceof IniModifierInterface) { + throw new IniException('Cannot set comments on master which is only an ini reader'); + } + $this->master->setComments($name, $comments, $section, $key); + } + + /** + * remove comment lines preceding an option from the two ini file. + * + * @param string $name the name of the option to find + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function removeComments($name, $section = 0, $key = null) + { + if ($this->master instanceof IniModifierInterface) { + $this->master->removeComments($name, $section, $key); + } + $this->overrider->removeComments($name, $section, $key); + } + + /** + * remove comment lines preceding an option from master ini file only. + * + * @param string $name the name of the option to find + * @param string $section the section where to set the item. 0 is the global section + * @param int $key for option which is an item of array, the key in the array. + */ + public function removeCommentsOnMaster($name, $section = 0, $key = null) + { + if ($this->master instanceof IniModifierInterface) { + throw new IniException('Cannot remove comments on master which is only an ini reader'); + } + $this->master->removeComments($name, $section, $key); + } + /** * remove a section from the two ini file. diff --git a/tests/IniModifierGetTest.php b/tests/IniModifierNormalGetTest.php similarity index 94% rename from tests/IniModifierGetTest.php rename to tests/IniModifierNormalGetTest.php index a9b4ab7..8fc53d5 100644 --- a/tests/IniModifierGetTest.php +++ b/tests/IniModifierNormalGetTest.php @@ -10,7 +10,7 @@ require_once(__DIR__.'/lib.php'); -class IniModifierGetTest extends PHPUnit_Framework_TestCase { +class IniModifierNormalGetTest extends PHPUnit_Framework_TestCase { function testGetValue() { $parser = new testIniFileModifier('foo.ini', ' @@ -18,6 +18,7 @@ function testGetValue() { foo=bar anumber=98 +anumber_string = "98" string= "uuuuu" string2= "aaa bbb" @@ -25,6 +26,7 @@ function testGetValue() { multiline bbb" afloatnumber= 5.098 +afloatnumber_string = "5.098" [aSection] laurent=toto @@ -57,6 +59,7 @@ function testGetValue() { $this->assertEquals($parser->getValue('foo'), 'bar' ); $this->assertEquals($parser->getValue('anumber'), 98 ); + $this->assertEquals($parser->getValue('anumber_string'), 98 ); $this->assertEquals($parser->getValue('string'), 'uuuuu' ); $this->assertEquals($parser->getValue('string2'), 'aaa bbb'); @@ -64,6 +67,7 @@ function testGetValue() { multiline bbb'); $this->assertEquals($parser->getValue('afloatnumber'), 5.098 ); + $this->assertEquals($parser->getValue('afloatnumber_string'), 5.098 ); $this->assertEquals($parser->getValue('truc','aSection'), null ); $this->assertEquals($parser->getValue('laurent','aSection'), 'toto' ); $this->assertEquals($parser->getValue('trucon','aSection'), true ); diff --git a/tests/IniModifierSetTest.php b/tests/IniModifierNormalSetTest.php similarity index 99% rename from tests/IniModifierSetTest.php rename to tests/IniModifierNormalSetTest.php index 3ee54b3..e2722f2 100644 --- a/tests/IniModifierSetTest.php +++ b/tests/IniModifierNormalSetTest.php @@ -10,7 +10,7 @@ require_once(__DIR__ . '/lib.php'); -class IniModifierSetTest extends PHPUnit_Framework_TestCase +class IniModifierNormalSetTest extends PHPUnit_Framework_TestCase { protected function prepareParserSetValue() @@ -165,9 +165,9 @@ function testSetValue() 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), array(IniModifier::TK_VALUE, 'truc', 'bidule2'), + array(IniModifier::TK_VALUE, 'name', 'toto'), array(IniModifier::TK_WS, ""), array(IniModifier::TK_WS, ""), - array(IniModifier::TK_VALUE, 'name', 'toto'), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -346,9 +346,9 @@ function testSetArrayValue() 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), array(IniModifier::TK_WS, ""), array(IniModifier::TK_WS, ""), - array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -370,15 +370,15 @@ function testSetArrayValue() array(IniModifier::TK_VALUE, 'truc', 'machin'), array(IniModifier::TK_VALUE, 'flag', 'on'), array(IniModifier::TK_VALUE, 'noflag', 'off'), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + array(IniModifier::TK_WS, ""), ), 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), array(IniModifier::TK_WS, ""), array(IniModifier::TK_WS, ""), - array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -413,8 +413,8 @@ function testSetArrayValue2() array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), array(IniModifier::TK_VALUE, 'example', '1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 2), + array(IniModifier::TK_WS, ""), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -428,8 +428,8 @@ function testSetArrayValue2() array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), array(IniModifier::TK_VALUE, 'example', '1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 2), + array(IniModifier::TK_WS, ""), ), 'aSection' => array( array(IniModifier::TK_SECTION, "[aSection]"), @@ -447,8 +447,8 @@ function testSetArrayValue2() array(IniModifier::TK_VALUE, 'foo', 'button'), array(IniModifier::TK_VALUE, 'example', '1'), array(IniModifier::TK_WS, '--'), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), ), 'aSection' => array( array(IniModifier::TK_SECTION, "[aSection]"), @@ -484,9 +484,9 @@ function testSetArrayValueWithArray() 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), array(IniModifier::TK_WS, ""), array(IniModifier::TK_WS, ""), - array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -513,11 +513,11 @@ function testSetArrayValueWithArray() 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), array(IniModifier::TK_VALUE, 'truc', 'machin2'), - array(IniModifier::TK_WS, ""), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'name', 'black', 0), array(IniModifier::TK_ARR_VALUE, 'name', 'brown', 1), array(IniModifier::TK_ARR_VALUE, 'name', 'yellow', 2), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -544,11 +544,11 @@ function testSetArrayValueWithArray() 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), array(IniModifier::TK_VALUE, 'truc', 'machin2'), - array(IniModifier::TK_WS, ""), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'name', 'red', 0), array(IniModifier::TK_ARR_VALUE, 'name', 'white', 1), array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -593,8 +593,8 @@ function testModifyArrayValueWithArray() array(IniModifier::TK_VALUE, 'ddd', 'eee'), array(IniModifier::TK_ARR_VALUE, 'mylist', 'brown', 1), array(IniModifier::TK_VALUE, 'fff', 'ggg'), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'mylist', 'yellow', 2), + array(IniModifier::TK_WS, ""), ), 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), @@ -624,8 +624,8 @@ function testModifyArrayValueWithArray() array(IniModifier::TK_VALUE, 'ddd', 'eee'), array(IniModifier::TK_WS, '--'), array(IniModifier::TK_VALUE, 'fff', 'ggg'), + array(IniModifier::TK_WS, '--'), array(IniModifier::TK_WS, ""), - array(IniModifier::TK_WS, '--') ), 'othersection' => array( array(IniModifier::TK_SECTION, "[othersection]"), @@ -670,8 +670,8 @@ function testSetAssocArrayValue() array(IniModifier::TK_VALUE, 'example', '1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 'key1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 'champ'), + array(IniModifier::TK_WS, ""), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -686,8 +686,8 @@ function testSetAssocArrayValue() array(IniModifier::TK_VALUE, 'example', '1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'modif', 'key1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 'champ'), + array(IniModifier::TK_WS, ""), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -702,8 +702,8 @@ function testSetAssocArrayValue() array(IniModifier::TK_VALUE, 'example', '1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'modif', 'key1'), array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), - array(IniModifier::TK_WS, ""), array(IniModifier::TK_ARR_VALUE, 'foo', 'modif2', 'champ'), + array(IniModifier::TK_WS, ""), ), ); $this->assertEquals($expected, $parser->getContent()); @@ -747,7 +747,6 @@ public function testSetValues() { ; super section [the_section] - truc=machin bidule=1 truck=on @@ -777,7 +776,6 @@ public function testSetExample() { foo[]=baz assoc[key1]=car assoc[otherkey]=bus - foo[]="other value" foo[]=five assoc[ov]="other value" diff --git a/tests/IniModifierNumericGetTest.php b/tests/IniModifierNumericGetTest.php new file mode 100644 index 0000000..fca857c --- /dev/null +++ b/tests/IniModifierNumericGetTest.php @@ -0,0 +1,180 @@ +assertEquals($parser->getValue('foo'), 'bar' ); + $this->assertEquals($parser->getValue('anumber'), 98 ); + $this->assertEquals($parser->getValue('anumber_string'), 98 ); + $this->assertEquals($parser->getValue('string'), 'uuuuu' ); + $this->assertEquals($parser->getValue('string2'), 'aaa +bbb'); + $this->assertEquals($parser->getValue('string3'), 'aaa + multiline +bbb'); + $this->assertEquals($parser->getValue('afloatnumber'), 5.098 ); + $this->assertEquals($parser->getValue('afloatnumber_string'), 5.098 ); + $this->assertEquals($parser->getValue('truc','aSection'), null ); + $this->assertEquals($parser->getValue('laurent','aSection'), 'toto' ); + $this->assertEquals($parser->getValue('trucon','aSection'), "on" ); + $this->assertEquals($parser->getValue('machintrue','aSection'), "true" ); + $this->assertEquals($parser->getValue('bidule1','aSection'), 1 ); + $this->assertEquals($parser->getValue('chouetyes','aSection'), "yes" ); + $this->assertEquals($parser->getValue('trucoff','aSection'), "off" ); + $this->assertEquals($parser->getValue('machinfalse','aSection'), "false" ); + $this->assertEquals($parser->getValue('bidule0','aSection'), 0 ); + $this->assertEquals($parser->getValue('chouetno','aSection'), "no" ); + $this->assertEquals($parser->getValue('bizarre','aSection'), "none" ); + $this->assertEquals($parser->getValue('foo','vla',2), 'ccc' ); + $this->assertEquals($parser->getValue('foo','vla'), array('aaa', 'bbb', 'ccc')); + $this->assertEquals($parser->getValue('foo','the_section'), array('key1'=>'aaa', 'key2'=>"true", 'key4'=>"off", 'key3'=>'ccc')); + } + + + public function testGetValues() { + $content = ' +; a comment getValues('the_section'); + $expected = array('truc'=>'machin', 'bidule'=>1, 'truck'=>"on", 'foo'=>array('aaa', "true", 'off', 'ccc')); + $this->assertEquals($expected, $values); + + $values = $ini->getValues(0); + $expected = array('foo'=>'bar'); + $this->assertEquals($expected, $values); + + $values = $ini->getValues('aSection'); + $expected = array( + 'trucon' => "on", + 'machintrue' => "true", + 'bidule1' => 1, + 'chouetyes' => "yes", + 'trucoff' => "off", + 'machinfalse' => "false", + 'bidule0' => 0, + 'chouetno' => "no", + 'bizarre'=>"none", + ); + $this->assertEquals($expected, $values); + + + } + + public function testGetValuesAssocArray() { + $content = ' +; a comment getValues('the_section'); + $expected = array('truc'=>'machin', 'bidule'=>1, 'truck'=>"on", 'foo'=>array('key1'=>'aaa', 'key2'=>"true", 'key4'=>"off", 'key3'=>'ccc')); + $this->assertEquals($expected, $values); + + $values = $ini->getValues(0); + $expected = array('foo'=>'bar'); + $this->assertEquals($expected, $values); + } + +} diff --git a/tests/IniModifierNumericSetTest.php b/tests/IniModifierNumericSetTest.php new file mode 100644 index 0000000..7af1f9a --- /dev/null +++ b/tests/IniModifierNumericSetTest.php @@ -0,0 +1,731 @@ + array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content, IniModifier::PR_NUMERIC); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + return $parser; + } + + function testSetValue() + { + $parser = $this->prepareParserSetValue(); + // set Simple value + $parser->setValue('foo', 3.14159); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 3.14159), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // set value in a section + $parser->setValue('truc', 3.14159, 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 3.14159), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 3.14159), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // set value in an other section + $parser->setValue('truc', 3.14159, 'othersection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 3.14159), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 3.14159), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 3.14159), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + function testSetExistingValue() + { + $parser = $this->prepareParserSetValue(); + // set Simple value + $parser->setValue('foo', 'bar'); + $parser->setValue('flag', 'on', 'aSection'); + $parser->setValue('noflag', 'off', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + + // set same value in a section + $parser->setValue('truc', 'machin', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + + // set modified value in a section + $parser->setValue('truc', 'bidule', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'bidule'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + + $parser = $this->prepareParserSetValue(); + // set Simple value + $parser->setValue('flag', 'off', 'aSection'); + $parser->setValue('noflag', 'on', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'off'), + array(IniModifier::TK_VALUE, 'noflag', 'on'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $this->assertEquals($parser->getValue('flag', 'aSection'), 'off'); + $this->assertEquals($parser->getValue('noflag', 'aSection'), 'on'); + } + + + function testSetArrayValue() + { + $parser = $this->prepareParserSetValue(); + // append an array value + $parser->setValue('name', 'toto', 'othersection', ''); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // append an array value at the 0 key + $parser->setValue('theme', 'blue', 'aSection', 0); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + function testSetArrayValue2() + { + $content = ' +foo[]=bar +example=1 +foo[]=machine +'; + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + $this->assertEquals($expected, $parser->getContent()); + + $parser->setValue('foo', 'bla', 0, ''); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 2), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('theme', 'blue', 'aSection', '0'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 2), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('foo', 'button'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_VALUE, 'foo', 'button'), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + + function testSetArrayValueWithArray() + { + $parser = $this->prepareParserSetValue(); + // append an array value + $parser->setValue('name', 'toto', 'othersection', ''); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // now set a new value which is an array + $parser->setValue('name', array('black', 'brown', 'yellow'), 'othersection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'black', 0), + array(IniModifier::TK_ARR_VALUE, 'name', 'brown', 1), + array(IniModifier::TK_ARR_VALUE, 'name', 'yellow', 2), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // now change it by a smaller array + $parser->setValue('name', array('red', 'white'), 'othersection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'red', 0), + array(IniModifier::TK_ARR_VALUE, 'name', 'white', 1), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + function testModifyArrayValueWithArray() + { + $content = ' + ; a comment + +foo=bar + +[aSection] +truc=machin +mylist[]=hello +ddd=eee +mylist[]=bar +fff=ggg + +[othersection] +truc=machin2 + +'; + $parser = new testIniFileModifier('foo.ini', $content); + + // now set a new value which is an array + $parser->setValue('mylist', array('black', 'brown', 'yellow'), 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'black', 0), + array(IniModifier::TK_VALUE, 'ddd', 'eee'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'brown', 1), + array(IniModifier::TK_VALUE, 'fff', 'ggg'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'yellow', 2), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // now change it by a smaller array + $parser->setValue('mylist', array('red'), 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'red', 0), + array(IniModifier::TK_VALUE, 'ddd', 'eee'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_VALUE, 'fff', 'ggg'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + } + + function testSetAssocArrayValue() + { + $content = ' +foo[]=bar +example=1 +foo[key1]=machine +foo[]=vla +'; + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + $this->assertEquals($expected, $parser->getContent()); + + $parser->setValue('foo', 'bla', 0, 'champ'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 'champ'), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('foo', 'modif', 0, 'key1'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'modif', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 'champ'), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('foo', 'modif2', 0, 'champ'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'modif', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_ARR_VALUE, 'foo', 'modif2', 'champ'), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + + public function testSetValues() { + $content = ' +; a comment setValues( + array( + 'truc'=>'machin', + 'bidule'=>1, + 'truck'=>true, + 'notruck'=>false, + 'foo'=>array('aaa', 'bbb', 'machin'=>'ccc') + ), + 'the_section'); + $expected = ' +; a comment assertEquals($expected, $ini->generate()); + $this->assertTrue($ini->isModified()); + $ini->clearModifierFlag(); + } + + + + public function testSetExample() { + $content = 'foo[]=bar +foo[]=baz +assoc[key1]=car +assoc[otherkey]=bus +'; + $ini = new testIniFileModifier('foo.ini', $content); + $ini->setValue('foo', 'other value', 0, ''); + $ini->setValue('foo', 'five', 0, 5); + $ini->setValue('assoc', 'other value', 0, 'ov'); + $expected = 'foo[]=bar +foo[]=baz +assoc[key1]=car +assoc[otherkey]=bus +foo[]="other value" +foo[]=five +assoc[ov]="other value" +'; + $this->assertEquals($expected, $ini->generate()); + $this->assertTrue($ini->isModified()); + $ini->clearModifierFlag(); + } + +} diff --git a/tests/IniModifierRawGetTest.php b/tests/IniModifierRawGetTest.php new file mode 100644 index 0000000..a76d641 --- /dev/null +++ b/tests/IniModifierRawGetTest.php @@ -0,0 +1,180 @@ +assertEquals($parser->getValue('foo'), 'bar' ); + $this->assertEquals($parser->getValue('anumber'), '98' ); + $this->assertEquals($parser->getValue('anumber_string'), '98' ); + $this->assertEquals($parser->getValue('string'), 'uuuuu' ); + $this->assertEquals($parser->getValue('string2'), 'aaa +bbb'); + $this->assertEquals($parser->getValue('string3'), 'aaa + multiline +bbb'); + $this->assertEquals($parser->getValue('afloatnumber'), '5.098' ); + $this->assertEquals($parser->getValue('afloatnumber_string'), '5.098' ); + $this->assertEquals($parser->getValue('truc','aSection'), null ); + $this->assertEquals($parser->getValue('laurent','aSection'), 'toto' ); + $this->assertEquals($parser->getValue('trucon','aSection'), "on" ); + $this->assertEquals($parser->getValue('machintrue','aSection'), "true" ); + $this->assertEquals($parser->getValue('bidule1','aSection'), '1' ); + $this->assertEquals($parser->getValue('chouetyes','aSection'), "yes" ); + $this->assertEquals($parser->getValue('trucoff','aSection'), "off" ); + $this->assertEquals($parser->getValue('machinfalse','aSection'), "false" ); + $this->assertEquals($parser->getValue('bidule0','aSection'), '0' ); + $this->assertEquals($parser->getValue('chouetno','aSection'), "no" ); + $this->assertEquals($parser->getValue('bizarre','aSection'), "none" ); + $this->assertEquals($parser->getValue('foo','vla',2), 'ccc' ); + $this->assertEquals($parser->getValue('foo','vla'), array('aaa', 'bbb', 'ccc')); + $this->assertEquals($parser->getValue('foo','the_section'), array('key1'=>'aaa', 'key2'=>"true", 'key4'=>"off", 'key3'=>'ccc')); + } + + + public function testGetValues() { + $content = ' +; a comment getValues('the_section'); + $expected = array('truc'=>'machin', 'bidule'=>'1', 'truck'=>"on", 'foo'=>array('aaa', "true", 'off', 'ccc')); + $this->assertEquals($expected, $values); + + $values = $ini->getValues(0); + $expected = array('foo'=>'bar'); + $this->assertEquals($expected, $values); + + $values = $ini->getValues('aSection'); + $expected = array( + 'trucon' => "on", + 'machintrue' => "true", + 'bidule1' => '1', + 'chouetyes' => "yes", + 'trucoff' => "off", + 'machinfalse' => "false", + 'bidule0' => '0', + 'chouetno' => "no", + 'bizarre'=>"none", + ); + $this->assertEquals($expected, $values); + + + } + + public function testGetValuesAssocArray() { + $content = ' +; a comment getValues('the_section'); + $expected = array('truc'=>'machin', 'bidule'=>'1', 'truck'=>"on", 'foo'=>array('key1'=>'aaa', 'key2'=>"true", 'key4'=>"off", 'key3'=>'ccc')); + $this->assertEquals($expected, $values); + + $values = $ini->getValues(0); + $expected = array('foo'=>'bar'); + $this->assertEquals($expected, $values); + } + +} diff --git a/tests/IniModifierRawSetTest.php b/tests/IniModifierRawSetTest.php new file mode 100644 index 0000000..7bdab39 --- /dev/null +++ b/tests/IniModifierRawSetTest.php @@ -0,0 +1,731 @@ + array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content, IniModifier::PR_RAW); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + return $parser; + } + + function testSetValue() + { + $parser = $this->prepareParserSetValue(); + // set Simple value + $parser->setValue('foo', 3.14159); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', '3.14159'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // set value in a section + $parser->setValue('truc', 3.14159, 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', '3.14159'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', '3.14159'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // set value in an other section + $parser->setValue('truc', 3.14159, 'othersection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', '3.14159'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', '3.14159'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', '3.14159'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + function testSetExistingValue() + { + $parser = $this->prepareParserSetValue(); + // set Simple value + $parser->setValue('foo', 'bar'); + $parser->setValue('flag', 'on', 'aSection'); + $parser->setValue('noflag', 'off', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + + // set same value in a section + $parser->setValue('truc', 'machin', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + + // set modified value in a section + $parser->setValue('truc', 'bidule', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'bidule'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + + $parser = $this->prepareParserSetValue(); + // set Simple value + $parser->setValue('flag', 'off', 'aSection'); + $parser->setValue('noflag', 'on', 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'off'), + array(IniModifier::TK_VALUE, 'noflag', 'on'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $this->assertEquals($parser->getValue('flag', 'aSection'), 'off'); + $this->assertEquals($parser->getValue('noflag', 'aSection'), 'on'); + } + + + function testSetArrayValue() + { + $parser = $this->prepareParserSetValue(); + // append an array value + $parser->setValue('name', 'toto', 'othersection', ''); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // append an array value at the 0 key + $parser->setValue('theme', 'blue', 'aSection', 0); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + function testSetArrayValue2() + { + $content = ' +foo[]=bar +example=1 +foo[]=machine +'; + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + $this->assertEquals($expected, $parser->getContent()); + + $parser->setValue('foo', 'bla', 0, ''); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 2), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('theme', 'blue', 'aSection', '0'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 1), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 2), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('foo', 'button'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_VALUE, 'foo', 'button'), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_ARR_VALUE, 'theme', 'blue', 0), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + + function testSetArrayValueWithArray() + { + $parser = $this->prepareParserSetValue(); + // append an array value + $parser->setValue('name', 'toto', 'othersection', ''); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'toto', 0), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // now set a new value which is an array + $parser->setValue('name', array('black', 'brown', 'yellow'), 'othersection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'black', 0), + array(IniModifier::TK_ARR_VALUE, 'name', 'brown', 1), + array(IniModifier::TK_ARR_VALUE, 'name', 'yellow', 2), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // now change it by a smaller array + $parser->setValue('name', array('red', 'white'), 'othersection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_ARR_VALUE, 'name', 'red', 0), + array(IniModifier::TK_ARR_VALUE, 'name', 'white', 1), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + function testModifyArrayValueWithArray() + { + $content = ' + ; a comment + +foo=bar + +[aSection] +truc=machin +mylist[]=hello +ddd=eee +mylist[]=bar +fff=ggg + +[othersection] +truc=machin2 + +'; + $parser = new testIniFileModifier('foo.ini', $content); + + // now set a new value which is an array + $parser->setValue('mylist', array('black', 'brown', 'yellow'), 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'black', 0), + array(IniModifier::TK_VALUE, 'ddd', 'eee'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'brown', 1), + array(IniModifier::TK_VALUE, 'fff', 'ggg'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'yellow', 2), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // now change it by a smaller array + $parser->setValue('mylist', array('red'), 'aSection'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, " ; a comment"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_ARR_VALUE, 'mylist', 'red', 0), + array(IniModifier::TK_VALUE, 'ddd', 'eee'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_VALUE, 'fff', 'ggg'), + array(IniModifier::TK_WS, '--'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + } + + function testSetAssocArrayValue() + { + $content = ' +foo[]=bar +example=1 +foo[key1]=machine +foo[]=vla +'; + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + $this->assertEquals($expected, $parser->getContent()); + + $parser->setValue('foo', 'bla', 0, 'champ'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'machine', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 'champ'), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('foo', 'modif', 0, 'key1'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'modif', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bla', 'champ'), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + $parser->setValue('foo', 'modif2', 0, 'champ'); + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'foo', 'bar', 0), + array(IniModifier::TK_VALUE, 'example', '1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'modif', 'key1'), + array(IniModifier::TK_ARR_VALUE, 'foo', 'vla', 2), + array(IniModifier::TK_ARR_VALUE, 'foo', 'modif2', 'champ'), + array(IniModifier::TK_WS, ""), + ), + ); + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + + public function testSetValues() { + $content = ' +; a comment setValues( + array( + 'truc'=>'machin', + 'bidule'=>1, + 'truck'=>true, + 'notruck'=>false, + 'foo'=>array('aaa', 'bbb', 'machin'=>'ccc') + ), + 'the_section'); + $expected = ' +; a comment assertEquals($expected, $ini->generate()); + $this->assertTrue($ini->isModified()); + $ini->clearModifierFlag(); + } + + + + public function testSetExample() { + $content = 'foo[]=bar +foo[]=baz +assoc[key1]=car +assoc[otherkey]=bus +'; + $ini = new testIniFileModifier('foo.ini', $content); + $ini->setValue('foo', 'other value', 0, ''); + $ini->setValue('foo', 'five', 0, 5); + $ini->setValue('assoc', 'other value', 0, 'ov'); + $expected = 'foo[]=bar +foo[]=baz +assoc[key1]=car +assoc[otherkey]=bus +foo[]="other value" +foo[]=five +assoc[ov]="other value" +'; + $this->assertEquals($expected, $ini->generate()); + $this->assertTrue($ini->isModified()); + $ini->clearModifierFlag(); + } + +} diff --git a/tests/IniModifierRemoveCommentsTest.php b/tests/IniModifierRemoveCommentsTest.php new file mode 100644 index 0000000..c64f85f --- /dev/null +++ b/tests/IniModifierRemoveCommentsTest.php @@ -0,0 +1,193 @@ + array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, "; global foo comment "), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_COMMENT, "; truc comment"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment 1"), + array(IniModifier::TK_COMMENT, "; flag comment 2"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_COMMENT, "; bedo a comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_COMMENT, "; bedo b comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + return $parser; + } + + function testRemoveComments() + { + $parser = $this->prepareParserRemoveComments(); + + // remove single comment - global + $parser->removeComments('foo'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_COMMENT, "; truc comment"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment 1"), + array(IniModifier::TK_COMMENT, "; flag comment 2"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_COMMENT, "; bedo a comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_COMMENT, "; bedo b comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // remove single comment + $parser->removeComments('truc', 'aSection'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment 1"), + array(IniModifier::TK_COMMENT, "; flag comment 2"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_COMMENT, "; bedo a comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_COMMENT, "; bedo b comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // remove multiline comment + $parser->removeComments('flag', 'aSection'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_COMMENT, "; bedo a comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_COMMENT, "; bedo b comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // remove single comment with key + $parser->removeComments('bedo', 'aSection', 'b'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_COMMENT, "; bedo a comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } +} diff --git a/tests/IniModifierSetCommentsTest.php b/tests/IniModifierSetCommentsTest.php new file mode 100644 index 0000000..56e04b5 --- /dev/null +++ b/tests/IniModifierSetCommentsTest.php @@ -0,0 +1,236 @@ + array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, "; global foo comment "), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + return $parser; + } + + function testSetComments() + { + $parser = $this->prepareParserSetComments(); + + // set single comment - global + $parser->setComments('foo', "; new global foo comment"); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_COMMENT, "; new global foo comment"), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // set single comment - in section + $parser->setComments('truc', "truc comment", 'aSection'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_COMMENT, "; new global foo comment"), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_COMMENT, ";truc comment"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + + // set multiline comment + $parser->setComments('truc', ["othersection truc line 1" , "othersection truc line 2"], 'othersection'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, "--"), + array(IniModifier::TK_WS, " "), + array(IniModifier::TK_COMMENT, "; new global foo comment"), + array(IniModifier::TK_VALUE, 'foo', 'bar'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_COMMENT, ";truc comment"), + array(IniModifier::TK_VALUE, 'truc', 'machin'), + array(IniModifier::TK_COMMENT, "; flag comment"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + 'othersection' => array( + array(IniModifier::TK_SECTION, "[othersection]"), + array(IniModifier::TK_COMMENT, ";othersection truc line 1"), + array(IniModifier::TK_COMMENT, ";othersection truc line 2"), + array(IniModifier::TK_VALUE, 'truc', 'machin2'), + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } + + protected function prepareParserSetCommentsWithKey() + { + $content = ' +bedo[a]="bedo a" +bedo[b]="bedo b" + +[aSection] +truc[a]="machin a" +truc[b]="machin b" +; flag comment +flag=on +noflag=off +'; + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_ARR_VALUE, 'truc', 'machin a', 'a'), + array(IniModifier::TK_ARR_VALUE, 'truc', 'machin b', 'b'), + array(IniModifier::TK_COMMENT, "; flag comment"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $parser = new testIniFileModifier('foo.ini', $content); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertFalse($parser->isModified()); + return $parser; + } + + function testSetCommentsWithKey() + { + $parser = $this->prepareParserSetCommentsWithKey(); + + // set single comment - global + $parser->setComments('bedo', "; bedo a comment", 0, 'a'); + + $expected = array( + 0 => array( + array(IniModifier::TK_WS, ""), + array(IniModifier::TK_COMMENT, "; bedo a comment"), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo a', 'a'), + array(IniModifier::TK_ARR_VALUE, 'bedo', 'bedo b', 'b'), + array(IniModifier::TK_WS, ""), + ), + 'aSection' => array( + array(IniModifier::TK_SECTION, "[aSection]"), + array(IniModifier::TK_ARR_VALUE, 'truc', 'machin a', 'a'), + array(IniModifier::TK_ARR_VALUE, 'truc', 'machin b', 'b'), + array(IniModifier::TK_COMMENT, "; flag comment"), + array(IniModifier::TK_VALUE, 'flag', 'on'), + array(IniModifier::TK_VALUE, 'noflag', 'off'), + array(IniModifier::TK_WS, ""), + ), + ); + + $this->assertEquals($expected, $parser->getContent()); + $this->assertTrue($parser->isModified()); + $parser->clearModifierFlag(); + } +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml index af9081c..cd5a942 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -3,9 +3,15 @@ IniModifierTest.php - IniModifierSetTest.php - IniModifierGetTest.php + IniModifierNormalSetTest.php + IniModifierNumericSetTest.php + IniModifierRawSetTest.php + IniModifierNormalGetTest.php + IniModifierNumericGetTest.php + IniModifierRawGetTest.php IniModifierRemoveTest.php + IniModifierSetCommentsTest.php + IniModifierRemoveCommentsTest.php IniModifierRenameTest.php IniModifierImportTest.php MultiIniModifierTest.php