Skip to content

Commit 44a03e9

Browse files
committed
Add writer tests
1 parent f7dd4fb commit 44a03e9

File tree

3 files changed

+132
-5
lines changed

3 files changed

+132
-5
lines changed

src/Config.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,31 @@ class Config extends AbstractConfig
2929
/**
3030
* Writes configuration to string.
3131
*
32-
* @param WriterInterface $writer Configuration writer
32+
* @param WriterInterface $writer Configuration writer
33+
* @param array $options Writer options (optional)
3334
*
3435
* @return string Encoded configuration string
3536
*
3637
* @throws WriteException If there is an error while writing a string
3738
*/
38-
public function toString(WriterInterface $writer)
39+
public function toString(WriterInterface $writer, $options = [])
3940
{
40-
return $writer->write($this);
41+
return $writer->write($this, $options);
4142
}
4243

4344
/**
4445
* Writes configuration to file.
4546
*
4647
* @param string $filename Configuration file name
4748
* @param WriterInterface $writer Configuration writer (optional)
49+
* @param array $options Writer options (optional)
4850
*
4951
* @return void
5052
*
5153
* @throws UnsupportedFormatException If file extension is unsupported
5254
* @throws WriteException If there is an error while writing a file
5355
*/
54-
public function toFile($filename, WriterInterface $writer = null)
56+
public function toFile($filename, WriterInterface $writer = null, $options = [])
5557
{
5658
if ($writer === null) {
5759
// Get file information
@@ -68,7 +70,7 @@ public function toFile($filename, WriterInterface $writer = null)
6870
$writer = $this->getWriter($extension);
6971
}
7072

71-
$data = $this->toString($writer);
73+
$data = $this->toString($writer, $options);
7274

7375
// @codeCoverageIgnoreStart
7476
if (!is_dir(dirname($filename))) {

src/Writers/WriterInterface.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ConfigWriter\Writers;
4+
5+
use ConfigWriter\Exceptions\WriteException;
6+
use ConfigWriter\AbstractConfig;
7+
8+
/**
9+
* Configuration writer interface.
10+
*
11+
* @since 2.0.0
12+
*
13+
* @author Filip Š <projects@filips.si>
14+
*
15+
* @license MIT
16+
*
17+
* @package ConfigWriter
18+
*/
19+
20+
interface WriterInterface
21+
{
22+
/**
23+
* Returns configuration from `$config` as encoded string.
24+
*
25+
* @param AbstractWriter $config Configuration
26+
* @param array $options Writer options (optional)
27+
*
28+
* @return string Encoded configuration string
29+
*
30+
* @throws WriteException If there is an error while writing a string
31+
*/
32+
public function write(AbstractConfig $config, $options = []);
33+
34+
/**
35+
* Returns an array of allowed file extensions for this writer.
36+
*
37+
* @return array Array of file extensions
38+
*/
39+
public static function getSupportedExtensions();
40+
}

tests/ConfigTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace ConfigWriter\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ConfigWriter\Config;
7+
8+
class ConfigTest extends TestCase
9+
{
10+
/**
11+
* @var \ConfigWriter\Config
12+
*/
13+
protected $config;
14+
15+
public function setUp()
16+
{
17+
$config = new Config;
18+
19+
$config->addRecord('settings.development.debug', true, 'Debugging');
20+
$config->addRecord('settings.development.errors', E_ALL, 'Error reporting');
21+
22+
$config->addRecord('database.host', 'localhost', 'Database hostname');
23+
$config->addRecord('database.port', 3306, 'Database port');
24+
25+
$config->addRecord('title', 'My Website', 'Website title');
26+
$config->addRecord('description', 'This is my cool website!', 'Website descroption');
27+
28+
$database = $config->addSection('database', [], 'Database settings');
29+
30+
$database->addRecord('database.user', 'root', 'Database username');
31+
$database->addRecord('database.pass', '', 'Database password');
32+
33+
$this->config = $config;
34+
}
35+
36+
/**
37+
* @covers \ConfigWriter\Config::getWriter()
38+
* @covers \ConfigWriter\Config::toFile()
39+
* @covers \ConfigWriter\Config::toString()
40+
*/
41+
public function testWriting()
42+
{
43+
$filename = sys_get_temp_dir() . '/ConfigWriterTests/' . uniqid(rand()) . '.php';
44+
$this->config->toFile($filename);
45+
46+
$this->assertFileExists($filename);
47+
}
48+
49+
/**
50+
* @covers \ConfigWriter\Config::getWriter()
51+
* @covers \ConfigWriter\Config::toFile()
52+
* @covers \ConfigWriter\Config::toString()
53+
*/
54+
public function testWritingWithDistExtension()
55+
{
56+
$filename = sys_get_temp_dir() . '/ConfigWriterTests/' . uniqid(rand()) . '.php.dist';
57+
$this->config->toFile($filename);
58+
59+
$this->assertFileExists($filename);
60+
}
61+
62+
/**
63+
* @covers \ConfigWriter\Config::toFile()
64+
* @covers \ConfigWriter\Config::toString()
65+
*/
66+
public function testWritingCustomWriter()
67+
{
68+
$filename = sys_get_temp_dir() . '/ConfigWriterTests/' . uniqid(rand()) . '.txt';
69+
$this->config->toFile($filename, new \ConfigWriter\Writers\PhpWriter);
70+
71+
$this->assertFileExists($filename);
72+
}
73+
74+
/**
75+
* @covers \ConfigWriter\Config::getWriter()
76+
* @covers \ConfigWriter\Config::toFile()
77+
*
78+
* @expectedException \ConfigWriter\Exceptions\UnsupportedFormatException
79+
*/
80+
public function testWritingUnsupportedFormat()
81+
{
82+
$filename = sys_get_temp_dir() . '/ConfigWriterTests/' . uniqid(rand()) . '.png';
83+
$this->config->toFile($filename);
84+
}
85+
}

0 commit comments

Comments
 (0)