Skip to content
Snippets Groups Projects
Commit 7887c0cd authored by Demian Katz's avatar Demian Katz
Browse files

Added ability to seed Config\Writer with raw string.

Expanded test suite for writer.
parent dc833a1c
No related merge requests found
......@@ -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;
}
}
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment