Skip to content

Commit 16d840d

Browse files
committed
add a test for basic functionality
1 parent 8e20fda commit 16d840d

File tree

4 files changed

+181
-46
lines changed

4 files changed

+181
-46
lines changed

phpunit.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnError="false"
11+
stopOnFailure="false"
12+
syntaxCheck="true"
13+
verbose="true"
14+
>
15+
<testsuites>
16+
<testsuite name="Test Suite">
17+
<directory suffix="Test.php">./tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
</phpunit>

src/Commands/GenerateInclude.php

+4-46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php namespace MartinLindhe\VueInternationalizationGenerator\Commands;
22

3-
use DirectoryIterator;
4-
use Exception;
53
use Illuminate\Console\Command;
64

5+
use MartinLindhe\VueInternationalizationGenerator\Generator;
6+
77
class GenerateInclude extends Command
88
{
99
/**
@@ -20,63 +20,21 @@ class GenerateInclude extends Command
2020
*/
2121
protected $description = "Generates a vue-i18n compatible js array out of project translations";
2222

23-
2423
/**
2524
* Execute the console command.
2625
* @return mixed
27-
* @throws Exception
2826
*/
2927
public function handle()
3028
{
3129
$root = base_path() . '/resources/lang';
32-
if (!is_dir($root)) {
33-
throw new Exception('Path not found: '.$root);
34-
}
35-
36-
$projectLocales = [];
37-
$dir = new DirectoryIterator($root);
38-
foreach ($dir as $fileinfo) {
39-
if (!$fileinfo->isDot()
40-
&& $fileinfo->isDir()
41-
&& !in_array($fileinfo->getFilename(), ['vendor'])
42-
) {
43-
$projectLocales[$fileinfo->getFilename()] =
44-
$this->allocateLocaleArray($root . '/' . $fileinfo->getFilename());
45-
}
46-
}
4730

48-
$data = 'export default '
49-
. json_encode($projectLocales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
31+
$data = (new Generator)
32+
->generateFromPath($root);
5033

5134
$jsFile = base_path() . '/resources/assets/js/vue-i18n-locales.generated.js';
5235

5336
file_put_contents($jsFile, $data);
5437

5538
echo "Written to " . $jsFile . PHP_EOL;
5639
}
57-
58-
private function allocateLocaleArray($path)
59-
{
60-
$data = [];
61-
62-
$dir = new DirectoryIterator($path);
63-
foreach ($dir as $fileinfo) {
64-
if (!$fileinfo->isDot()) {
65-
$noExt = $this->removeExtension($fileinfo->getFilename());
66-
$data[$noExt] = include($path . '/' . $fileinfo->getFilename());
67-
}
68-
}
69-
70-
return $data;
71-
}
72-
73-
private function removeExtension($filename)
74-
{
75-
$pos = mb_strrpos($filename, '.');
76-
if ($pos === false) {
77-
return $filename;
78-
}
79-
80-
return mb_substr($filename, 0, $pos);
81-
}
8240
}

src/Generator.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php namespace MartinLindhe\VueInternationalizationGenerator;
2+
3+
use DirectoryIterator;
4+
use Exception;
5+
6+
class Generator
7+
{
8+
/**
9+
* @param string $path
10+
* @return string
11+
* @throws Exception
12+
*/
13+
public function generateFromPath($path)
14+
{
15+
if (!is_dir($path)) {
16+
throw new Exception('Directory not found: '.$path);
17+
}
18+
19+
$locales = [];
20+
$dir = new DirectoryIterator($path);
21+
foreach ($dir as $fileinfo) {
22+
if (!$fileinfo->isDot()
23+
&& $fileinfo->isDir()
24+
&& !in_array($fileinfo->getFilename(), ['vendor'])
25+
) {
26+
$locales[$fileinfo->getFilename()] =
27+
$this->allocateLocaleArray($path . '/' . $fileinfo->getFilename());
28+
}
29+
}
30+
31+
return 'export default '
32+
. json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
33+
}
34+
35+
/**
36+
* @param string $path
37+
* @return array
38+
*/
39+
private function allocateLocaleArray($path)
40+
{
41+
$data = [];
42+
43+
$dir = new DirectoryIterator($path);
44+
foreach ($dir as $fileinfo) {
45+
if (!$fileinfo->isDot()) {
46+
$noExt = $this->removeExtension($fileinfo->getFilename());
47+
$data[$noExt] = include($path . '/' . $fileinfo->getFilename());
48+
}
49+
}
50+
51+
return $data;
52+
}
53+
54+
/**
55+
* @param string $filename
56+
* @return string
57+
*/
58+
private function removeExtension($filename)
59+
{
60+
$pos = mb_strrpos($filename, '.');
61+
if ($pos === false) {
62+
return $filename;
63+
}
64+
65+
return mb_substr($filename, 0, $pos);
66+
}
67+
}

tests/GenerateTest.php

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
use MartinLindhe\VueInternationalizationGenerator\Generator;
4+
5+
class GenerateTest extends \PHPUnit_Framework_TestCase
6+
{
7+
private function generateLocaleFilesFrom(array $arr)
8+
{
9+
$root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand());
10+
11+
if (!is_dir($root)) {
12+
mkdir($root, 0777, true);
13+
}
14+
15+
foreach ($arr as $key => $val) {
16+
17+
if (!is_dir($root . '/' . $key)) {
18+
mkdir($root . '/' . $key);
19+
}
20+
21+
foreach ($val as $group => $content) {
22+
$outFile = $root . '/'. $key . '/' . $group . '.php';
23+
file_put_contents($outFile, '<?php return ' . var_export($content, true) . ';');
24+
}
25+
}
26+
27+
return $root;
28+
}
29+
30+
private function destroyLocaleFilesFrom(array $arr, $root)
31+
{
32+
foreach ($arr as $key => $val) {
33+
34+
foreach ($val as $group => $content) {
35+
$outFile = $root . '/'. $key . '/' . $group . '.php';
36+
if (file_exists($outFile)) {
37+
unlink($outFile);
38+
}
39+
}
40+
41+
if (is_dir($root . '/' . $key)) {
42+
rmdir($root . '/' . $key);
43+
}
44+
45+
}
46+
47+
if (is_dir($root)) {
48+
rmdir($root);
49+
}
50+
}
51+
52+
function testBasic()
53+
{
54+
$arr = [
55+
'en' => [
56+
'help' => [
57+
'yes' => 'yes',
58+
'no' => 'no',
59+
]
60+
],
61+
'sv' => [
62+
'help' => [
63+
'yes' => 'ja',
64+
'no' => 'nej',
65+
]
66+
]
67+
];
68+
69+
$root = $this->generateLocaleFilesFrom($arr);
70+
71+
$this->assertEquals(
72+
'export default {' . PHP_EOL
73+
. ' "en": {' . PHP_EOL
74+
. ' "help": {' . PHP_EOL
75+
. ' "yes": "yes",' . PHP_EOL
76+
. ' "no": "no"' . PHP_EOL
77+
. ' }' . PHP_EOL
78+
. ' },' . PHP_EOL
79+
. ' "sv": {' . PHP_EOL
80+
. ' "help": {' . PHP_EOL
81+
. ' "yes": "ja",' . PHP_EOL
82+
. ' "no": "nej"' . PHP_EOL
83+
. ' }' . PHP_EOL
84+
. ' }' . PHP_EOL
85+
. '}' . PHP_EOL,
86+
(new Generator)->generateFromPath($root));
87+
88+
$this->destroyLocaleFilesFrom($arr, $root);
89+
}
90+
}

0 commit comments

Comments
 (0)