Completed
Pull Request — master (#69)
by Raúl
02:46 queued 01:22
created

ReadPoTest::testBasic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Sepia\Test\UnitTest;
4
5
use Sepia\PoParser\Catalog\Entry;
6
use Sepia\Test\AbstractFixtureTest;
7
8
class ReadPoTest extends AbstractFixtureTest
9
{
10
    public function testBasic()
11
    {
12
        $catalog = $this->parseFile('basic.po');
13
14
        $entry = $catalog->getEntry('string.1');
15
16
        $this->assertNotNull($entry);
17
        $this->assertEquals('string.1', $entry->getMsgId());
18
        $this->assertEquals('translation.1', $entry->getMsgStr());
19
    }
20
21
    public function testBasicMultiline()
22
    {
23
        $catalog = $this->parseFile('basicMultiline.po');
24
25
        $entry = $catalog->getEntry('string.1');
26
27
        $this->assertNotNull($entry);
28
        $this->assertEquals('string.1', $entry->getMsgId());
29
        $this->assertEquals('translation line 1 translation line 2', $entry->getMsgStr());
30
    }
31
32
    public function testBasicCollection()
33
    {
34
        $catalog = $this->parseFile('basicCollection.po');
35
36
        $this->assertCount(2, $catalog->getEntries());
37
38
        $entry = $catalog->getEntry('string.1');
39
        $this->assertNotNull($entry);
40
        $this->assertEquals('string.1', $entry->getMsgId());
41
        $this->assertEquals('translation.1', $entry->getMsgStr());
42
43
        $entry = $catalog->getEntry('string.2');
44
        $this->assertNotNull($entry);
45
        $this->assertEquals('string.2', $entry->getMsgId());
46
        $this->assertEquals('translation.2', $entry->getMsgStr());
47
    }
48
49
    public function testEntriesWithContext()
50
    {
51
        $catalog = $this->parseFile('context.po');
52
53
        $withContext = $catalog->getEntry('string.1', 'register');
54
        $this->assertNotNull($withContext);
55
        $this->assertEquals('register', $withContext->getMsgCtxt());
56
57
        $withoutContext = $catalog->getEntry('string.1');
58
        $this->assertNotNull($withoutContext);
59
        $this->assertEmpty($withoutContext->getMsgCtxt());
60
        $this->assertNotEquals($withContext, $withoutContext);
61
    }
62
63
    public function testPlurals()
64
    {
65
        $catalog = $this->parseFile('plurals.po');
66
67
        $entry = $catalog->getEntry('%s post not updated, somebody is editing it.');
68
        $this->assertNotNull($entry);
69
        $this->assertNotEmpty($entry->getMsgStrPlurals());
70
        $this->assertEquals(
71
            array(
72
                '%s entrada no actualizada, alguien la está editando.',
73
                '%s entradas no actualizadas, alguien las está editando.',
74
            ),
75
            $entry->getMsgStrPlurals()
76
        );
77
    }
78
79
    public function testPluralsMultiline()
80
    {
81
        $catalog = $this->parseFile('pluralsMultiline.po');
82
        $entry = $catalog->getEntry('%s post not updated,somebody is editing it.');
83
84
        $this->assertNotNull($entry);
85
        $this->assertNotEmpty($entry->getMsgStrPlurals());
86
        $this->assertEquals(
87
            array(
88
                '%s entrada no actualizada,alguien la está editando.',
89
                '%s entradas no actualizadas,alguien las está editando.',
90
            ),
91
            $entry->getMsgStrPlurals()
92
        );
93
    }
94
    
95
    public function testFlags()
96
    {
97
        $catalog = $this->parseFile('multiflags.po');
98
99
        $this->assertCount(1, $catalog->getEntries());
100
        $entry = $catalog->getEntry('Attachment', 'Background Attachment');
101
102
        $this->assertNotNull($entry);
103
        $this->assertCount(2, $entry->getFlags());
104
        $this->assertEquals(array('php-format', 'fuzzy'), $entry->getFlags());
105
    }
106
107
    public function testTranslatorComment()
108
    {
109
        $catalog = $this->parseFile('healthy.po');
110
        $entry = $catalog->getEntry('string.2');
111
112
        $this->assertNotNull($entry);
113
        $this->assertEquals(array('Translator comment'), $entry->getTranslatorComments());
114
    }
115
116
    public function testTranslatorWithNoPreSpace()
117
    {
118
        $catalog = $this->parseFile('commentWithNoSpace.po');
119
        $entry = $catalog->getEntry('test');
120
121
        $this->assertNotNull($entry);
122
        $this->assertEquals(array('index.ctp:101'), $entry->getTranslatorComments());
123
    }
124
125
    public function testDeveloperComment()
126
    {
127
        $catalog = $this->parseFile('healthy.po');
128
        $entry = $catalog->getEntry('string.2');
129
130
        $this->assertNotNull($entry);
131
        $this->assertEquals(array('Code comment'), $entry->getDeveloperComments());
132
    }
133
134
135
    public function testPreviousUntranslated()
136
    {
137
        $catalog = $this->parseFile('previous_unstranslated.po');
138
139
        $this->assertCount(1, $catalog->getEntries());
140
141
        $entry = new Entry('this is a string', 'this is a translation');
142
        $entry->setPreviousEntry(new Entry('this is a previous string', 'this is a previous translation string'));
143
        $this->assertEquals(
144
            $entry,
145
            $catalog->getEntry('this is a string')
146
        );
147
    }
148
149
    public function testPreviousUntranslatedMultiline()
150
    {
151
        $catalog = $this->parseFile('previousStringMultiline.po');
152
153
        $entry = $catalog->getEntry('this is a string');
154
        $this->assertNotNull($entry);
155
156
        $previous = $entry->getPreviousEntry();
157
        $this->assertNotNull($previous);
158
        $this->assertEquals('this is a previous string', $previous->getMsgId());
159
        $this->assertEquals('Doloribus nulla odit et aut est. Rerum molestiae pariatur suscipit unde in quidem alias alias. Ut ea omnis placeat rerum quae asperiores. Et recusandae praesentium ea.', $previous->getMsgStr());
160
    }
161
162
163
164
165
    public function testMultilineEntries()
166
    {
167
        $catalog = $this->parseFile('multilines.po');
168
169
        $longMsgId = '%user% acaba de responder tu comentario.<br>Consulta que te ha dicho %link%aquí</a>.';
170
171
        $entryExpected = new Entry(
172
            $longMsgId,
173
            '%user% acaba de respondre el teu comentari.<br>Consulta que t\'ha dit %link%aquí</a>.'
174
        );
175
        $entryExpected->setReference(
176
            array('../../classes/controller/ccccc.php:361')
177
        );
178
179
        $entry = $catalog->getEntry($longMsgId);
180
        $this->assertNotNull($entry);
181
        $this->assertEquals($entryExpected, $entry);
182
    }
183
184
    public function testNoHeader()
185
    {
186
        $catalog = $this->parseFile('noheader.po');
187
188
        $this->assertCount(2, $catalog->getEntries());
189
    }
190
191
    public function testHeaders()
192
    {
193
        try {
194
            $catalog = $this->parseFile('healthy.po');
195
            $headers = $catalog->getHeaders();
196
197
            $this->assertCount(18, $headers);
198
            $this->assertEquals('Project-Id-Version: ', $headers[0]);
199
            $this->assertEquals('Report-Msgid-Bugs-To: ', $headers[1]);
200
            $this->assertEquals('POT-Creation-Date: 2013-09-25 15:55+0100', $headers[2]);
201
            $this->assertEquals('PO-Revision-Date: ', $headers[3]);
202
            $this->assertEquals('Last-Translator: Raúl Ferràs <[email protected]>', $headers[4]);
203
            $this->assertEquals('Language-Team: ', $headers[5]);
204
            $this->assertEquals('MIME-Version: 1.0', $headers[6]);
205
            $this->assertEquals('Content-Type: text/plain; charset=UTF-8', $headers[7]);
206
            $this->assertEquals('Content-Transfer-Encoding: 8bit', $headers[8]);
207
            $this->assertEquals('Plural-Forms: nplurals=2; plural=n != 1;', $headers[9]);
208
            $this->assertEquals('X-Poedit-SourceCharset: UTF-8', $headers[10]);
209
            $this->assertEquals('X-Poedit-KeywordsList: __;_e;_n;_t', $headers[11]);
210
            $this->assertEquals('X-Textdomain-Support: yes', $headers[12]);
211
            $this->assertEquals('X-Poedit-Basepath: .', $headers[13]);
212
            $this->assertEquals('X-Generator: Poedit 1.5.7', $headers[14]);
213
            $this->assertEquals('X-Poedit-SearchPath-0: .', $headers[15]);
214
            $this->assertEquals('X-Poedit-SearchPath-1: ../..', $headers[16]);
215
            $this->assertEquals('X-Poedit-SearchPath-2: ../../../modules', $headers[17]);
216
        } catch (\Exception $e) {
217
            $this->fail($e->getMessage());
218
        }
219
    }
220
221
    public function testNoBlankLinesSeparatingEntries()
222
    {
223
        $catalog = $this->parseFile('noblankline.po');
224
225
        $this->assertCount(2, $catalog->getEntries());
226
    }
227
}
228