Passed
Push — master ( e9344a...7222b6 )
by Petr
07:47
created

LangLoaderTest::testGetNoTranslate()   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_langs\Interfaces\ILang;
8
use kalanis\kw_langs\Interfaces\ILoader;
9
use kalanis\kw_langs\Lang;
10
use kalanis\kw_langs\LangException;
11
use kalanis\kw_langs\Loaders\ClassLoader;
12
use kalanis\kw_langs\Loaders\MultiLoader;
13
use kalanis\kw_langs\Loaders\PhpLoader;
14
use kalanis\kw_langs\Support;
15
use kalanis\kw_paths\Path;
16
use kalanis\kw_paths\PathsException;
17
use kalanis\kw_routed_paths\RoutedPath;
18
use kalanis\kw_routed_paths\Sources as routeSource;
19
20
21
class LangLoaderTest extends CommonTestClass
22
{
23
    /**
24
     * @throws LangException
25
     */
26
    public function testGetVirtualFile(): void
27
    {
28
        Lang::init(new XYLoader(), 'hrk');
29
        Lang::load('dummy');
30
        $this->assertEquals('abcmnodefpqrghistujklvwx%syz0123%s456', Lang::get('wtf'));
31
    }
32
33
    /**
34
     * @throws LangException
35
     * @throws PathsException
36
     */
37
    public function testGetRealFile(): void
38
    {
39
        $path = new Path();
40
        $path->setDocumentRoot(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data');
41
        $routed = new RoutedPath(new routeSource\Arrays(['lang' => 'fra']));
42
        Lang::init(new PhpLoader($path, $routed), Support::fillFromPaths($routed, 'hrk', true));
43
        Lang::load('dummy');
44
        $this->assertEquals('Alors quoi?', Lang::get('dashboard.page'));
45
    }
46
47
    /**
48
     * @throws LangException
49
     * @throws PathsException
50
     */
51
    public function testGetNoFile(): void
52
    {
53
        $path = new Path();
54
        $path->setDocumentRoot(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data');
55
        $routed = new RoutedPath(new routeSource\Arrays(['lang' => 'fra']));
56
        Lang::init(new PhpLoader($path, $routed), Support::fillFromPaths($routed, 'hrk', true));
57
        Lang::load('unknown');
58
        $this->assertEquals('some.page', Lang::get('some.page'));
59
    }
60
61
    /**
62
     * @throws LangException
63
     */
64
    public function testGetNoTranslate(): void
65
    {
66
        Lang::init(new XYLoader(), 'hrk');
67
        Lang::load('unknown');
68
        $this->assertEquals('**really-not-existing', Lang::get('**really-not-existing'));
69
    }
70
71
    /**
72
     * @throws PathsException
73
     */
74
    public function testSupport(): void
75
    {
76
        $routed1 = new RoutedPath(new routeSource\Arrays(['lang' => 'hrk']));
77
        $this->assertEquals('hrk', Support::fillFromPaths($routed1, 'cas', true));
78
79
        $routed2 = new RoutedPath(new routeSource\Arrays(['lang' => '', 'path' => 'cas/off/nope']));
80
        $this->assertEquals('cas', Support::fillFromPaths($routed2, 'ign', true));
81
        $this->assertEquals('ign', Support::fillFromPaths($routed2, 'ign', false));
82
    }
83
84
    public function testSupportSetter(): void
85
    {
86
        $store = new \ArrayObject();
87
        $this->assertEquals('cas', Support::fillFromArray($store, 'cas'));
88
        Support::setToArray($store, '');
89
        $this->assertEquals('cas', Support::fillFromArray($store, 'cas'));
90
        Support::setToArray($store, 'moa');
91
        $this->assertEquals('moa', Support::fillFromArray($store, 'ign'));
92
    }
93
94
    /**
95
     * @throws LangException
96
     */
97
    public function testMultiLoader(): void
98
    {
99
        // init multi
100
        $lib = new MultiLoader();
101
        Lang::init($lib, 'hrk');
102
103
        // unknown now
104
        $lib->load('unknown', 'nop');
105
106
        // add some
107
        $lib->addLoader(new XYLoader());
108
        $this->assertEquals('**none-known', Lang::get('**none-known')); // not set
109
        Lang::load(XYLoader::class);
110
        $this->assertEquals('abc%smnodefpqrghistujklvwxyz%s0123456', Lang::get('srl')); // set
111
    }
112
113
    /**
114
     * @throws LangException
115
     */
116
    public function testClassLoader(): void
117
    {
118
        // init multi
119
        $lib = new ClassLoader();
120
        Lang::init($lib, 'hrk');
121
122
        // unknown now
123
        $lib->load('unknown', 'nop');
124
125
        // add some
126
        $lib->addClass(new XYLang());
127
        $this->assertEquals('**none-known', Lang::get('**none-known')); // not set
128
        Lang::load(XYLang::class);
129
        $this->assertEquals('43vwx%s', Lang::get('jkl78')); // set
130
    }
131
}
132
133
134
class XYLoader implements ILoader
135
{
136
    public function load(string $module, string $path = ''): array
137
    {
138
        return [
139
            'hrk' => [
140
                'wtf' => 'abcmnodefpqrghistujklvwx%syz0123%s456',
141
                'srl' => 'abc%smnodefpqrghistujklvwxyz%s0123456',
142
            ],
143
        ];
144
    }
145
}
146
147
148
class XYLang implements ILang
149
{
150
    public function setLang(string $lang): ILang
151
    {
152
        // nothing need
153
        return $this;
154
    }
155
156
    public function getTranslations(): array
157
    {
158
        return [
159
            'abc12' => '09mno',
160
            'jkl78' => '43vwx%s',
161
        ];
162
    }
163
}
164