Passed
Push — master ( 1fb189...7018c2 )
by Petr
07:38
created

LoadersTest::testNoLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_confs\ConfException;
8
use kalanis\kw_confs\Config;
9
use kalanis\kw_confs\Interfaces\IConf;
10
use kalanis\kw_confs\Interfaces\ILoader;
11
use kalanis\kw_confs\Loaders\ClassLoader;
12
use kalanis\kw_confs\Loaders\MultiLoader;
13
use kalanis\kw_confs\Loaders\PhpLoader;
14
use kalanis\kw_paths\Path;
15
use kalanis\kw_paths\PathsException;
16
use kalanis\kw_routed_paths\RoutedPath;
17
use kalanis\kw_routed_paths\Sources as routeSource;
18
19
20
class LoadersTest extends CommonTestClass
21
{
22
    /**
23
     * @throws ConfException
24
     */
25
    public function testGetVirtualFile(): void
26
    {
27
        Config::init(new XYLoader());
28
        Config::load('dummy', 'hrk');
29
        $this->assertEquals('vwx%s', Config::get('dummy', 'jkl'));
30
    }
31
32
    /**
33
     * @throws ConfException
34
     */
35
    public function testNoLoader(): void
36
    {
37
        Config::init(null);
38
        $this->expectException(ConfException::class);
39
        Config::load('anything', 'will die');
40
    }
41
42
    /**
43
     * @throws ConfException
44
     * @throws PathsException
45
     */
46
    public function testGetRealFile(): void
47
    {
48
        $path = new Path();
49
        $path->setDocumentRoot(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data');
50
        $routed = new RoutedPath(new routeSource\Arrays([]));
51
        Config::init(new PhpLoader($path, $routed));
52
        Config::load('dummy');
53
        $this->assertEquals(1024, Config::get('dummy', 'upload_max_width'));
54
    }
55
56
    /**
57
     * @throws ConfException
58
     * @throws PathsException
59
     */
60
    public function testGetNoFile(): void
61
    {
62
        $path = new Path();
63
        $path->setDocumentRoot(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data');
64
        $routed = new RoutedPath(new routeSource\Arrays([]));
65
        Config::init(new PhpLoader($path, $routed));
66
        Config::load('unknown');
67
        $this->assertEquals('**really-not-existing', Config::get('**not-important', '**not-important', '**really-not-existing'));
68
    }
69
70
    /**
71
     * @throws ConfException
72
     */
73
    public function testMultiLoader(): void
74
    {
75
        $lib = new MultiLoader();
76
        $this->assertEmpty($lib->load('dummy', 'none'));
77
        $lib->addLoader(new XYLoader());
78
        $set = $lib->load('dummy', 'after loaded');
79
        $this->assertEquals(['abc' => 'mno', 'jkl' => 'vwx%s', ], $set);
80
    }
81
82
    /**
83
     * @throws ConfException
84
     */
85
    public function testClassLoader(): void
86
    {
87
        $lib = new ClassLoader();
88
        $this->assertEmpty($lib->load('not set', 'none'));
89
        $lib->addClass(new XYConf());
90
        $this->assertEmpty($lib->load('still not set', 'none'));
91
        $this->assertEquals(['abc01' => '98mno', 'jkl67' => '32vwx%s', ], $lib->load('testing', 'ignore now'));
92
    }
93
}
94
95
96
class XYLoader implements ILoader
97
{
98
    public function load(string $module, string $conf = ''): array
99
    {
100
        return [
101
            'abc' => 'mno',
102
            'jkl' => 'vwx%s',
103
        ];
104
    }
105
}
106
107
108
class XYConf implements IConf
109
{
110
    public function setPart(string $part): void
111
    {
112
        // nothing
113
    }
114
115
    public function getConfName(): string
116
    {
117
        return 'testing';
118
    }
119
120
    public function getSettings(): array
121
    {
122
        return [
123
            'abc01' => '98mno',
124
            'jkl67' => '32vwx%s',
125
        ];
126
    }
127
}
128