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
|
|
|
namespace Tight\Tests; |
27
|
|
|
/** |
28
|
|
|
* Description of LocalizeModuleTest |
29
|
|
|
* |
30
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
31
|
|
|
*/ |
32
|
|
|
class LocalizeModuleTest extends \PHPUnit_Framework_TestCase |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
public static $resourceFolder = "resources"; |
36
|
|
|
private $locale_en = [ |
37
|
|
|
"app" => "Test App", |
38
|
|
|
"data" => [ |
39
|
|
|
"name" => "Name", |
40
|
|
|
"user" => "User", |
41
|
|
|
"password" => "Passowrd", |
42
|
|
|
] |
43
|
|
|
]; |
44
|
|
|
private $locale_es = [ |
45
|
|
|
"app" => "Test de aplicación", |
46
|
|
|
"data" => [ |
47
|
|
|
"name" => "Nombre", |
48
|
|
|
"user" => "Usuario", |
49
|
|
|
"password" => "Contraseña" |
50
|
|
|
] |
51
|
|
|
]; |
52
|
|
|
private $locale_fr = [ |
53
|
|
|
"app" => "Test d'application", |
54
|
|
|
"data" => [ |
55
|
|
|
"name" => "Nom", |
56
|
|
|
"user" => "Utilisateur", |
57
|
|
|
"password" => "Mot de passe" |
58
|
|
|
] |
59
|
|
|
]; |
60
|
|
|
private $config = [ |
61
|
|
|
]; |
62
|
|
|
private $module; |
63
|
|
|
|
64
|
|
|
public function setUp() { |
65
|
|
|
$valuesFile_en = self::$resourceFolder . "/values.json"; |
66
|
|
|
$valuesFile_es = self::$resourceFolder . "/values_es.json"; |
67
|
|
|
$valuesFile_fr = self::$resourceFolder . "/values_fr.json"; |
68
|
|
|
if (!is_dir(self::$resourceFolder)) { |
69
|
|
|
mkdir(self::$resourceFolder); |
70
|
|
|
} |
71
|
|
|
if (!is_file($valuesFile_en)) { |
72
|
|
|
file_put_contents($valuesFile_en, json_encode($this->locale_en)); |
73
|
|
|
} |
74
|
|
|
if (!is_file($valuesFile_es)) { |
75
|
|
|
file_put_contents($valuesFile_es, json_encode($this->locale_es)); |
76
|
|
|
} |
77
|
|
|
if (!is_file($valuesFile_fr)) { |
78
|
|
|
file_put_contents($valuesFile_fr, json_encode($this->locale_fr)); |
79
|
|
|
} |
80
|
|
|
$this->config = [ |
81
|
|
|
"resourceFolder" => "./" . self::$resourceFolder . "/" |
82
|
|
|
]; |
83
|
|
|
$this->module = new \Tight\Modules\Localize\Localize($this->config); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function tearDown() { |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function testLocalizeModuleExceptionDirectoryNotFound() { |
94
|
|
|
$config = ["resourceFolder" => "directoryNotFound"]; |
95
|
|
|
$this->setExpectedException("\Tight\Modules\ModuleException"); |
96
|
|
|
new \Tight\Modules\Localize\Localize($config); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function testLocalizeModuleExceptionInvalidLocale() { |
103
|
|
|
$config = [ |
104
|
|
|
"resourceFolder" => self::$resourceFolder, |
105
|
|
|
"resourceFileName" => "strings" |
106
|
|
|
]; |
107
|
|
|
$this->setExpectedException("\Tight\Modules\ModuleException"); |
108
|
|
|
new \Tight\Modules\Localize\Localize($config); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @test |
113
|
|
|
*/ |
114
|
|
|
public function testLocalizeModuleGetConfig() { |
115
|
|
|
$config = new \Tight\Modules\Localize\LocalizeConfig($this->config); |
116
|
|
|
$this->assertEquals($config, $this->module->getConfig()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @test |
121
|
|
|
*/ |
122
|
|
|
public function testLocalizeModuleGetAppConfig() { |
123
|
|
|
// The locale section in Tight\TightConfig is empty due to the method |
124
|
|
|
// of instantiating the module class (calling the class instead of |
125
|
|
|
// Tight\Tight) |
126
|
|
|
$config = new \Tight\TightConfig(["locale" => []]); |
127
|
|
|
$this->assertEquals($config, $this->module->getAppConfig()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
*/ |
133
|
|
|
public function testLocalizeModuleInvalidArgumentException() { |
134
|
|
|
$this->setExpectedException("\InvalidArgumentException"); |
135
|
|
|
new \Tight\Modules\Localize\Localize("This is a string"); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @test |
140
|
|
|
*/ |
141
|
|
|
public function testLocalizeModuleGetValues() { |
142
|
|
|
$expected = $this->locale_en; |
143
|
|
|
$this->assertEquals($expected, $this->module->getValues()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @test |
148
|
|
|
*/ |
149
|
|
|
public function testLocalizeModuleGetLocales() { |
150
|
|
|
$expected = ["en", "es", "fr"]; |
151
|
|
|
$this->assertEquals($expected, $this->module->getLocales()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
* @depends testLocalizeModuleGetValues |
157
|
|
|
* @covers \Tight\Modules\Localize\Localize::setLocale |
158
|
|
|
*/ |
159
|
|
|
public function testLocalizeModuleSetLocale() { |
160
|
|
|
$expected = $this->locale_es; |
161
|
|
|
$this->module->setLocale("es"); |
162
|
|
|
$this->assertEquals($expected, $this->module->getValues()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @test |
167
|
|
|
* @depends testLocalizeModuleSetLocale |
168
|
|
|
*/ |
169
|
|
|
public function testLocalizeModuleGet() { |
170
|
|
|
$this->assertEquals($this->locale_en['app'], $this->module->get("app")); |
171
|
|
|
$this->assertEquals($this->locale_en['data']['name'], $this->module->get("data")['name']); |
172
|
|
|
$this->module->setLocale("es"); |
173
|
|
|
$this->assertEquals($this->locale_es['app'], $this->module->get("app")); |
174
|
|
|
$this->assertEquals($this->locale_es['data']['name'], $this->module->get("data")['name']); |
175
|
|
|
$this->module->setLocale("fr"); |
176
|
|
|
$this->assertEquals($this->locale_fr['app'], $this->module->get("app")); |
177
|
|
|
$this->assertEquals($this->locale_fr['data']['name'], $this->module->get("data")['name']); |
178
|
|
|
$this->module->setLocale("en"); |
179
|
|
|
$this->assertEmpty($this->module->get("keyNotDefined")); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: