Completed
Push — develop ( b39607 )
by Raúl
16s
created

WriteTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 106
c 2
b 0
f 0
wmc 8
lcom 1
cbo 9
rs 10

5 Methods

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