ConverterTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 6 1
A setUp() 0 10 3
A testConvertJMS() 0 20 1
A testConvertYaml() 0 9 1
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\Converter\Tests\Functional\Service;
13
14
use PHPUnit\Framework\TestCase;
15
use Translation\Bundle\Model\Metadata;
16
use Translation\Converter\Reader\JmsReader;
17
use Translation\Converter\Service\Converter;
18
use Translation\SymfonyStorage\Loader\XliffLoader;
19
use Symfony\Component\Translation\Loader;
20
21
class ConverterTest extends TestCase
22
{
23
    protected static $fixturesDir;
24
25
    protected static $outputDir;
26
27
    public static function setUpBeforeClass()
28
    {
29
        self::$outputDir = sys_get_temp_dir().'/translation_converter_output';
30
        self::$fixturesDir = __DIR__.'/../../Fixtures';
31
        parent::setUpBeforeClass();
32
    }
33
34
    protected function setUp()
35
    {
36
        @mkdir(self::$outputDir, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
37
        $files = glob(self::$outputDir.'/*');
38
        foreach ($files as $file) {
39
            if (is_file($file)) {
40
                unlink($file);
41
            }
42
        }
43
    }
44
45
    public function testConvertJMS()
46
    {
47
        $reader = new JmsReader();
48
        $converter = new Converter($reader, ['xlf', 'xliff']);
49
        $converter->convert(self::$fixturesDir, self::$outputDir, ['en']);
50
51
        $catalogue = (new XliffLoader())->load(self::$outputDir.'/messages.en.xlf', 'en');
52
        $this->assertEquals('This is a bar.', $catalogue->get('bar'));
53
        $meta = new Metadata($catalogue->getMetadata('bar'));
54
        $this->assertTrue($meta->isApproved());
55
        $this->assertEquals('new', $meta->getState());
56
        $this->assertEquals('Tests/Translation/XliffMessageUpdaterTest.php', $meta->getSourceLocations()[0]['path']);
57
58
        $catalogue = (new XliffLoader())->load(self::$outputDir.'/xliff.en.xlf', 'en');
59
        $this->assertEquals('This is a bar.', $catalogue->get('bar'));
60
        $meta = new Metadata($catalogue->getMetadata('bar'));
61
        $this->assertTrue($meta->isApproved());
62
        $this->assertEquals('new', $meta->getState());
63
        $this->assertEquals('Tests/Translation/XliffMessageUpdaterTest.php', $meta->getSourceLocations()[0]['path']);
64
    }
65
66
    public function testConvertYaml()
67
    {
68
        $reader = new Loader\YamlFileLoader();
69
        $converter = new Converter($reader, 'yml');
70
        $converter->convert(self::$fixturesDir, self::$outputDir, ['en']);
71
72
        $catalogue = (new XliffLoader())->load(self::$outputDir.'/admin.en.xlf', 'en');
73
        $this->assertEquals('Business', $catalogue->get('en.symbol.anonymous'));
74
    }
75
}
76