Completed
Push — master ( 75ac31...11edf4 )
by Tobias
08:15 queued 06:58
created

XliffLoaderTest::testXliff20Meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\SymfonyStorage\Tests\Unit\Loader;
13
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\Translation\Exception\InvalidResourceException;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Translation\SymfonyStorage\Loader\XliffLoader;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class XliffLoaderTest extends TestCase
23
{
24
    /**
25
     * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
26
     */
27
    public function testEmptyContent()
28
    {
29
        $loader = new XliffLoader();
30
        $loader->extractFromContent(' ', new MessageCatalogue('en'), 'messages');
31
    }
32
33
    public function testInvalidContent()
34
    {
35
        $loader = new XliffLoader();
36
37
        try {
38
            $loader->extractFromContent('Foobar', new MessageCatalogue('en'), 'messages');
39
        } catch (InvalidResourceException $e) {
40
            $invalidArgument = $e->getPrevious();
41
            $this->assertNotNull($invalidArgument);
42
            $this->assertContains('[ERROR 4] Start tag expected', $invalidArgument->getMessage());
43
44
            return;
45
        }
46
        $this->fail('XliffLoader must throw exception on invalid XML');
47
    }
48
49 View Code Duplication
    public function testXliff12()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $content = file_get_contents(__DIR__.'/../../Fixtures/single-file/messages.en.xlf');
52
        $catalogue = new MessageCatalogue('en');
53
        (new XliffLoader())->extractFromContent($content, $catalogue, 'messages');
54
        $this->assertTrue($catalogue->defines('test_0'));
55
        $this->assertTrue($catalogue->defines('test_1'));
56
    }
57
58 View Code Duplication
    public function testXliff20()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $content = <<<'XML'
61
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0"
62
 srcLang="en-US" trgLang="sv">
63
 <file id="f1" original="Example">
64
  <skeleton href="Example"/>
65
  <unit id="1">
66
   <segment>
67
    <source>key0</source>
68
    <target>Foo</target>
69
   </segment>
70
  </unit>
71
  <unit id="2">
72
   <segment>
73
    <source>key1</source>
74
    <target>Bar</target>
75
   </segment>
76
  </unit>
77
 </file>
78
</xliff>
79
XML;
80
81
        $catalogue = new MessageCatalogue('en');
82
        (new XliffLoader())->extractFromContent($content, $catalogue, 'messages');
83
        $this->assertTrue($catalogue->defines('key0'));
84
        $this->assertTrue($catalogue->defines('key1'));
85
    }
86
87
    public function testXliff20Meta()
88
    {
89
        $content = file_get_contents(__DIR__.'/../../Fixtures/meta.en.xlf');
90
91
        $catalogue = new MessageCatalogue('en');
92
        (new XliffLoader())->extractFromContent($content, $catalogue, 'messages');
93
        $this->assertTrue($catalogue->defines('foo'));
94
        $metadata = $catalogue->getMetadata('foo');
95
        $this->assertNotEmpty($metadata);
96
        $this->assertCount(3, $metadata['notes']);
97
98
        $this->assertEquals('state', $metadata['notes'][0]['category']);
99
        $this->assertEquals('new', $metadata['notes'][0]['content']);
100
101
        $this->assertEquals('approved', $metadata['notes'][1]['category']);
102
        $this->assertEquals('true', $metadata['notes'][1]['content']);
103
104
        $this->assertEquals('section', $metadata['notes'][2]['category']);
105
        $this->assertEquals('1', $metadata['notes'][2]['priority']);
106
        $this->assertEquals('user login', $metadata['notes'][2]['content']);
107
    }
108
}
109