Completed
Pull Request — master (#68)
by Raúl
02:28 queued 01:14
created

PoFeaturesTest::testUpdateHeadersWrong()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Sepia\Test;
4
5
use Sepia\PoParser\Parser;
6
use Sepia\PoParser\PoReader\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 = Parser::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 = Parser::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
            $catalog = Parser::parseFile($this->resourcesPath.'healthy.po');
54
            $headers = $catalog->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
            $catalog = Parser::parseFile($this->resourcesPath.'multilines.po');
86
            $result = $catalog->getEntries();
87
            $headers = $catalog->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
            $catalog = Parser::parseFile($this->resourcesPath.'plurals.po');
105
            $headers = $catalog->getHeaders();
106
            $result = $catalog->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
            $catalog = Parser::parseFile($this->resourcesPath.'pluralsMultiline.po');
119
            $this->assertCount(2, $catalog->getEntries());
120
            $entries = $catalog->getEntries();
121
            foreach ($entries as $id => $entry) {
122
                $this->assertTrue(isset($entry['msgstr[0]']));
123
                $this->assertTrue(isset($entry['msgstr[1]']));
124
            }
125
        } catch (\Exception $e) {
126
            $this->fail($e->getMessage());
127
        }
128
    }
129
130
131
    /**
132
     *    Test Writing file
133
     */
134
    public function testWrite()
135
    {
136
        $this->markTestSkipped();
137
        // Read & write a simple file
138
        $catalog = Parser::parseFile($this->resourcesPath.'healthy.po');
139
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
140
141
        $this->assertFileEquals($this->resourcesPath.'healthy.po', $this->resourcesPath.'temp.po');
142
143
        // Read & write a file with no headers
144
        $catalog = Parser::parseFile($this->resourcesPath.'noheader.po');
145
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
146
147
        $this->assertFileEquals($this->resourcesPath.'noheader.po', $this->resourcesPath.'temp.po');
148
149
        // Read & write a po file with multilines
150
        $catalog = Parser::parseFile($this->resourcesPath.'multilines.po');
151
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
152
153
        $this->assertFileEquals($this->resourcesPath.'multilines.po', $this->resourcesPath.'temp.po');
154
155
        // Read & write a po file with contexts
156
        $catalog = Parser::parseFile($this->resourcesPath.'context.po');
157
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
158
159
        $this->assertFileEquals($this->resourcesPath.'context.po', $this->resourcesPath.'temp.po');
160
161
162
        // Read & write a po file with previous unstranslated strings
163
        $catalog = Parser::parseFile($this->resourcesPath.'previous_unstranslated.po');
164
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
165
166
        $this->assertFileEquals($this->resourcesPath.'previous_unstranslated.po', $this->resourcesPath.'temp.po');
167
168
        // Read & write a po file with multiple flags
169
        $catalog = Parser::parseFile($this->resourcesPath.'multiflags.po');
170
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
171
172
        $this->assertFileEquals($this->resourcesPath.'multiflags.po', $this->resourcesPath.'temp.po');
173
174
175
        unlink($this->resourcesPath.'temp.po');
176
    }
177
178
    /**
179
     * Test update entry, update plural forms
180
     */
181
    public function testUpdatePlurals()
182
    {
183
        $this->markTestSkipped();
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 = Parser::parseFile($this->resourcesPath.'plurals.po');
191
192
        $parser->setEntry(
0 ignored issues
show
Bug introduced by
The method setEntry() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
193
            $msgid,
194
            array(
195
                'msgid' => $msgid,
196
                'msgstr' => $msgstr,
197
            )
198
        );
199
200
        $parser->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
201
202
        $parser = Parser::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 = Parser::parseFile($this->resourcesPath.'context.po');
219
        $entries = $parser->getEntries();
220
221
        $entries[$msgid]['msgstr'] = array('translate');
222
        $parser->setEntry($msgid, $entries[$msgid]);
0 ignored issues
show
Bug introduced by
The method setEntry() does not seem to exist on object<Sepia\PoParser\Catalog>.

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 for success update headers
227
     */
228
    public function testUpdateHeaders()
229
    {
230
        $this->markTestSkipped();
231
        $parser = Parser::parseFile($this->resourcesPath.'context.po');
232
233
        $newHeaders = array(
234
            '"Project-Id-Version: \n"',
235
            '"Report-Msgid-Bugs-To: \n"',
236
            '"POT-Creation-Date: \n"',
237
            '"PO-Revision-Date: \n"',
238
            '"Last-Translator: none\n"',
239
            '"Language-Team: \n"',
240
            '"MIME-Version: 1.0\n"',
241
            '"Content-Type: text/plain; charset=UTF-8\n"',
242
            '"Content-Transfer-Encoding: 8bit\n"',
243
            '"Plural-Forms: nplurals=2; plural=n != 1;\n"',
244
        );
245
246
        $result = $parser->setHeaders($newHeaders);
0 ignored issues
show
Bug introduced by
The method setHeaders() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
247
        $this->assertTrue($result);
248
        $parser->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
249
250
        $newPoFile = Parser::parseFile($this->resourcesPath.'temp.po');
251
        $readHeaders = $newPoFile->getHeaders();
252
        $this->assertEquals($newHeaders, $readHeaders);
253
    }
254
255
    /**
256
     * Test for fail update headers
257
     */
258
    public function testUpdateHeadersWrong()
259
    {
260
        $this->markTestSkipped();
261
        $pofile = new Parser(new StringHandler(''));
262
        $result = $pofile->setHeaders('header');
0 ignored issues
show
Bug introduced by
The method setHeaders() does not seem to exist on object<Sepia\PoParser\Parser>.

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...
263
        $this->assertFalse($result);
264
    }
265
266
    /**
267
     * Test for po files with no blank lines between entries
268
     */
269
    public function testNoBlankLines()
270
    {
271
        $catalog = Parser::parseFile($this->resourcesPath.'noblankline.po');
272
        $entries = $catalog->getEntries();
273
274
        $expected = array(
275
            'one' => array(
276
                'msgid' => array(0 => 'one'),
277
                'msgstr' => array(0 => 'uno'),
278
            ),
279
            'two' => array(
280
                'msgid' => array(0 => 'two'),
281
                'msgstr' => array(0 => 'dos'),
282
            ),
283
        );
284
285
        $this->assertEquals($entries, $expected);
286
    }
287
288
289
    /**
290
     *  Test for entries with multiple flags
291
     */
292
    public function testFlags()
293
    {
294
        $this->markTestSkipped();
295
        // Read po file with 'php-format' flag. Add 'fuzzy' flag. 
296
        // Compare the result with the version that has 'php-format' and 'fuzzy' flags
297
        $catalog = Parser::parseFile($this->resourcesPath.'flags-phpformat.po');
298
        $entries = $catalog->getEntries();
299
300
        foreach ($entries as $msgid => $entry) {
301
            $entry['flags'][] = 'fuzzy';
302
            $catalog->setEntry($msgid, $entry);
0 ignored issues
show
Bug introduced by
The method setEntry() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
303
        }
304
305
        $catalog->writeFile($this->resourcesPath.'temp.po');
0 ignored issues
show
Bug introduced by
The method writeFile() does not seem to exist on object<Sepia\PoParser\Catalog>.

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...
306
        $this->assertFileEquals($this->resourcesPath.'flags-phpformat-fuzzy.po', $this->resourcesPath.'temp.po');
307
    }
308
309
310
    /**
311
     *  Test for reading previous unstranslated strings
312
     */
313
    public function testPreviousUnstranslated()
314
    {
315
        $catalog = Parser::parseFile($this->resourcesPath.'previous_unstranslated.po');
316
        $entries = $catalog->getEntries();
317
318
        $expected = array(
319
            'this is a string' => array(
320
                'msgid' => array('this is a string'),
321
                'msgstr' => array('this is a translation'),
322
                'previous' => array(
323
                    'msgid' => array('this is a previous string'),
324
                    'msgstr' => array('this is a previous translation string'),
325
                ),
326
            ),
327
        );
328
329
        $this->assertEquals($entries, $expected);
330
    }
331
}
332