Completed
Push — master ( 2ca227...d5b3be )
by Alejandro
02:53
created

LocalizeModuleTest::testGetConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
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" => "Password",
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 $valuesXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
65
<resources>
66
    <values lang='en'>
67
        <string name='app'>Test App</string>
68
        <array name='data'>
69
            <string name='name'>Name</string>
70
            <string name='user'>User</string>
71
            <string name='password'>Password</string>
72
        </array>
73
    </values>
74
    <values lang='es'>
75
        <string name='app'>Test de aplicación</string>
76
        <array name='data'>
77
            <string name='name'>Nombre</string>
78
            <string name='user'>Usuario</string>
79
            <string name='password'>Contraseña</string>
80
        </array>
81
    </values>
82
    <values lang='fr'>
83
        <string name='app'>Test d'application</string>
84
        <array name='data'>
85
            <string name='name'>Nom</string>
86
            <string name='user'>Utilisateur</string>
87
            <string name='password'>Mot de passe</string>
88
        </array>
89
    </values>
90
</resources>";
91
    private $module;
92
93
    public function setUp() {
94
        $valuesFile_en = self::$resourceFolder . "/values.json";
95
        $valuesFile_es = self::$resourceFolder . "/values_es.json";
96
        $valuesFile_fr = self::$resourceFolder . "/values_fr.json";
97
        $valuesXml = self::$resourceFolder . "/values.xml";
98
        if (!is_dir(self::$resourceFolder)) {
99
            mkdir(self::$resourceFolder);
100
        }
101
        if (!is_file($valuesFile_en)) {
102
            file_put_contents($valuesFile_en, json_encode($this->localeEn));
103
        }
104
        if (!is_file($valuesFile_es)) {
105
            file_put_contents($valuesFile_es, json_encode($this->localeEs));
106
        }
107
        if (!is_file($valuesFile_fr)) {
108
            file_put_contents($valuesFile_fr, json_encode($this->localeFr));
109
        }
110
        if (!is_file($valuesXml)) {
111
            file_put_contents($valuesXml, $this->valuesXml);
112
        }
113
        $this->config = [
114
            "resourceFolder" => "./" . self::$resourceFolder . "/"
115
        ];
116
        $this->module = new \Tight\Modules\Localize\Localize($this->config);
117
    }
118
119
    public function tearDown() {
120
        $valuesFile_en = self::$resourceFolder . "/values.json";
121
        $valuesFile_es = self::$resourceFolder . "/values_es.json";
122
        $valuesFile_fr = self::$resourceFolder . "/values_fr.json";
123
        $valuesXml = self::$resourceFolder . "/values.xml";
124
        $files = [$valuesFile_en, $valuesFile_es, $valuesFile_fr, $valuesXml];
125
        $size = count($files);
126
        for ($index = 0; $index < $size; $index++) {
127
            if (is_file($files[$index])) {
128
                unlink($files[$index]);
129
            }
130
        }
131
    }
132
133
    /**
134
     * @test
135
     * @expectedException \Tight\Exception\ModuleException
136
     */
137
    public function testModuleExceptionDirectoryNotFound() {
138
        $config = ["resourceFolder" => "directoryNotFound"];
139
        $this->setExpectedException("\Tight\Exception\ModuleException");
140
        new \Tight\Modules\Localize\Localize($config);
141
    }
142
143
    /**
144
     * @test
145
     * @expectedException \Tight\Exception\ModuleException
146
     */
147
    public function testModuleExceptionInvalidLocale() {
148
        $config = [
149
            "resourceFolder" => self::$resourceFolder,
150
            "resourceFileName" => "strings"
151
        ];
152
        $this->setExpectedException("\Tight\Exception\ModuleException");
153
        new \Tight\Modules\Localize\Localize($config);
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function testGetConfig() {
160
        $config = new \Tight\Modules\Localize\LocalizeConfig($this->config);
161
        $this->assertEquals($config, $this->module->getConfig());
162
    }
163
164
    /**
165
     * @test
166
     * @expectedException \InvalidArgumentException
167
     */
168
    public function testInvalidArgumentException() {
169
        $this->setExpectedException("\InvalidArgumentException");
170
        new \Tight\Modules\Localize\Localize("This is a string");
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function testJsonGetValues() {
177
        $expected = $this->localeEn;
178
        $this->assertEquals($expected, $this->module->getValues());
179
    }
180
181
    /**
182
     * @test
183
     */
184
    public function testJsonGetLocales() {
185
        $expected = ["en", "es", "fr"];
186
        $this->assertEquals($expected, $this->module->getLocales());
187
    }
188
189
    /**
190
     * @test
191
     * @depends testJsonGetValues
192
     * @covers \Tight\Modules\Localize\Localize::setLocale
193
     */
194
    public function testJsonSetLocale() {
195
        $expected = $this->localeEs;
196
        $this->module->setLocale("es");
197
        $this->assertEquals($expected, $this->module->getValues());
198
    }
199
200
    /**
201
     * @test
202
     * @depends testJsonSetLocale
203
     */
204
    public function testJsonGet() {
205
        $this->assertEquals($this->localeEn['app'], $this->module->get("app"));
206
        $this->assertEquals($this->localeEn['data']['name'], $this->module->get("data")['name']);
207
        $this->module->setLocale("es");
208
        $this->assertEquals($this->localeEs['app'], $this->module->get("app"));
209
        $this->assertEquals($this->localeEs['data']['name'], $this->module->get("data")['name']);
210
        $this->module->setLocale("fr");
211
        $this->assertEquals($this->localeFr['app'], $this->module->get("app"));
212
        $this->assertEquals($this->localeFr['data']['name'], $this->module->get("data")['name']);
213
        $this->module->setLocale("en");
214
        $this->assertEmpty($this->module->get("keyNotDefined"));
215
    }
216
217
    /**
218
     * @test
219
     */
220
    public function testSetResourceFileType() {
221
        $this->module->setResourceFileType(\Tight\Modules\Localize\LocalizeConfig::FILETYPE_XML);
222
        $this->assertEquals(\Tight\Modules\Localize\LocalizeConfig::FILETYPE_XML, $this->module->getResourceFileType());
0 ignored issues
show
Bug introduced by
The method getResourceFileType() does not seem to exist on object<Tight\Modules\Localize\Localize>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
223
    }
224
225
    /**
226
     * @test
227
     * @depends testSetResourceFileType
228
     */
229
    public function testXmlGetValues() {
230
        $this->module->setResourceFileType(\Tight\Modules\Localize\LocalizeConfig::FILETYPE_XML);
231
        $this->assertEquals($this->localeEn, $this->module->getValues());
232
    }
233
234
}
235