1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Tight\Tests; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Description of LocalizeModuleTest |
31
|
|
|
* |
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
33
|
|
|
*/ |
34
|
|
|
class LocalizeModuleTest extends \PHPUnit_Framework_TestCase |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
public static $resourceFolder = "resources"; |
38
|
|
|
private $localeEn = [ |
39
|
|
|
"app" => "Test App", |
40
|
|
|
"data" => [ |
41
|
|
|
"name" => "Name", |
42
|
|
|
"user" => "User", |
43
|
|
|
"password" => "Passowrd", |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
private $localeEs = [ |
47
|
|
|
"app" => "Test de aplicación", |
48
|
|
|
"data" => [ |
49
|
|
|
"name" => "Nombre", |
50
|
|
|
"user" => "Usuario", |
51
|
|
|
"password" => "Contraseña" |
52
|
|
|
] |
53
|
|
|
]; |
54
|
|
|
private $localeFr = [ |
55
|
|
|
"app" => "Test d'application", |
56
|
|
|
"data" => [ |
57
|
|
|
"name" => "Nom", |
58
|
|
|
"user" => "Utilisateur", |
59
|
|
|
"password" => "Mot de passe" |
60
|
|
|
] |
61
|
|
|
]; |
62
|
|
|
private $config = [ |
63
|
|
|
]; |
64
|
|
|
private $module; |
65
|
|
|
|
66
|
|
|
public function setUp() { |
67
|
|
|
$valuesFile_en = self::$resourceFolder . "/values.json"; |
68
|
|
|
$valuesFile_es = self::$resourceFolder . "/values_es.json"; |
69
|
|
|
$valuesFile_fr = self::$resourceFolder . "/values_fr.json"; |
70
|
|
|
if (!is_dir(self::$resourceFolder)) { |
71
|
|
|
mkdir(self::$resourceFolder); |
72
|
|
|
} |
73
|
|
|
if (!is_file($valuesFile_en)) { |
74
|
|
|
file_put_contents($valuesFile_en, json_encode($this->localeEn)); |
75
|
|
|
} |
76
|
|
|
if (!is_file($valuesFile_es)) { |
77
|
|
|
file_put_contents($valuesFile_es, json_encode($this->localeEs)); |
78
|
|
|
} |
79
|
|
|
if (!is_file($valuesFile_fr)) { |
80
|
|
|
file_put_contents($valuesFile_fr, json_encode($this->localeFr)); |
81
|
|
|
} |
82
|
|
|
$this->config = [ |
83
|
|
|
"resourceFolder" => "./" . self::$resourceFolder . "/" |
84
|
|
|
]; |
85
|
|
|
$this->module = new \Tight\Modules\Localize\Localize($this->config); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function tearDown() { |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
* @expectedException \Tight\Modules\ModuleException |
95
|
|
|
*/ |
96
|
|
|
public function testLocalizeModuleExceptionDirectoryNotFound() { |
97
|
|
|
$config = ["resourceFolder" => "directoryNotFound"]; |
98
|
|
|
$this->setExpectedException("\Tight\Modules\ModuleException"); |
99
|
|
|
new \Tight\Modules\Localize\Localize($config); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @test |
104
|
|
|
* @expectedException \Tight\Modules\ModuleException |
105
|
|
|
*/ |
106
|
|
|
public function testLocalizeModuleExceptionInvalidLocale() { |
107
|
|
|
$config = [ |
108
|
|
|
"resourceFolder" => self::$resourceFolder, |
109
|
|
|
"resourceFileName" => "strings" |
110
|
|
|
]; |
111
|
|
|
$this->setExpectedException("\Tight\Modules\ModuleException"); |
112
|
|
|
new \Tight\Modules\Localize\Localize($config); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
*/ |
118
|
|
|
public function testLocalizeModuleGetConfig() { |
119
|
|
|
$config = new \Tight\Modules\Localize\LocalizeConfig($this->config); |
120
|
|
|
$this->assertEquals($config, $this->module->getConfig()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @test |
125
|
|
|
* @expectedException \InvalidArgumentException |
126
|
|
|
*/ |
127
|
|
|
public function testLocalizeModuleInvalidArgumentException() { |
128
|
|
|
$this->setExpectedException("\InvalidArgumentException"); |
129
|
|
|
new \Tight\Modules\Localize\Localize("This is a string"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
*/ |
135
|
|
|
public function testLocalizeModuleGetValues() { |
136
|
|
|
$expected = $this->localeEn; |
137
|
|
|
$this->assertEquals($expected, $this->module->getValues()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @test |
142
|
|
|
*/ |
143
|
|
|
public function testLocalizeModuleGetLocales() { |
144
|
|
|
$expected = ["en", "es", "fr"]; |
145
|
|
|
$this->assertEquals($expected, $this->module->getLocales()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @test |
150
|
|
|
* @depends testLocalizeModuleGetValues |
151
|
|
|
* @covers \Tight\Modules\Localize\Localize::setLocale |
152
|
|
|
*/ |
153
|
|
|
public function testLocalizeModuleSetLocale() { |
154
|
|
|
$expected = $this->localeEs; |
155
|
|
|
$this->module->setLocale("es"); |
156
|
|
|
$this->assertEquals($expected, $this->module->getValues()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @test |
161
|
|
|
* @depends testLocalizeModuleSetLocale |
162
|
|
|
*/ |
163
|
|
|
public function testLocalizeModuleGet() { |
164
|
|
|
$this->assertEquals($this->localeEn['app'], $this->module->get("app")); |
165
|
|
|
$this->assertEquals($this->localeEn['data']['name'], $this->module->get("data")['name']); |
166
|
|
|
$this->module->setLocale("es"); |
167
|
|
|
$this->assertEquals($this->localeEs['app'], $this->module->get("app")); |
168
|
|
|
$this->assertEquals($this->localeEs['data']['name'], $this->module->get("data")['name']); |
169
|
|
|
$this->module->setLocale("fr"); |
170
|
|
|
$this->assertEquals($this->localeFr['app'], $this->module->get("app")); |
171
|
|
|
$this->assertEquals($this->localeFr['data']['name'], $this->module->get("data")['name']); |
172
|
|
|
$this->module->setLocale("en"); |
173
|
|
|
$this->assertEmpty($this->module->get("keyNotDefined")); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|