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

FooParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Slince\Config\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Config\Config;
6
use Slince\Config\Exception\UnsupportedFormatException;
7
use Slince\Config\Parser\ParserInterface;
8
9
class ConfigTest extends TestCase
10
{
11
    public function testLoadPHPFile()
12
    {
13
        $config = new Config();
14
        $config->load(__DIR__ . '/Fixtures/config.php');
15
        $this->assertEquals('bar', $config->get('foo'));
16
    }
17
18
    public function testLoadIniFile()
19
    {
20
        $config = new Config();
21
        $config->load(__DIR__ . '/Fixtures/config.ini');
22
        $this->assertEquals('baz', $config['foo']['bar']);
23
    }
24
25
    public function testLoadJsonFile()
26
    {
27
        $config = new Config();
28
        $config->load(__DIR__ . '/Fixtures/config.json');
29
        $this->assertEquals('bar', $config->get('foo'));
30
    }
31
32
    public function testLoadXmlFile()
33
    {
34
        $config = new Config();
35
        $config->load(__DIR__ . '/Fixtures/config.xml');
36
        $this->assertEquals('bar', $config['foo']);
37
    }
38
39
    public function testLoadYamlFile()
40
    {
41
        $config = new Config();
42
        $config->load(__DIR__ . '/Fixtures/config.yml');
43
        $this->assertEquals('bar', $config->get('foo'));
44
    }
45
46
    public function testDumpPHPFile()
47
    {
48
        $config = new Config();
49
        $config->merge([
50
            'foo' => 'bar',
51
            'bar' => [
52
                'foo',
53
                'bar',
54
                'baz'
55
            ]
56
        ]);
57
        $targetFile = __DIR__ . '/Tmp/config-dump.php';
58
        $this->assertTrue($config->dump($targetFile));
59
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
60
    }
61
62
    public function testDumpIniFile()
63
    {
64
        $config = new Config();
65
        $config->merge([
66
            'foo' => [
67
                'bar' => [
68
                    'foo',
69
                    'bar',
70
                    'baz'
71
                ]
72
            ]
73
        ]);
74
        $targetFile = __DIR__ . '/Tmp/config-dump.ini';
75
        $this->assertTrue($config->dump($targetFile));
76
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
77
    }
78
79
    public function testDumpJsonFile()
80
    {
81
        $config = new Config();
82
        $config->merge([
83
            'foo' => 'bar',
84
            'bar' => [
85
                'foo',
86
                'bar',
87
                'baz'
88
            ]
89
        ]);
90
        $targetFile = __DIR__ . '/Tmp/config-dump.json';
91
        $this->assertTrue($config->dump($targetFile));
92
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
93
    }
94
95
    public function testDumpXmlFile()
96
    {
97
        $config = new Config();
98
        $config->merge([
99
            'foo' => 'bar',
100
            'bar' => [
101
                'foo',
102
                'bar',
103
                'baz'
104
            ]
105
        ]);
106
        $targetFile = __DIR__ . '/Tmp/config-dump.xml';
107
        $this->assertTrue($config->dump($targetFile));
108
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
109
    }
110
111
    public function testDumpYamlFile()
112
    {
113
        $config = new Config();
114
        $config->merge([
115
            'foo' => 'bar',
116
            'bar' => [
117
                'foo',
118
                'bar',
119
                'baz'
120
            ]
121
        ]);
122
        $targetFile = __DIR__ . '/Tmp/config-dump.yaml';
123
        $this->assertTrue($config->dump($targetFile));
124
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
125
    }
126
127
    public function testLoadDirectory()
128
    {
129
        $config = new Config(__DIR__ . '/Fixtures/config');
130
        $this->assertCount(3, $config);
131
        $this->assertEquals('bar', $config->get('foo'));
132
        $this->assertEquals('baz', $config->get('bar'));
133
        $this->assertEquals('foo', $config->get('baz'));
134
    }
135
136
    public function testUnsupportedFormat()
137
    {
138
        $this->setExpectedException(UnsupportedFormatException::class);
139
        new Config(__DIR__ . '/Fixtures/config.ext');
140
    }
141
142
    public function testAddParser()
143
    {
144
        Config::addParser(new FooParser());
145
        $config = new Config();
146
        $config->load(__DIR__ . '/Fixtures/config.ext');
147
        $this->assertEquals('baz', $config->get('foo'));
148
    }
149
}
150
151
class FooParser implements ParserInterface
152
{
153
    public function parse($file)
154
    {
155
        return [
156
            'foo' => 'baz'
157
        ];
158
    }
159
160
    public function dump($file, array $data)
161
    {
162
    }
163
164
    public static function getSupportedExtensions()
165
    {
166
        return ['ext'];
167
    }
168
}
169