Passed
Push — master ( 73c257...7773a0 )
by Chema
01:11 queued 12s
created

ConfigReaderTest::test_no_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Framework\Config;
6
7
use Gacela\Framework\Config\ConfigInit;
8
use Gacela\Framework\Config\ConfigReaderException;
9
use Gacela\Framework\Config\ConfigReaderInterface;
10
use Gacela\Framework\Config\GacelaJsonConfig;
11
use Gacela\Framework\Config\GacelaJsonConfigFactoryInterface;
12
use Gacela\Framework\Config\PathFinderInterface;
13
use PHPUnit\Framework\TestCase;
14
15
final class ConfigReaderTest extends TestCase
16
{
17
    public function test_no_config(): void
18
    {
19
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
20
        $gacelaJsonConfigCreator
21
            ->method('createGacelaJsonConfig')
22
            ->willReturn(GacelaJsonConfig::withDefaults());
23
24
        $configInit = new ConfigInit(
25
            'application_root_dir',
26
            $gacelaJsonConfigCreator,
27
            $this->createMock(PathFinderInterface::class),
28
            [
29
                'php' => $this->createStub(ConfigReaderInterface::class),
30
            ]
31
        );
32
33
        self::assertSame([], $configInit->readAll());
34
    }
35
36
    public function test_non_supported_reader_type(): void
37
    {
38
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
39
        $gacelaJsonConfigCreator
40
            ->method('createGacelaJsonConfig')
41
            ->willReturn(GacelaJsonConfig::fromArray([
42
                'config' => [
43
                    'type' => 'non-supported-type',
44
                    'path' => 'path-value',
45
                    'path_local' => 'path_local-value',
46
                ],
47
            ]));
48
49
        $pathFinder = $this->createMock(PathFinderInterface::class);
50
        $pathFinder->method('matchingPattern')->willReturn(['path1']);
51
52
        $configInit = new ConfigInit(
53
            'application_root_dir',
54
            $gacelaJsonConfigCreator,
55
            $pathFinder,
56
            []
57
        );
58
59
        $this->expectException(ConfigReaderException::class);
60
        $configInit->readAll();
61
    }
62
63
    public function test_read_single_config(): void
64
    {
65
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
66
        $gacelaJsonConfigCreator
67
            ->method('createGacelaJsonConfig')
68
            ->willReturn(GacelaJsonConfig::fromArray([
69
                'config' => [
70
                    [
71
                        'type' => 'supported-type',
72
                    ],
73
                ],
74
            ]));
75
76
        $reader = $this->createStub(ConfigReaderInterface::class);
77
        $reader->method('canRead')->willReturn(true);
78
        $reader->method('read')->willReturn(['key' => 'value']);
79
80
        $configInit = new ConfigInit(
81
            'application_root_dir',
82
            $gacelaJsonConfigCreator,
83
            $this->createMock(PathFinderInterface::class),
84
            [
85
                'supported-type' => $reader,
86
            ]
87
        );
88
89
        self::assertSame(['key' => 'value'], $configInit->readAll());
90
    }
91
92
    public function test_read_multiple_config(): void
93
    {
94
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
95
        $gacelaJsonConfigCreator
96
            ->method('createGacelaJsonConfig')
97
            ->willReturn(GacelaJsonConfig::fromArray([
98
                'config' => [
99
                    [
100
                        'type' => 'supported-type1',
101
                    ],
102
                    [
103
                        'type' => 'supported-type2',
104
                    ],
105
                ],
106
            ]));
107
108
        $reader1 = $this->createStub(ConfigReaderInterface::class);
109
        $reader1->method('canRead')->willReturn(true);
110
        $reader1->method('read')->willReturn(['key1' => 'value1']);
111
112
        $reader2 = $this->createStub(ConfigReaderInterface::class);
113
        $reader2->method('canRead')->willReturn(true);
114
        $reader2->method('read')->willReturn(['key2' => 'value2']);
115
116
        $configInit = new ConfigInit(
117
            'application_root_dir',
118
            $gacelaJsonConfigCreator,
119
            $this->createMock(PathFinderInterface::class),
120
            [
121
                'supported-type1' => $reader1,
122
                'supported-type2' => $reader2,
123
            ]
124
        );
125
126
        self::assertSame([
127
            'key1' => 'value1',
128
            'key2' => 'value2',
129
        ], $configInit->readAll());
130
    }
131
}
132