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

BaseTranslationTest::getLangFilenames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Tests\Unit;
4
5
use Tests\TestCase;
6
use Illuminate\Support\Facades\Lang;
7
use Illuminate\Support\Facades\App;
8
9
abstract class BaseTranslationTest extends TestCase
10
{
11
    /**
12
     * The directory containing all lang files.
13
     *
14
     * @var string
15
     */
16
    protected $rootLangDir = 'resources/lang';
17
18
    /**
19
     * The locale codes being used by the app.
20
     *
21
     * @var string[]
22
     */
23
    protected $locales = ['en', 'fr'];
24
25
    /**
26
     * Return an array of all Lang files in directory tree
27
     * @param  string $dir directory to start searching in
28
     */
29
    protected function getAllFilesInDirectoryTree($dir) {
30
        $dir = trim($dir, '/');
31
        $filesAndDirectories = scandir($dir);
32
        $files = [];
33
        foreach($filesAndDirectories as $fileName) {
34
            $path = implode('/', [$dir, $fileName]);
35
            if ($fileName === '.' || $fileName === '..') {
36
                //Do nothing
37
            } elseif (is_dir($path)) {
38
                $files = array_merge($files, $this->getAllFilesInDirectoryTree($path));
39
            } else {
40
                array_push($files, $path);
41
            }
42
        }
43
        return $files;
44
    }
45
46
    protected function removePrefix($str, $prefix) {
47
        if (substr($str, 0, strlen($prefix)) == $prefix) {
48
            $str = substr($str, strlen($prefix));
49
        }
50
        return $str;
51
    }
52
53
    protected function removeFiletype($filename) {
54
        return preg_replace('/\\.[^.\\s]+$/', '', $filename);
55
    }
56
57
    protected function formatLangFilenames($langFiles, $prefix) {
58
        $output = [];
59
        foreach($langFiles as $file) {
60
            $relativeName = $this->removePrefix($file, $prefix);
61
            $relativeName = $this->removeFiletype($relativeName);
62
            $relativeName = trim($relativeName, '/');
63
            array_push($output, $relativeName);
64
        }
65
        return $output;
66
    }
67
68
    protected function getLangFilenames($lang) {
69
        $dir = implode('/', [$this->rootLangDir, $lang]);
70
        $langFiles = $this->getAllFilesInDirectoryTree($dir);
71
        $formatedLangFiles = $this->formatLangFilenames($langFiles, $dir);
72
        return $formatedLangFiles;
73
    }
74
75
    protected function getAllLangFilenames() {
76
        $langs = ['en', 'fr'];
77
        $output = [];
78
        foreach ($langs as $lang) {
79
            $output = array_merge($output, $this->getLangFilenames($lang));
80
        }
81
        return $output;
82
    }
83
84
    /**
85
     * Return an array of strings that can be used with Lang::get() to
86
     * get every value in the specified lang entry (which can either represent
87
     * a lang file or an array with a lang file).
88
     *
89
     * @param string $langEntry
90
     * @return string[]
91
     */
92
    protected function getAllLangEntriesInFile($langEntry)
93
    {
94
        $values = Lang::get($langEntry);
95
        if (is_string($values)) {
96
            return [$langEntry];
97
        } elseif (is_array($values)) {
98
            $entries = [];
99
            foreach ($values as $key => $value) {
100
                $path = $langEntry . "." . $key;
101
                $entries = array_merge($entries, $this->getAllLangEntriesInFile($path));
102
            }
103
            return $entries;
104
        } else {
105
            return [];
106
        }
107
    }
108
109
    /**
110
     * Return the array of strings that can be used with Lang::get() to
111
     * get all lang file entries, in all languages.
112
     *
113
     * @return string[]
114
     */
115
    protected function getAllLangPaths(): array
116
    {
117
        $paths = [];
118
        foreach ($this->locales as $locale) {
119
            App::setLocale($locale);
120
            $langFiles = $this->getLangFilenames($locale);
121
122
            foreach ($langFiles as $langFile) {
123
                $newPaths = $this->getAllLangEntriesInFile($langFile);
124
                $paths = array_merge($paths, $newPaths);
125
            }
126
        }
127
        return array_unique($paths);
128
    }
129
}
130