Completed
Pull Request — master (#71)
by
unknown
01:22
created

WriteTest::assertPoFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
cc 3
eloc 16
nc 3
nop 2
1
<?php
2
3
namespace Sepia\Test;
4
5
use Sepia\PoParser\Catalog;
6
use Sepia\PoParser\PoCompiler;
7
use Sepia\PoParser\SourceHandler\FileSystem;
8
9
class WriteTest extends AbstractFixtureTest
10
{
11
    public function testWrite()
12
    {
13
        $faker = \Faker\Factory::create();
14
        $catalogSource = new Catalog();
15
16
        // Normal Entry
17
        $entry = Catalog\EntryFactory::createFromArray(array(
18
            'msgid' => 'string.1',
19
            'msgstr' => 'translation.1',
20
            'msgctxt' => 'context.1',
21
            'reference' => array('src/views/forms.php:44'),
22
            'tcomment' => array('translator comment'),
23
            'ccomment' => array('code comment'),
24
            'flags' => array('1', '2', '3')
25
        ));
26
        $previousEntry = Catalog\EntryFactory::createFromArray(array(
27
           'msgid' => 'previous.string.1',
28
           'msgctxt' => 'previous.context.1'
29
        ));
30
        $entry->setPreviousEntry($previousEntry);
31
        $catalogSource->addEntry($entry);
32
33
        // Obsolete entry
34
        $entry = Catalog\EntryFactory::createFromArray(array(
35
            'msgid' => 'obsolete.1',
36
            'msgstr' => $faker->paragraph(5),
37
            'msgctxt' => 'obsolete.context',
38
            'obsolete' => true
39
        ));
40
        $catalogSource->addEntry($entry);
41
42
        $this->saveCatalog($catalogSource);
43
44
        $catalog = $this->parseFile('temp.po');
45
        $this->assertPoFile($catalogSource, $catalog);
46
    }
47
48
    public function testWritePlurals()
49
    {
50
        $catalogSource = new Catalog();
51
        // Normal Entry
52
        $entry = Catalog\EntryFactory::createFromArray(array(
53
            'msgid' => 'string.1',
54
            'msgstr' => 'translation.1',
55
            'msgstr[0]' => 'translation.plural.0',
56
            'msgstr[1]' => 'translation.plural.1',
57
            'msgstr[2]' => 'translation.plural.2',
58
            'reference' => array('src/views/forms.php:44'),
59
            'tcomment' => array('translator comment'),
60
            'ccomment' => array('code comment'),
61
            'flags' => array('1', '2', '3')
62
        ));
63
64
        $catalogSource->addEntry($entry);
65
66
        $this->saveCatalog($catalogSource);
67
        $catalog = $this->parseFile('temp.po');
68
        $entry = $catalog->getEntry('string.1');
69
        $this->assertCount(3, $entry->getMsgStrPlurals());
70
    }
71
72
    /**
73
     * @throws \Exception
74
     */
75
    protected function saveCatalog(Catalog $catalog)
76
    {
77
        $fileHandler = new FileSystem($this->resourcesPath.'temp.po');
78
        $compiler = new PoCompiler();
79
        $fileHandler->save($compiler->compile($catalog));
80
    }
81
82
    private function assertPoFile(Catalog $catalogSource, Catalog $catalogNew)
83
    {
84
        foreach ($catalogSource->getEntries() as $entry) {
85
            $entryWritten = $catalogNew->getEntry($entry->getMsgId(), $entry->getMsgCtxt());
86
87
            $this->assertNotNull($entryWritten, 'Entry not found:'.$entry->getMsgId().','.$entry->getMsgCtxt());
88
89
            $this->assertEquals($entry->getMsgStr(), $entryWritten->getMsgStr());
90
            $this->assertEquals($entry->getMsgCtxt(), $entryWritten->getMsgCtxt());
91
            $this->assertEquals($entry->getFlags(), $entryWritten->getFlags());
92
            $this->assertEquals($entry->isObsolete(), $entryWritten->isObsolete());
93
94
            if ($entry->isObsolete() === true) {
95
                $this->assertEmpty($entryWritten->getReference());
96
                $this->assertEmpty($entryWritten->getTranslatorComments());
97
                $this->assertEmpty($entryWritten->getDeveloperComments());
98
            } else {
99
                $this->assertEquals($entry->getReference(), $entryWritten->getReference());
100
                $this->assertEquals($entry->getDeveloperComments(), $entryWritten->getDeveloperComments());
101
                $this->assertEquals($entry->getTranslatorComments(), $entryWritten->getTranslatorComments());
102
            }
103
        }
104
    }
105
106
    public function tearDown()
107
    {
108
        parent::tearDown();
109
110
        if (file_exists($this->resourcesPath.'temp.po')) {
111
        //    unlink($this->resourcesPath.'temp.po');
112
        }
113
    }
114
}
115