LangTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 44
c 5
b 0
f 1
dl 0
loc 80
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testBasic() 0 19 1
A testClass() 0 13 1
A testUnderscores() 0 15 1
A testLangsInPath() 0 11 1
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\PhpLoader;
12
use kalanis\kw_langs\Support;
13
use kalanis\kw_paths\Path;
14
use kalanis\kw_paths\PathsException;
15
use kalanis\kw_routed_paths\RoutedPath;
16
use kalanis\kw_routed_paths\Sources as routeSource;
17
18
19
class LangTest extends CommonTestClass
20
{
21
    /**
22
     * @throws LangException
23
     * @throws PathsException
24
     */
25
    public function testBasic(): void
26
    {
27
        $path = new Path();
28
        $path->setDocumentRoot('/tmp/none');
29
        $routed = new RoutedPath(new routeSource\Arrays([]));
30
        Lang::init(new PhpLoader($path, $routed), 'foo');
31
32
        Lang::init(new XLoader(), 'bar');
33
        Lang::load('baz');
34
        $this->assertEquals('bar', Lang::getLang());
35
36
        $this->assertEquals('pqr', Lang::get('def'));
37
        $this->assertEquals('ewq', Lang::get('ewq', 'lkj'));
38
        $this->assertEquals('vwx123', Lang::get('jkl', '123'));
39
        $this->assertEquals('asdf123', Lang::get('asdf%s', '123'));
40
        $this->assertEquals('asdf%s', Lang::get('asdf%s'));
41
        $this->assertEquals('123%s456', Lang::get('yz0'));
42
43
        $this->assertInstanceOf(ILoader::class, Lang::getLoader());
44
    }
45
46
    /**
47
     * @throws PathsException
48
     */
49
    public function testClass(): void
50
    {
51
        $path = new Path();
52
        $path->setDocumentRoot('/tmp/none');
53
        $routed = new RoutedPath(new routeSource\Arrays([]));
54
        Lang::init(new PhpLoader($path, $routed), Support::fillFromPaths($routed, 'bar', false));
55
        Lang::loadClass(new XLang());
56
        $this->assertEquals('bar', Lang::getLang());
57
58
        $this->assertEquals('65stu', Lang::get('ghi56'));
59
        $this->assertEquals('ewq', Lang::get('ewq', 'lkj'));
60
        $this->assertEquals('43vwx123', Lang::get('jkl78', '123'));
61
        $this->assertEquals('asdf123', Lang::get('asdf%s', '123'));
62
    }
63
64
    /**
65
     * @throws PathsException
66
     */
67
    public function testLangsInPath(): void
68
    {
69
        $path = new Path();
70
        $path->setDocumentRoot('/tmp/none');
71
        $routed1 = new RoutedPath(new routeSource\Arrays(['path' => 'abcdef/ghijkl/mnopqrs.tuv'])); // nope - first segment is too long
72
        Lang::init(new PhpLoader($path, $routed1), Support::fillFromPaths($routed1, 'bar', true));
73
        $this->assertEquals('bar', Lang::getLang());
74
75
        $routed2 = new RoutedPath(new routeSource\Arrays(['path' => 'abc/def/ghijkl/mnopqrs.tuv'])); // yep - only 3 letters in first segment
76
        Lang::init(new PhpLoader($path, $routed2), Support::fillFromPaths($routed2, 'bar', true));
77
        $this->assertEquals('abc', Lang::getLang());
78
    }
79
80
    /**
81
     * @throws LangException
82
     * @throws PathsException
83
     */
84
    public function testUnderscores(): void
85
    {
86
        require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'php-src' . DIRECTORY_SEPARATOR . 'double.php';
87
88
        $path = new Path();
89
        $path->setDocumentRoot('/tmp/none');
90
        $routed = new RoutedPath(new routeSource\Arrays(['lang' => 'cro']));
91
        Lang::init(new PhpLoader($path, $routed), Support::fillFromPaths($routed, 'bar', false));
92
        Lang::load('baz');
93
94
        $this->assertEquals('cro', Lang::getLang());
95
        $this->assertEquals('pqr', __('def'));
96
        $this->assertEquals('ewq', __('ewq', 'lkj'));
97
        $this->assertEquals('vwx123', __('jkl', '123'));
98
        $this->assertEquals('asdf123', __('asdf%s', '123'));
99
    }
100
}
101
102
103
class XLoader implements ILoader
104
{
105
    public function load(string $module, string $lang): array
106
    {
107
        return [
108
            'abc' => 'mno',
109
            'def' => 'pqr',
110
            'ghi' => 'stu',
111
            'jkl' => 'vwx%s',
112
            'yz0' => '123%s456',
113
        ];
114
    }
115
}
116
117
118
class XLang implements ILang
119
{
120
    public function setLang(string $lang): ILang
121
    {
122
        // nothing need
123
        return $this;
124
    }
125
126
    public function getTranslations(): array
127
    {
128
        return [
129
            'abc12' => '09mno',
130
            'def34' => '87pqr',
131
            'ghi56' => '65stu',
132
            'jkl78' => '43vwx%s',
133
        ];
134
    }
135
}
136