1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Translation\Loaders; |
5
|
|
|
|
6
|
|
|
use Tester\Assert; |
7
|
|
|
use Nexendrie\Translation\InvalidFolderException; |
8
|
|
|
use Nexendrie\Translation\FolderNotSetException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* General test suit for file loaders |
12
|
|
|
* |
13
|
|
|
* @author Jakub Konečný |
14
|
|
|
*/ |
15
|
|
|
abstract class FileLoaderTestAbstract extends \Tester\TestCase { |
16
|
|
|
protected FileLoader $loader; |
17
|
|
|
|
18
|
|
|
public function testGetLang(): void { |
19
|
|
|
$lang = $this->loader->lang; |
20
|
|
|
Assert::type("string", $lang); |
21
|
|
|
Assert::same("en", $lang); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testSetLang(): void { |
25
|
|
|
$this->loader->lang = "cs"; |
26
|
|
|
$lang = $this->loader->lang; |
27
|
|
|
Assert::type("string", $lang); |
28
|
|
|
Assert::same("cs", $lang); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetFolders(): void { |
32
|
|
|
$folders = $this->loader->folders; |
33
|
|
|
Assert::type("array", $folders); |
34
|
|
|
Assert::count(2, $folders); |
35
|
|
|
Assert::same(__DIR__ . "/../../../lang", $folders[0]); |
36
|
|
|
Assert::same(__DIR__ . "/../../../lang2", $folders[1]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testSetFolders(): void { |
40
|
|
|
Assert::exception(function(): void { |
41
|
|
|
$this->loader->folders = [""]; |
42
|
|
|
}, InvalidFolderException::class, "Folder does not exist."); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetResources(): void { |
46
|
|
|
// texts were not loaded yet so there are no resources |
47
|
|
|
$resources = $this->loader->resources; |
48
|
|
|
Assert::type("array", $resources); |
49
|
|
|
Assert::count(0, $resources); |
50
|
|
|
// english texts are loaded, there is 1 resource for each domain |
51
|
|
|
$this->loader->getTexts(); |
|
|
|
|
52
|
|
|
$resources = $this->loader->resources; |
53
|
|
|
Assert::type("array", $resources); |
54
|
|
|
Assert::count(3, $resources); |
55
|
|
|
Assert::count(1, $resources["messages"]); |
56
|
|
|
Assert::count(1, $resources["book"]); |
57
|
|
|
Assert::count(1, $resources["abc"]); |
58
|
|
|
// czech and english texts are loaded, there are 2 resources for each domain |
59
|
|
|
$this->loader->lang = "cs"; |
60
|
|
|
$this->loader->getTexts(); |
|
|
|
|
61
|
|
|
$resources = $this->loader->resources; |
62
|
|
|
Assert::type("array", $resources); |
63
|
|
|
Assert::count(3, $resources); |
64
|
|
|
Assert::count(2, $resources["messages"]); |
65
|
|
|
Assert::count(2, $resources["book"]); |
66
|
|
|
Assert::count(2, $resources["abc"]); |
67
|
|
|
// the language does not exist, 1 (default) resource for each domain |
68
|
|
|
if($this->loader instanceof MessagesCatalogue) { |
69
|
|
|
return; // the following tests for some reason fail with MessagesCatalogue |
70
|
|
|
} |
71
|
|
|
$this->loader->lang = "xyz"; |
72
|
|
|
$this->loader->getTexts(); |
|
|
|
|
73
|
|
|
$resources = $this->loader->resources; |
74
|
|
|
Assert::type("array", $resources); |
75
|
|
|
Assert::count(3, $resources); |
76
|
|
|
Assert::count(1, $resources["messages"]); |
77
|
|
|
Assert::count(1, $resources["book"]); |
78
|
|
|
Assert::count(1, $resources["abc"]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGetTexts(): void { |
82
|
|
|
$texts = $this->loader->getTexts(); |
|
|
|
|
83
|
|
|
Assert::type("array", $texts); |
84
|
|
|
Assert::count(3, $texts); |
85
|
|
|
Assert::type("array", $texts["messages"]); |
86
|
|
|
Assert::count(3, $texts["messages"]); |
87
|
|
|
Assert::type("array", $texts["book"]); |
88
|
|
|
Assert::count(5, $texts["book"]); |
89
|
|
|
$this->loader->lang = "cs"; |
90
|
|
|
$texts = $this->loader->getTexts(); |
|
|
|
|
91
|
|
|
Assert::type("array", $texts); |
92
|
|
|
Assert::count(3, $texts); |
93
|
|
|
Assert::type("array", $texts["messages"]); |
94
|
|
|
Assert::count(3, $texts["messages"]); |
95
|
|
|
Assert::type("array", $texts["book"]); |
96
|
|
|
Assert::count(5, $texts["book"]); |
97
|
|
|
if($this->loader instanceof MessagesCatalogue) { |
98
|
|
|
return; // the following tests for some reason fail with MessagesCatalogue |
99
|
|
|
} |
100
|
|
|
$this->loader->lang = "xyz"; |
101
|
|
|
$texts = $this->loader->getTexts(); |
|
|
|
|
102
|
|
|
Assert::type("array", $texts); |
103
|
|
|
Assert::count(3, $texts); |
104
|
|
|
Assert::type("array", $texts["messages"]); |
105
|
|
|
Assert::count(3, $texts["messages"]); |
106
|
|
|
Assert::type("array", $texts["book"]); |
107
|
|
|
Assert::count(5, $texts["book"]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testNoFolder(): void { |
111
|
|
|
Assert::exception(function(): void { |
112
|
|
|
$class = get_class($this->loader); |
113
|
|
|
$this->loader = new $class(); |
114
|
|
|
$this->loader->getTexts(); |
|
|
|
|
115
|
|
|
}, FolderNotSetException::class, "Folder for translations was not set."); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetAvailableLanguages(): void { |
119
|
|
|
$result = $this->loader->getAvailableLanguages(); |
120
|
|
|
Assert::type("array", $result); |
121
|
|
|
Assert::count(2, $result); |
122
|
|
|
Assert::contains("en", $result); |
123
|
|
|
Assert::contains("cs", $result); |
124
|
|
|
Assert::exception(function(): void { |
125
|
|
|
$class = get_class($this->loader); |
126
|
|
|
$this->loader = new $class(); |
127
|
|
|
$this->loader->getAvailableLanguages(); |
128
|
|
|
}, FolderNotSetException::class, "Folder for translations was not set."); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetResolverName(): void { |
132
|
|
|
$name = $this->loader->getResolverName(); |
133
|
|
|
Assert::type("string", $name); |
134
|
|
|
Assert::same("ManualLocaleResolver", $name); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
?> |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.