diff --git a/module/VuFind/src/VuFind/Config/Writer.php b/module/VuFind/src/VuFind/Config/Writer.php index 315a633bbbdb7a9f1dc17b15a68683cebac5bae7..360938b88018cd6ac722dec51fb50187a3d24aea 100644 --- a/module/VuFind/src/VuFind/Config/Writer.php +++ b/module/VuFind/src/VuFind/Config/Writer.php @@ -55,24 +55,27 @@ class Writer /** * Constructor * - * @param string $filename Configuration file to write - * @param array|null $content Content to load into file (set to null to load - * contents of existing file specified by $filename) - * @param array $comments Comments to associate with content (ignored if - * $content is null). + * @param string $filename Configuration file to write + * @param string|array|null $content Content to load into file (set to null to + * load contents of existing file specified by $filename; set to array to build + * string in combination with $comments; set to string to use raw config string) + * @param array $comments Comments to associate with content (ignored + * if $content is not an array). * * @throws \Exception */ public function __construct($filename, $content = null, $comments = array()) { $this->filename = $filename; - if (is_null($content)) { + if (null === $content) { $this->content = file_get_contents($filename); if (!$this->content) { throw new \Exception('Could not read ' . $filename); } - } else { + } else if (is_array($content)) { $this->content = $this->buildContent($content, $comments); + } else { + $this->content = $content; } } diff --git a/module/VuFind/tests/unit-tests/src/Config/WriterTest.php b/module/VuFind/tests/unit-tests/src/Config/WriterTest.php index 0efa3fb6a74c0875b457be10657f1caa59a0fb16..497394503ce7539adf4f876730e3cdcc5cb98caa 100644 --- a/module/VuFind/tests/unit-tests/src/Config/WriterTest.php +++ b/module/VuFind/tests/unit-tests/src/Config/WriterTest.php @@ -81,4 +81,33 @@ class WriterTest extends \VuFindTest\Unit\TestCase $test = new Writer('fake.ini', $cfg, $comments); $this->assertEquals($target, $test->getContent()); } + + /** + * Test reading from a string. + * + * @return void + */ + public function testReadString() + { + $cfg = "[test]\nkey1=val1\n"; + $test = new Writer('fake.ini', $cfg); + $this->assertEquals($cfg, $test->getContent()); + } + + /** + * Test setting a value. + * + * @return void + */ + public function testBasicSet() + { + $cfg = "[test]\nkey1=val1\nkey3=val3\n"; + $test = new Writer('fake.ini', $cfg); + $test->set('test', 'key2', 'val2'); + $test->set('test', 'key1', 'val1b'); + $ini = parse_ini_string($test->getContent(), true); + $this->assertEquals('val1b', $ini['test']['key1']); + $this->assertEquals('val2', $ini['test']['key2']); + $this->assertEquals('val3', $ini['test']['key3']); + } } \ No newline at end of file