Passed
Push — master ( 7773a0...e045e9 )
by Chema
02:46
created

ConfigTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ read() 0 3 1
A setUp() 0 4 1
A test_get_default_value_from_undefined_key() 0 3 1
A tearDown() 0 3 1
A test_get_undefined_key() 0 4 1
A hp$0 ➔ test_get_using_custom_reader() 0 19 1
test_get_using_custom_reader() 0 19 ?
A hp$0 ➔ canRead() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Framework\Container;
6
7
use Gacela\Framework\Config;
8
use Gacela\Framework\Config\ConfigReaderInterface;
9
use PHPUnit\Framework\TestCase;
10
11
final class ConfigTest extends TestCase
12
{
13
    private Config $config;
14
15
    public function setUp(): void
16
    {
17
        Config::resetInstance();
18
        $this->config = Config::getInstance();
19
    }
20
21
    public function tearDown(): void
22
    {
23
        Config::resetInstance();
24
    }
25
26
    public function test_get_undefined_key(): void
27
    {
28
        $this->expectExceptionMessageMatches('/Could not find config key "key"/');
29
        $this->config->get('key');
30
    }
31
32
    public function test_get_default_value_from_undefined_key(): void
33
    {
34
        self::assertSame('default', $this->config->get('key', 'default'));
35
    }
36
37
    public function test_get_using_custom_reader(): void
38
    {
39
        $this->config->setConfigReaders([
40
            Config\GacelaJsonConfigItem::DEFAULT_TYPE => new class() implements ConfigReaderInterface {
41
                public function read(string $absolutePath): array
42
                {
43
                    return ['key' => 'value'];
44
                }
45
46
                public function canRead(string $absolutePath): bool
47
                {
48
                    return true;
49
                }
50
            },
51
        ]);
52
53
        $this->config->init();
54
55
        self::assertSame('value', $this->config->get('key'));
56
    }
57
}
58