DictionaryTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 72
c 1
b 0
f 0
dl 0
loc 120
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testMultidimensionalRecords() 0 22 1
A testExtend() 0 42 1
A testNoTranslation() 0 4 1
A testExtendWithBadLanguage() 0 30 1
A testFlatRecords() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Tests;
6
7
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Efabrica\Translatte\Dictionary;
9
use InvalidArgumentException;
10
11
class DictionaryTest extends TestCase
12
{
13
    public function testNoTranslation(): void
14
    {
15
        $dictionary = new Dictionary('sk_SK', []);
16
        $this->assertNull($dictionary->findTranslation('no_translation'));
17
    }
18
19
    public function testFlatRecords(): void
20
    {
21
        $records = [
22
            'to.translate.key1' => 'translated.key1',
23
            'to.translate.key2' => 'translated.key2',
24
            'to.translate.key24' => 'translated.key24'
25
        ];
26
27
        $dictionary = new Dictionary('sk_SK', $records);
28
        $this->assertSame('sk_SK', $dictionary->getLang());
29
        $this->assertSame($records, $dictionary->getRecords());
30
        $this->assertSame($records['to.translate.key24'], $dictionary->findTranslation('to.translate.key24'));
31
    }
32
33
    public function testMultidimensionalRecords(): void
34
    {
35
        $recordsMultidimensional = [
36
            'to' => [
37
                'translate' => [
38
                    'key1' => 'translated.key1',
39
                    'key2' => 'translated.key2',
40
                    'key24' => 'translated.key24'
41
                ]
42
            ]
43
        ];
44
45
        $recordsFlat = [
46
            'to.translate.key1' => 'translated.key1',
47
            'to.translate.key2' => 'translated.key2',
48
            'to.translate.key24' => 'translated.key24'
49
        ];
50
51
        $dictionary = new Dictionary('en_US', $recordsMultidimensional);
52
        $this->assertSame('en_US', $dictionary->getLang());
53
        $this->assertSame($recordsFlat, $dictionary->getRecords());
54
        $this->assertSame($recordsMultidimensional['to']['translate']['key24'], $dictionary->findTranslation('to.translate.key24'));
55
    }
56
57
    public function testExtend(): void
58
    {
59
        $records1 = [
60
            'to' => [
61
                'translate' => [
62
                    'key1' => 'translated.key1',
63
                    'key2' => 'translated.key2',
64
                    'key24' => 'translated.key24'
65
                ]
66
            ]
67
        ];
68
69
        $records2 = [
70
            'to' => [
71
                'translate' => [
72
                    'key7' => 'translated.key7',
73
                    'key8' => 'translated.key8',
74
                    'key77' => 'translated.key77'
75
                ]
76
            ],
77
            'another' => [
78
                'to_translate' => 'translated'
79
            ]
80
        ];
81
82
        $recordsResult = [
83
            'to.translate.key1' => 'translated.key1',
84
            'to.translate.key2' => 'translated.key2',
85
            'to.translate.key24' => 'translated.key24',
86
            'to.translate.key7' => 'translated.key7',
87
            'to.translate.key8' => 'translated.key8',
88
            'to.translate.key77' => 'translated.key77',
89
            'another.to_translate' => 'translated'
90
        ];
91
92
        $dictionary1 = new Dictionary('sk_SK', $records1);
93
        $dictionary2 = new Dictionary('sk_SK', $records2);
94
        $dictionary1->extend($dictionary2);
95
        $this->assertSame($recordsResult, $dictionary1->getRecords());
96
        $this->assertSame($recordsResult['to.translate.key24'], $dictionary1->findTranslation('to.translate.key24'));
97
        $this->assertSame($recordsResult['to.translate.key7'], $dictionary1->findTranslation('to.translate.key7'));
98
        $this->assertSame($recordsResult['another.to_translate'], $dictionary1->findTranslation('another.to_translate'));
99
    }
100
101
    public function testExtendWithBadLanguage(): void
102
    {
103
        $records1 = [
104
            'to' => [
105
                'translate' => [
106
                    'key1' => 'translated.key1',
107
                    'key2' => 'translated.key2',
108
                    'key24' => 'translated.key24'
109
                ]
110
            ]
111
        ];
112
113
        $records2 = [
114
            'to' => [
115
                'translate' => [
116
                    'key7' => 'translated.key7',
117
                    'key8' => 'translated.key8',
118
                    'key77' => 'translated.key77'
119
                ]
120
            ],
121
            'another' => [
122
                'to_translate' => 'translated'
123
            ]
124
        ];
125
126
        $dictionary1 = new Dictionary('sk_SK', $records1);
127
        $dictionary2 = new Dictionary('en_US', $records2);
128
129
        $this->expectException(InvalidArgumentException::class);
130
        $dictionary1->extend($dictionary2);
131
    }
132
}
133