Passed
Push — feature/lang_file_unit_test ( 635234 )
by Tristan
09:04
created

testAllLangValuesDifferentInFrenchAndEnglish()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
cc 7
nc 10
nop 0
1
<?php
2
namespace Tests\Unit;
3
4
use Tests\TestCase;
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\App;
7
8
class LangFilesTest extends BaseTranslationTest
9
{
10
    public function testAllLangFilesWellFormatted()
11
    {
12
        foreach ($this->locales as $locale) {
13
            foreach ($this->getAllLangFilenames() as $langFile) {
14
                $this->assertInternalType('array', Lang::get($langFile));
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

14
                /** @scrutinizer ignore-deprecated */ $this->assertInternalType('array', Lang::get($langFile));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
15
            }
16
        }
17
    }
18
19
    /**
20
     * Tests for lang entries that are empty strings.
21
     * If tests are run with --verbose, displays wich keys have empty values.
22
     *
23
     * @return void
24
     */
25
    public function testNoEmptyStrings()
26
    {
27
        $emptyEntries = [];
28
        foreach ($this->getAllLangPaths() as $path) {
29
            foreach ($this->locales as $locale) {
30
                App::setLocale($locale);
31
                $value = Lang::get($path);
32
                if ($value === "") {
33
                    $fullPath = $locale . "/" . $path;
34
                    array_push($emptyEntries, $fullPath);
35
                }
36
            }
37
        }
38
        if (!empty($emptyEntries)) {
39
            print_r("\n");
40
            print_r("The following lang entries are empty strings:\n");
41
            print_r($emptyEntries);
42
            print_r("\n");
43
        }
44
        $this->assertEmpty($emptyEntries);
45
    }
46
47
    /**
48
     * Contains lang keys that are expected to be missing in a particular language
49
     *
50
     * @var array
51
     */
52
    protected $permittedMissing = [
53
        'en' => [
54
            'validation.attributes.name'
55
        ],
56
        'fr' => []
57
    ];
58
59
    /**
60
     * Tests for lang entries that are an empty array instead of a string,
61
     * or that are present in one language but not another. Ignores keys
62
     * in $this->permittedMissing.
63
     *
64
     * If tests are run with --verbose, displays wich keys are missing.
65
     *
66
     * @return void
67
     */
68
    public function testNoMissingStrings()
69
    {
70
        $missingEntries = [];
71
        foreach ($this->locales as $locale) {
72
            $missingEntries[$locale] = [];
73
        }
74
        foreach ($this->getAllLangPaths() as $path) {
75
            foreach ($this->locales as $locale) {
76
                App::setLocale($locale);
77
                if (!Lang::has($path) && !in_array($path, $this->permittedMissing[$locale])) {
78
                    array_push($missingEntries[$locale], $path);
79
                }
80
            }
81
        }
82
        $allMissingEntries = [];
83
        foreach ($this->locales as $locale) {
84
            if (!empty($missingEntries[$locale])) {
85
                print_r("\n");
86
                print_r("The following lang entries are missing in $locale\n");
87
                print_r($missingEntries[$locale]);
88
                print_r("\n");
89
            }
90
            $allMissingEntries = array_merge($allMissingEntries, $missingEntries[$locale]);
91
        }
92
93
        $this->assertEmpty($allMissingEntries);
94
    }
95
96
    /**
97
     * The list of keys that are expected to have identical values in multiple languages.
98
     * If tests are run with --verbose, displays wich keys have identical values.
99
     *
100
     * @var array
101
     */
102
    protected $sameTranslations = [];
103
    public function testAllLangValuesDifferentInFrenchAndEnglish()
104
    {
105
        $identicalEntries = [];
106
        foreach ($this->getAllLangPaths() as $path) {
107
            $prevValues = [];
108
            foreach ($this->locales as $locale) {
109
                App::setLocale($locale);
110
                $value = Lang::get($path);
111
                if (in_array($value, $this->sameTranslations)) {
112
                //TODO: do nothing?
113
                } elseif (Lang::has($path) && in_array($value, $prevValues)) {
114
                    array_push($identicalEntries, $path);
115
                // $this->assertNotContains($value, $prevValues);
116
                }
117
                array_push($prevValues, $value);
118
            }
119
        }
120
        $identicalEntries = array_unique($identicalEntries);
121
        if (!empty($identicalEntries)) {
122
            print_r("\n");
123
            print_r("The following lang entries are identical in multiple languages:\n");
124
            print_r($identicalEntries);
125
            print_r("\n");
126
        }
127
        $this->assertEmpty($identicalEntries);
128
    }
129
}
130