From 7887c0cdb8858d8ab5fa1718144af4a4f55a6755 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Mon, 13 May 2013 14:01:45 -0400 Subject: [PATCH] Added ability to seed Config\Writer with raw string. Expanded test suite for writer. --- module/VuFind/src/VuFind/Config/Writer.php | 17 ++++++----- .../unit-tests/src/Config/WriterTest.php | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/module/VuFind/src/VuFind/Config/Writer.php b/module/VuFind/src/VuFind/Config/Writer.php index 315a633bbbd..360938b8801 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 0efa3fb6a74..497394503ce 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 -- GitLab