Completed
Push — master ( c73eff...f26eb2 )
by Christian
03:34
created

XliffTest::validateXliff()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Resources;
15
16
use PHPUnit\Framework\TestCase;
17
use Symfony\Component\Translation\Exception\InvalidResourceException;
18
use Symfony\Component\Translation\Loader\XliffFileLoader;
19
20
final class XliffTest extends TestCase
21
{
22
    /**
23
     * @var XliffFileLoader
24
     */
25
    private $loader;
26
27
    /**
28
     * @var string[]
29
     */
30
    private $errors = [];
31
32
    protected function setUp(): void
33
    {
34
        $this->loader = new XliffFileLoader();
35
    }
36
37
    /**
38
     * @dataProvider getXliffPaths
39
     */
40
    public function testXliff($path): void
41
    {
42
        $this->validatePath($path);
43
        if (\count($this->errors) > 0) {
44
            $this->fail(sprintf('Unable to parse xliff files: %s', implode(', ', $this->errors)));
45
        }
46
        $this->assertCount(
47
            0,
48
            $this->errors,
49
            sprintf('Unable to parse xliff files: %s', implode(', ', $this->errors))
50
        );
51
    }
52
53
    /**
54
     * @return string[] List all path to validate xliff
55
     */
56
    public function getXliffPaths(): array
57
    {
58
        return [[__DIR__.'/../../src/Resources/translations']];
59
    }
60
61
    /**
62
     * @param string $file The path to the xliff file
63
     */
64
    private function validateXliff(string $file): void
65
    {
66
        try {
67
            $this->loader->load($file, 'en');
68
            $this->assertTrue(true, sprintf('Successful loading file: %s', $file));
69
        } catch (InvalidResourceException $e) {
70
            $this->errors[] = sprintf('%s => %s', $file, $e->getMessage());
71
        }
72
    }
73
74
    /**
75
     * @param string $path The path to lookup for Xliff file
76
     */
77
    private function validatePath(string $path): void
78
    {
79
        $files = glob(sprintf('%s/*.xliff', $path));
80
        foreach ($files as $file) {
81
            $this->validateXliff($file);
82
        }
83
    }
84
}
85