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