Passed
Push — 4 ( e7d52d...941df1 )
by Garion
08:53
created

testThrowsMeaningfulExceptionWhenYmlIsCorrupted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\i18n\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\i18n\Messages\YamlReader;
7
use Symfony\Component\Translation\Exception\InvalidResourceException;
8
9
class YamlReaderTest extends SapphireTest
10
{
11
    /**
12
     *
13
     */
14
    public function testRead()
15
    {
16
        $reader = new YamlReader();
17
        $path = __DIR__ . '/i18nTest/_fakewebroot/i18ntestmodule/lang/en.yml';
18
        $output = $reader->read('en', $path);
19
        $expected = [
20
            'NONAMESPACE' => 'Include Entity without Namespace',
21
            'REPLACEMENTNONAMESPACE' => 'My replacement no namespace: {replacement}',
22
            'REPLACEMENTINCLUDENONAMESPACE' => 'My include replacement no namespace: {replacement}',
23
            'LAYOUTTEMPLATENONAMESPACE' => 'Layout Template no namespace',
24
            'i18nTestModule.ENTITY' => 'Entity with "Double Quotes"',
25
            'i18nTestModule.ADDITION' => 'Addition',
26
            'i18nTestModule.MAINTEMPLATE' => 'Main Template',
27
            'i18nTestModule.WITHNAMESPACE' => 'Include Entity with Namespace',
28
            'i18nTestModule.LAYOUTTEMPLATE' => 'Layout Template',
29
            'i18nTestModule.REPLACEMENTNAMESPACE' => 'My replacement: {replacement}',
30
            'i18nTestModuleInclude_ss.REPLACEMENTINCLUDENAMESPACE' => 'My include replacement: {replacement}',
31
            'i18nTestModule.PLURALS' => [
32
                'one' => 'A test',
33
                'other' => '{count} tests',
34
            ],
35
            'Month.PLURALS' => [
36
                'one' => 'A month',
37
                'other' => '{count} months',
38
            ],
39
        ];
40
        $this->assertEquals($expected, $output);
41
    }
42
43
44
    public function testThrowsMeaningfulExceptionWhenYmlIsCorrupted()
45
    {
46
        $path = __DIR__ . '/i18nTest/_fakewebroot/i18ntestmodule/lang/en_corrupt.yml';
47
        $this->expectException(InvalidResourceException::class);
48
        $regex_path = str_replace('.', '\.', $path);
49
        $this->expectExceptionMessageRegExp('@^Error parsing YAML, invalid file "' . $regex_path . '"\. Message: ([\w ].*) line 5 @');
50
        $reader = new YamlReader();
51
        $reader->read('en', $path);
52
    }
53
}
54