Completed
Push — master ( ff7be8...06b891 )
by Taosikai
13:34
created

IniParserTest::testWriteToString()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 38
rs 8.8571
c 1
b 0
f 0
1
<?php
2
namespace Slince\Config\Tests\Parser;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Config\Exception\ParseException;
6
use Slince\Config\Parser\IniParser;
7
8
class IniParserTest extends TestCase
9
{
10
    public function testParse()
11
    {
12
        $parser = new IniParser();
13
        $data = $parser->parse(__DIR__ . '/../Fixtures/config.ini');
14
        $this->assertEquals('baz', $data['foo']['bar']);
15
    }
16
17
    public function testException()
18
    {
19
        $this->setExpectedException(ParseException::class);
20
        (new IniParser())->parse(__DIR__ . '/../Fixtures/syntax_error_ini_file.ini');
21
    }
22
23
    public function testDump()
24
    {
25
        $parser = new IniParser();
26
        $file = __DIR__ . '/../Tmp/ini-dump.ini';
27
        $this->assertTrue($parser->dump($file, [
28
            'foo' => [
29
                'bar' => [
30
                    'foo',
31
                    'bar',
32
                    'baz'
33
                ]
34
            ]
35
        ]));
36
        $this->assertEquals([
37
            'foo' => [
38
                'bar' => [
39
                    'foo',
40
                    'bar',
41
                    'baz'
42
                ]
43
            ]
44
        ], $parser->parse($file));
45
    }
46
47
    public function testWriteToString()
48
    {
49
        $config = array(
50
            'Section 1' => array(
51
                'foo' => 'bar',
52
                'bool_true' => true,
53
                'bool_false' => false,
54
                'int' => 10,
55
                'float' => 10.3,
56
                'array' => array(
57
                    'string',
58
                    10.3,
59
                    true,
60
                    false,
61
                ),
62
            ),
63
            'Section 2' => array(
64
                'foo' => 'bar',
65
            ),
66
        );
67
        $expected = <<<EOT
68
[Section 1]
69
foo = "bar"
70
bool_true = 1
71
bool_false = 0
72
int = 10
73
float = 10.3
74
array[] = "string"
75
array[] = 10.3
76
array[] = 1
77
array[] = 0
78
79
[Section 2]
80
foo = "bar"
81
EOT;
82
        $this->assertEquals(parse_ini_string($expected, true),
83
            parse_ini_string(IniParser::writeToString($config), true));
84
    }
85
}
86