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

WriteTest::testWrite()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
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
    /**
49
     * @throws \Exception
50
     */
51
    protected function saveCatalog(Catalog $catalog)
52
    {
53
        $fileHandler = new FileSystem($this->resourcesPath.'temp.po');
54
        $compiler = new PoCompiler();
55
        $fileHandler->save($compiler->compile($catalog));
56
    }
57
58
    private function assertPoFile(Catalog $catalogSource, Catalog $catalogNew)
59
    {
60
        foreach ($catalogSource->getEntries() as $entry) {
61
            $entryWritten = $catalogNew->getEntry($entry->getMsgId(), $entry->getMsgCtxt());
62
63
            $this->assertNotNull($entryWritten, 'Entry not found:'.$entry->getMsgId().','.$entry->getMsgCtxt());
64
65
            $this->assertEquals($entry->getMsgStr(), $entryWritten->getMsgStr());
66
            $this->assertEquals($entry->getMsgCtxt(), $entryWritten->getMsgCtxt());
67
            $this->assertEquals($entry->getFlags(), $entryWritten->getFlags());
68
            $this->assertEquals($entry->isObsolete(), $entryWritten->isObsolete());
69
70
            if ($entry->isObsolete() === true) {
71
                $this->assertEmpty($entryWritten->getReference());
72
                $this->assertEmpty($entryWritten->getTranslatorComments());
73
                $this->assertEmpty($entryWritten->getDeveloperComments());
74
            } else {
75
                $this->assertEquals($entry->getReference(), $entryWritten->getReference());
76
                $this->assertEquals($entry->getDeveloperComments(), $entryWritten->getDeveloperComments());
77
                $this->assertEquals($entry->getTranslatorComments(), $entryWritten->getTranslatorComments());
78
            }
79
        }
80
    }
81
82
    public function tearDown()
83
    {
84
        parent::tearDown();
85
86
        if (file_exists($this->resourcesPath.'temp.po')) {
87
        //    unlink($this->resourcesPath.'temp.po');
88
        }
89
    }
90
}
91