Completed
Push — master ( 506011...44e905 )
by Taosikai
11:27
created

ConfigTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 132
rs 10
c 1
b 0
f 0
wmc 12
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadPHPFile() 0 6 1
A testLoadIniFile() 0 6 1
A testLoadJsonFile() 0 6 1
A testLoadXmlFile() 0 6 1
A testLoadYamlFile() 0 6 1
A testDumpPHPFile() 0 15 1
A testDumpIniFile() 0 15 1
A testDumpJsonFile() 0 15 1
A testDumpXmlFile() 0 15 1
A testDumpYamlFile() 0 15 1
A testLoadDirectory() 0 8 1
A testUnsupportedFormat() 0 5 1
1
<?php
2
namespace Slince\Config\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Config\Config;
6
use Slince\Config\Exception\ParseException;
7
use Slince\Config\Exception\UnsupportedFormatException;
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('bar', $config->get('foo'));
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' => 'bar',
67
            'bar' => [
68
                'foo',
69
                'bar',
70
                'baz'
71
            ]
72
        ]);
73
        $targetFile = __DIR__ . '/Tmp/config-dump.ini';
74
        $this->setExpectedException(ParseException::class);
75
        $config->dump($targetFile);
76
    }
77
78
    public function testDumpJsonFile()
79
    {
80
        $config = new Config();
81
        $config->merge([
82
            'foo' => 'bar',
83
            'bar' => [
84
                'foo',
85
                'bar',
86
                'baz'
87
            ]
88
        ]);
89
        $targetFile = __DIR__ . '/Tmp/config-dump.json';
90
        $this->assertTrue($config->dump($targetFile));
91
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
92
    }
93
94
    public function testDumpXmlFile()
95
    {
96
        $config = new Config();
97
        $config->merge([
98
            'foo' => 'bar',
99
            'bar' => [
100
                'foo',
101
                'bar',
102
                'baz'
103
            ]
104
        ]);
105
        $targetFile = __DIR__ . '/Tmp/config-dump.xml';
106
        $this->assertTrue($config->dump($targetFile));
107
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
108
    }
109
110
    public function testDumpYamlFile()
111
    {
112
        $config = new Config();
113
        $config->merge([
114
            'foo' => 'bar',
115
            'bar' => [
116
                'foo',
117
                'bar',
118
                'baz'
119
            ]
120
        ]);
121
        $targetFile = __DIR__ . '/Tmp/config-dump.yaml';
122
        $this->assertTrue($config->dump($targetFile));
123
        $this->assertEquals($config->toArray(), (new Config($targetFile))->toArray());
124
    }
125
126
    public function testLoadDirectory()
127
    {
128
        $config = new Config(__DIR__ . '/Fixtures/config');
129
        $this->assertCount(3, $config);
130
        $this->assertEquals('bar', $config->get('foo'));
131
        $this->assertEquals('baz', $config->get('bar'));
132
        $this->assertEquals('foo', $config->get('baz'));
133
    }
134
135
    public function testUnsupportedFormat()
136
    {
137
        $this->setExpectedException(UnsupportedFormatException::class);
138
        new Config(__DIR__ . '/Fixtures/config.ext');
139
    }
140
}
141