Completed
Push — master ( 7ecb24...006012 )
by Tobias
06:02
created

XliffLoaderTest::testXliff20FromResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 20
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 14
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 View Code Duplication
    public function testXliff20Meta()
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...
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 View Code Duplication
    public function testXliff20FromResource()
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...
110
    {
111
        $file = __DIR__.'/../../Fixtures/meta.en.xlf';
112
113
        $catalogue = (new XliffLoader())->load($file, 'en', 'messages');
114
        $this->assertTrue($catalogue->defines('foo'));
115
        $metadata = $catalogue->getMetadata('foo');
116
        $this->assertNotEmpty($metadata);
117
        $this->assertCount(3, $metadata['notes']);
118
119
        $this->assertEquals('state', $metadata['notes'][0]['category']);
120
        $this->assertEquals('new', $metadata['notes'][0]['content']);
121
122
        $this->assertEquals('approved', $metadata['notes'][1]['category']);
123
        $this->assertEquals('true', $metadata['notes'][1]['content']);
124
125
        $this->assertEquals('section', $metadata['notes'][2]['category']);
126
        $this->assertEquals('1', $metadata['notes'][2]['priority']);
127
        $this->assertEquals('user login', $metadata['notes'][2]['content']);
128
    }
129
}
130