Passed
Push — master ( 889c24...e8b07e )
by Jesús
01:15 queued 11s
created

ConfigInitTest::test_read_single_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 29
rs 9.7
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\ConfigReaderInterface;
9
use Gacela\Framework\Config\GacelaJsonConfig;
10
use Gacela\Framework\Config\GacelaJsonConfigFactoryInterface;
11
use Gacela\Framework\Config\PathFinderInterface;
12
use PHPUnit\Framework\TestCase;
13
14
final class ConfigInitTest extends TestCase
15
{
16
    public function test_no_config(): void
17
    {
18
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
19
        $gacelaJsonConfigCreator
20
            ->method('createGacelaJsonConfig')
21
            ->willReturn(GacelaJsonConfig::withDefaults());
22
23
        $readers = [
24
            'php' => $this->createStub(ConfigReaderInterface::class),
25
        ];
26
27
        $configInit = new ConfigInit(
28
            'application_root_dir',
29
            $gacelaJsonConfigCreator,
30
            $this->createMock(PathFinderInterface::class),
31
            $readers
32
        );
33
34
        self::assertSame([], $configInit->readAll());
35
    }
36
37
    public function test_one_reader_linked_to_unsupported_type_is_ignored(): void
38
    {
39
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
40
        $gacelaJsonConfigCreator
41
            ->method('createGacelaJsonConfig')
42
            ->willReturn(GacelaJsonConfig::withDefaults());
43
44
        $pathFinder = $this->createMock(PathFinderInterface::class);
45
        $pathFinder->method('matchingPattern')->willReturn(['path1']);
46
47
        $readers = [
48
            'unsupported_type' => $this->createStub(ConfigReaderInterface::class),
49
        ];
50
51
        $configInit = new ConfigInit(
52
            'application_root_dir',
53
            $gacelaJsonConfigCreator,
54
            $pathFinder,
55
            $readers
56
        );
57
58
        self::assertSame([], $configInit->readAll());
59
    }
60
61
    public function test_no_readers_returns_empty_array(): void
62
    {
63
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
64
        $gacelaJsonConfigCreator
65
            ->method('createGacelaJsonConfig')
66
            ->willReturn(GacelaJsonConfig::withDefaults());
67
68
        $pathFinder = $this->createMock(PathFinderInterface::class);
69
        $pathFinder->method('matchingPattern')->willReturn(['path1']);
70
71
        $readers = [];
72
73
        $configInit = new ConfigInit(
74
            'application_root_dir',
75
            $gacelaJsonConfigCreator,
76
            $pathFinder,
77
            $readers
78
        );
79
80
        self::assertSame([], $configInit->readAll());
81
    }
82
83
    public function test_read_single_config(): void
84
    {
85
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
86
        $gacelaJsonConfigCreator
87
            ->method('createGacelaJsonConfig')
88
            ->willReturn(GacelaJsonConfig::fromArray([
89
                'config' => [
90
                    [
91
                        'type' => 'supported-type',
92
                    ],
93
                ],
94
            ]));
95
96
        $reader = $this->createStub(ConfigReaderInterface::class);
97
        $reader->method('canRead')->willReturn(true);
98
        $reader->method('read')->willReturn(['key' => 'value']);
99
100
        $readers = [
101
            'supported-type' => $reader,
102
        ];
103
104
        $configInit = new ConfigInit(
105
            'application_root_dir',
106
            $gacelaJsonConfigCreator,
107
            $this->createMock(PathFinderInterface::class),
108
            $readers
109
        );
110
111
        self::assertSame(['key' => 'value'], $configInit->readAll());
112
    }
113
114
    public function test_read_multiple_config(): void
115
    {
116
        $gacelaJsonConfigCreator = $this->createStub(GacelaJsonConfigFactoryInterface::class);
117
        $gacelaJsonConfigCreator
118
            ->method('createGacelaJsonConfig')
119
            ->willReturn(GacelaJsonConfig::fromArray([
120
                'config' => [
121
                    [
122
                        'type' => 'supported-type1',
123
                    ],
124
                    [
125
                        'type' => 'supported-type2',
126
                    ],
127
                ],
128
            ]));
129
130
        $reader1 = $this->createStub(ConfigReaderInterface::class);
131
        $reader1->method('canRead')->willReturn(true);
132
        $reader1->method('read')->willReturn(['key1' => 'value1']);
133
134
        $reader2 = $this->createStub(ConfigReaderInterface::class);
135
        $reader2->method('canRead')->willReturn(true);
136
        $reader2->method('read')->willReturn(['key2' => 'value2']);
137
138
        $readers = [
139
            'supported-type1' => $reader1,
140
            'supported-type2' => $reader2,
141
        ];
142
143
        $configInit = new ConfigInit(
144
            'application_root_dir',
145
            $gacelaJsonConfigCreator,
146
            $this->createMock(PathFinderInterface::class),
147
            $readers
148
        );
149
150
        self::assertSame([
151
            'key1' => 'value1',
152
            'key2' => 'value2',
153
        ], $configInit->readAll());
154
    }
155
}
156