Completed
Push — feature/high_five ( cf7883...85675c )
by Raúl
05:22
created

PoFeaturesTest::testPlurals()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 6
nop 0
1
<?php
2
3
namespace Sepia\Test;
4
5
use Sepia\PoParser;
6
use Sepia\StringHandler;
7
8
class PoFeaturesTest extends AbstractFixtureTest
9
{
10
    public function tearDown()
11
    {
12
        parent::tearDown();
13
14
        if (file_exists($this->resourcesPath.'temp.po')) {
15
            unlink($this->resourcesPath.'temp.po');
16
        }
17
    }
18
19
    public function testRead()
20
    {
21
        try {
22
            $parser = PoParser::parseFile($this->resourcesPath.'healthy.po');
23
            $result = $parser->getEntries();
24
        } catch (\Exception $e) {
25
            $result = array();
26
            $this->fail($e->getMessage());
27
        }
28
29
        $this->assertCount(2, $result);
30
31
32
        // Read file without headers.
33
        // It should not skip first entry
34
        try {
35
            $parser = PoParser::parseFile($this->resourcesPath.'noheader.po');
36
            $result = $parser->getEntries();
37
        } catch (\Exception $e) {
38
            $result = array();
39
            $this->fail($e->getMessage());
40
        }
41
42
        $this->assertCount(2, $result, 'Did not read properly po file without headers.');
43
    }
44
45
46
    /**
47
     *    Tests reading the headers.
48
     *
49
     */
50
    public function testHeaders()
51
    {
52
        try {
53
            $parser = PoParser::parseFile($this->resourcesPath.'healthy.po');
54
            $headers = $parser->getHeaders();
55
56
            $this->assertCount(18, $headers);
57
            $this->assertEquals("\"Project-Id-Version: \\n\"", $headers[0]);
58
            $this->assertEquals("\"Report-Msgid-Bugs-To: \\n\"", $headers[1]);
59
            $this->assertEquals("\"POT-Creation-Date: 2013-09-25 15:55+0100\\n\"", $headers[2]);
60
            $this->assertEquals("\"PO-Revision-Date: \\n\"", $headers[3]);
61
            $this->assertEquals("\"Last-Translator: Raúl Ferràs <[email protected]>\\n\"", $headers[4]);
62
            $this->assertEquals("\"Language-Team: \\n\"", $headers[5]);
63
            $this->assertEquals("\"MIME-Version: 1.0\\n\"", $headers[6]);
64
            $this->assertEquals("\"Content-Type: text/plain; charset=UTF-8\\n\"", $headers[7]);
65
            $this->assertEquals("\"Content-Transfer-Encoding: 8bit\\n\"", $headers[8]);
66
            $this->assertEquals("\"Plural-Forms: nplurals=2; plural=n != 1;\\n\"", $headers[9]);
67
            $this->assertEquals("\"X-Poedit-SourceCharset: UTF-8\\n\"", $headers[10]);
68
            $this->assertEquals("\"X-Poedit-KeywordsList: __;_e;_n;_t\\n\"", $headers[11]);
69
            $this->assertEquals("\"X-Textdomain-Support: yes\\n\"", $headers[12]);
70
            $this->assertEquals("\"X-Poedit-Basepath: .\\n\"", $headers[13]);
71
            $this->assertEquals("\"X-Generator: Poedit 1.5.7\\n\"", $headers[14]);
72
            $this->assertEquals("\"X-Poedit-SearchPath-0: .\\n\"", $headers[15]);
73
            $this->assertEquals("\"X-Poedit-SearchPath-1: ../..\\n\"", $headers[16]);
74
            $this->assertEquals("\"X-Poedit-SearchPath-2: ../../../modules\\n\"", $headers[17]);
75
        } catch (\Exception $e) {
76
            $this->fail($e->getMessage());
77
//			$this->assertTrue( false, $e->getMessage() );
78
        }
79
    }
80
81
82
    public function testMultilineId()
83
    {
84
        try {
85
            $parser = PoParser::parseFile($this->resourcesPath.'multilines.po');
86
            $result = $parser->getEntries();
87
            $headers = $parser->getHeaders();
88
89
            $this->assertCount(18, $headers);
90
            $this->assertCount(9, $result);
91
        } catch (\Exception $e) {
92
            $this->fail($e->getMessage());
93
        }
94
    }
95
96
97
    /**
98
     *
99
     *
100
     */
101
    public function testPlurals()
102
    {
103
        try {
104
            $parser = PoParser::parseFile($this->resourcesPath.'plurals.po');
105
            $headers = $parser->getHeaders();
106
            $result = $parser->getEntries();
107
108
            $this->assertCount(7, $headers);
109
            $this->assertCount(15, $result);
110
        } catch (\Exception $e) {
111
            $this->fail($e->getMessage());
112
        }
113
    }
114
115
    public function testPluralsMultiline()
116
    {
117
        try {
118
            $parser = PoParser::parseFile($this->resourcesPath.'pluralsMultiline.po');
119
            $this->assertCount(2, $parser->getEntries());
120
            $entries = $parser->getEntries();
121
            $msgStringZero = "";
0 ignored issues
show
Unused Code introduced by
$msgStringZero is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
            $msgStringOne = "";
0 ignored issues
show
Unused Code introduced by
$msgStringOne is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123
            foreach ($entries as $id => $entry) {
124
                $this->assertTrue(isset($entry['msgstr[0]']));
125
                $this->assertTrue(isset($entry['msgstr[1]']));
126
            }
127
        } catch (\Exception $e) {
128
            $this->fail($e->getMessage());
129
        }
130
    }
131
132
133
    /**
134
     *    Test Writing file
135
     */
136
    public function testWrite()
137
    {
138
        // Read & write a simple file
139
        $parser = PoParser::parseFile($this->resourcesPath.'healthy.po');
140
        $parser->writeFile($this->resourcesPath.'temp.po');
141
142
        $this->assertFileEquals($this->resourcesPath.'healthy.po', $this->resourcesPath.'temp.po');
143
144
        // Read & write a file with no headers
145
        $parser = PoParser::parseFile($this->resourcesPath.'noheader.po');
146
        $parser->writeFile($this->resourcesPath.'temp.po');
147
148
        $this->assertFileEquals($this->resourcesPath.'noheader.po', $this->resourcesPath.'temp.po');
149
150
        // Read & write a po file with multilines
151
        $parser = PoParser::parseFile($this->resourcesPath.'multilines.po');
152
        $parser->writeFile($this->resourcesPath.'temp.po');
153
154
        $this->assertFileEquals($this->resourcesPath.'multilines.po', $this->resourcesPath.'temp.po');
155
156
        // Read & write a po file with contexts
157
        $parser = PoParser::parseFile($this->resourcesPath.'context.po');
158
        $parser->writeFile($this->resourcesPath.'temp.po');
159
160
        $this->assertFileEquals($this->resourcesPath.'context.po', $this->resourcesPath.'temp.po');
161
162
163
        // Read & write a po file with previous unstranslated strings
164
        $parser = PoParser::parseFile($this->resourcesPath.'previous_unstranslated.po');
165
        $parser->writeFile($this->resourcesPath.'temp.po');
166
167
        $this->assertFileEquals($this->resourcesPath.'previous_unstranslated.po', $this->resourcesPath.'temp.po');
168
169
        // Read & write a po file with multiple flags
170
        $parser = PoParser::parseFile($this->resourcesPath.'multiflags.po');
171
        $parser->writeFile($this->resourcesPath.'temp.po');
172
173
        $this->assertFileEquals($this->resourcesPath.'multiflags.po', $this->resourcesPath.'temp.po');
174
175
176
        unlink($this->resourcesPath.'temp.po');
177
    }
178
179
    /**
180
     * Test update entry, update plural forms
181
     */
182
    public function testUpdatePlurals()
183
    {
184
        $msgid = '%s post not updated, somebody is editing it.';
185
        $msgstr = array(
186
            "%s entrada no actualizada, alguien la está editando...",
187
            "%s entradas no actualizadas, alguien las está editando...",
188
        );
189
190
        $parser = PoParser::parseFile($this->resourcesPath.'plurals.po');
191
192
        $parser->setEntry(
193
            $msgid,
194
            array(
195
                'msgid' => $msgid,
196
                'msgstr' => $msgstr,
197
            )
198
        );
199
200
        $parser->writeFile($this->resourcesPath.'temp.po');
201
202
        $parser = PoParser::parseFile($this->resourcesPath.'temp.po');
203
        $newPlurals = $parser->getEntries();
204
        $this->assertEquals($newPlurals[$msgid]['msgstr'], $msgstr);
205
    }
206
207
208
    /**
209
     * Test update with fuzzy flag.
210
     *
211
     * @todo
212
     */
213
    public function testUpdateWithFuzzy()
214
    {
215
        $this->markTestSkipped();
216
        $msgid = '%1$s-%2$s';
217
218
        $parser = PoParser::parseFile($this->resourcesPath.'context.po');
219
        $entries = $parser->getEntries();
220
221
        $entries[$msgid]['msgstr'] = array('translate');
222
        $parser->setEntry($msgid, $entries[$msgid]);
223
    }
224
225
    /**
226
     * Test for success update headers
227
     */
228
    public function testUpdateHeaders()
229
    {
230
        $parser = PoParser::parseFile($this->resourcesPath.'context.po');
231
232
        $newHeaders = array(
233
            '"Project-Id-Version: \n"',
234
            '"Report-Msgid-Bugs-To: \n"',
235
            '"POT-Creation-Date: \n"',
236
            '"PO-Revision-Date: \n"',
237
            '"Last-Translator: none\n"',
238
            '"Language-Team: \n"',
239
            '"MIME-Version: 1.0\n"',
240
            '"Content-Type: text/plain; charset=UTF-8\n"',
241
            '"Content-Transfer-Encoding: 8bit\n"',
242
            '"Plural-Forms: nplurals=2; plural=n != 1;\n"',
243
        );
244
245
        $result = $parser->setHeaders($newHeaders);
246
        $this->assertTrue($result);
247
        $parser->writeFile($this->resourcesPath.'temp.po');
248
249
        $newPoFile = PoParser::parseFile($this->resourcesPath.'temp.po');
250
        $readHeaders = $newPoFile->getHeaders();
251
        $this->assertEquals($newHeaders, $readHeaders);
252
    }
253
254
    /**
255
     * Test for fail update headers
256
     */
257
    public function testUpdateHeadersWrong()
258
    {
259
        $pofile = new PoParser(new StringHandler(''));
260
        $result = $pofile->setHeaders('header');
261
        $this->assertFalse($result);
262
    }
263
264
    /**
265
     * Test for po files with no blank lines between entries
266
     */
267
    public function testNoBlankLines()
268
    {
269
        $parser = PoParser::parseFile($this->resourcesPath.'noblankline.po');
270
        $entries = $parser->getEntries();
271
272
        $expected = array(
273
            'one' => array(
274
                'msgid' => array(0 => 'one'),
275
                'msgstr' => array(0 => 'uno'),
276
            ),
277
            'two' => array(
278
                'msgid' => array(0 => 'two'),
279
                'msgstr' => array(0 => 'dos'),
280
            ),
281
        );
282
283
        $this->assertEquals($entries, $expected);
284
    }
285
286
287
    /**
288
     *  Test for entries with multiple flags
289
     */
290
    public function testFlags()
291
    {
292
        // Read po file with 'php-format' flag. Add 'fuzzy' flag. 
293
        // Compare the result with the version that has 'php-format' and 'fuzzy' flags
294
        $parser = PoParser::parseFile($this->resourcesPath.'flags-phpformat.po');
295
        $entries = $parser->getEntries();
296
297
        foreach ($entries as $msgid => $entry) {
298
            $entry['flags'][] = 'fuzzy';
299
            $parser->setEntry($msgid, $entry);
300
        }
301
302
        $parser->writeFile($this->resourcesPath.'temp.po');
303
        $this->assertFileEquals($this->resourcesPath.'flags-phpformat-fuzzy.po', $this->resourcesPath.'temp.po');
304
    }
305
306
307
    /**
308
     *  Test for reading previous unstranslated strings
309
     */
310
    public function testPreviousUnstranslated()
311
    {
312
        $parser = PoParser::parseFile($this->resourcesPath.'previous_unstranslated.po');
313
        $entries = $parser->getEntries();
314
315
        $expected = array(
316
            'this is a string' => array(
317
                'msgid' => array('this is a string'),
318
                'msgstr' => array('this is a translation'),
319
                'previous' => array(
320
                    'msgid' => array('this is a previous string'),
321
                    'msgstr' => array('this is a previous translation string'),
322
                ),
323
            ),
324
        );
325
326
        $this->assertEquals($entries, $expected);
327
    }
328
}
329