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