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

WriteTest::saveCatalog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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(
56
            $compiler->compile($catalog),
57
            $this->resourcesPath.'temp.po'
58
        );
59
    }
60
61
    private function assertPoFile(Catalog $catalogSource, Catalog $catalogNew)
62
    {
63
        foreach ($catalogSource->getEntries() as $entry) {
64
            $entryWritten = $catalogNew->getEntry($entry->getMsgId(), $entry->getMsgCtxt());
65
66
            $this->assertNotNull($entryWritten, 'Entry not found:'.$entry->getMsgId().','.$entry->getMsgCtxt());
67
68
            $this->assertEquals($entry->getMsgStr(), $entryWritten->getMsgStr());
69
            $this->assertEquals($entry->getMsgCtxt(), $entryWritten->getMsgCtxt());
70
            $this->assertEquals($entry->getFlags(), $entryWritten->getFlags());
71
            $this->assertEquals($entry->isObsolete(), $entryWritten->isObsolete());
72
73
            if ($entry->isObsolete() === true) {
74
                $this->assertEmpty($entryWritten->getReference());
75
                $this->assertEmpty($entryWritten->getTranslatorComments());
76
                $this->assertEmpty($entryWritten->getDeveloperComments());
77
            } else {
78
                $this->assertEquals($entry->getReference(), $entryWritten->getReference());
79
                $this->assertEquals($entry->getDeveloperComments(), $entryWritten->getDeveloperComments());
80
                $this->assertEquals($entry->getTranslatorComments(), $entryWritten->getTranslatorComments());
81
            }
82
        }
83
    }
84
85
    public function tearDown()
86
    {
87
        parent::tearDown();
88
89
        if (file_exists($this->resourcesPath.'temp.po')) {
90
        //    unlink($this->resourcesPath.'temp.po');
91
        }
92
    }
93
}
94