Test Failed
Push — master ( 3e37f9...24bbd2 )
by Petr
07:46
created

LoadersTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetNoFile() 0 8 1
A testMultiLoader() 0 7 1
A testGetVirtualFile() 0 5 1
A testGetRealFile() 0 8 1
A testClassLoader() 0 7 1
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
     * @throws PathsException
35
     */
36
    public function testGetRealFile(): void
37
    {
38
        $path = new Path();
39
        $path->setDocumentRoot(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data');
40
        $routed = new RoutedPath(new routeSource\Arrays([]));
41
        Config::init(new PhpLoader($path, $routed));
42
        Config::load('dummy');
43
        $this->assertEquals(1024, Config::get('dummy', 'upload_max_width'));
44
    }
45
46
    /**
47
     * @throws ConfException
48
     * @throws PathsException
49
     */
50
    public function testGetNoFile(): void
51
    {
52
        $path = new Path();
53
        $path->setDocumentRoot(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data');
54
        $routed = new RoutedPath(new routeSource\Arrays([]));
55
        Config::init(new PhpLoader($path, $routed));
56
        Config::load('unknown');
57
        $this->assertEquals('**really-not-existing', Config::get('**not-important', '**not-important', '**really-not-existing'));
58
    }
59
60
    /**
61
     * @throws ConfException
62
     */
63
    public function testMultiLoader(): void
64
    {
65
        $lib = new MultiLoader();
66
        $this->assertEmpty($lib->load('dummy', 'none'));
67
        $lib->addLoader(new XYLoader());
68
        $set = $lib->load('dummy', 'after loaded');
69
        $this->assertEquals(['abc' => 'mno', 'jkl' => 'vwx%s', ], $set);
70
    }
71
72
    /**
73
     * @throws ConfException
74
     */
75
    public function testClassLoader(): void
76
    {
77
        $lib = new ClassLoader();
78
        $this->assertEmpty($lib->load('not set', 'none'));
79
        $lib->addClass(new XYConf());
80
        $this->assertEmpty($lib->load('still not set', 'none'));
81
        $this->assertEquals(['abc01' => '98mno', 'jkl67' => '32vwx%s', ], $lib->load('testing', 'ignore now'));
82
    }
83
}
84
85
86
class XYLoader implements ILoader
87
{
88
    public function load(string $module, string $conf = ''): array
89
    {
90
        return [
91
            'abc' => 'mno',
92
            'jkl' => 'vwx%s',
93
        ];
94
    }
95
}
96
97
98
class XYConf implements IConf
99
{
100
    public function setPart(string $part): void
101
    {
102
        // nothing
103
    }
104
105
    public function getConfName(): string
106
    {
107
        return 'testing';
108
    }
109
110
    public function getSettings(): array
111
    {
112
        return [
113
            'abc01' => '98mno',
114
            'jkl67' => '32vwx%s',
115
        ];
116
    }
117
}
118