Completed
Push — translationunittests ( 2f16a4...13fec9 )
by Tristan
17:41
created

BaseTranslationTest::formatLangFilenames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Tests\Unit;
4
5
use Tests\TestCase;
6
7
abstract class BaseTranslationTest extends TestCase
8
{
9
    protected $rootLangDir = 'resources/lang';
10
11
    /**
12
     * Return an array of all Lang files in directory tree
13
     * @param  string $dir directory to start searching in
14
     */
15
    protected function getAllFilesInDirectoryTree($dir) {
16
        $dir = trim($dir, '/');
17
        $filesAndDirectories = scandir($dir);
18
        $files = [];
19
        foreach($filesAndDirectories as $fileName) {
20
            $path = implode('/', [$dir, $fileName]);
21
            if ($fileName === '.' || $fileName === '..') {
22
                //Do nothing
23
            } elseif (is_dir($path)) {
24
                $files = array_merge($files, $this->getAllFilesInDirectoryTree($path));
25
            } else {
26
                array_push($files, $path);
27
            }
28
        }
29
        return $files;
30
    }
31
32
    protected function removePrefix($str, $prefix) {
33
        if (substr($str, 0, strlen($prefix)) == $prefix) {
34
            $str = substr($str, strlen($prefix));
35
        }
36
        return $str;
37
    }
38
39
    protected function removeFiletype($filename) {
40
        return preg_replace('/\\.[^.\\s]+$/', '', $filename);
41
    }
42
43
    protected function formatLangFilenames($langFiles, $prefix) {
44
        $output = [];
45
        foreach($langFiles as $file) {
46
            $relativeName = $this->removePrefix($file, $prefix);
47
            $relativeName = $this->removeFiletype($relativeName);
48
            $relativeName = trim($relativeName, '/');
49
            array_push($output, $relativeName);
50
        }
51
        return $output;
52
    }
53
54
    protected function getLangFilenames($lang) {
55
        $dir = implode('/', [$this->rootLangDir, $lang]);
56
        $langFiles = $this->getAllFilesInDirectoryTree($dir);
57
        $formatedLangFiles = $this->formatLangFilenames($langFiles, $dir);
58
        return $formatedLangFiles;
59
    }
60
61
    protected function getAllLangFilenames() {
62
        $langs = ['en', 'fr'];
63
        $output = [];
64
        foreach($langs as $lang) {
65
            $output = array_merge($output, $this->getLangFilenames($lang));
66
        }
67
        return $output;
68
    }
69
}
70
// instead of checking if specific files exists, check if every file in the lang folder returns an array. Do it for french and english at ghe same time no seperate test needed.
71
//
72
//  Prefix: Write a function, make it a function that returns an array of everything that you could use to pass into Lang::get. (return auth, manager/applicant, everything under resources/lang/en. For now.)
73
 // Loop through that array and make sure everything is passable into Lang::get and use that for other tests.
74
75
/*---
76
 *
77
 * 1) Use Scandir to get everything in the folder and loop through them. (same function as step 7) (its a recursive function, if its a file, turn that into a string that we can pass into Lang::get, if its a folder, we need to call this function again using that folder as a location.
78
 * 2) If its a file, you need to use the name of the file, and pass it to lang::get.
79
 * 3) and then use Assert InternalType array. The Assert is to check what comes back from Lang::get. Its possible Lang::get causes an exception, but will show up in tests (probably)
80
 * 4) Resources/Lang/EN or FR is where you start looking.
81
 * 5) Function needs to take a second argument, what am I going to prefix the filename with(arugment 1 is the directory you are looking in), for the Lang::get funcction, start with empty string. (argument 2 is an empty string. ''),
82
 * 6) Check Applicant Profile Controller line 43 for a working example of Lang::get.
83
 *
84
 * ---
85
 *
86
 * Put the test in one function, but have the function contain two starting points. One for EN/ one for FR.
87
 *
88
 */
89