Skip to content

Commit 882d04c

Browse files
authored
Merge pull request #80 from RonasIT/49-add-translator-generator-tests
Add translation generator tests
2 parents d86f50c + 3252fcb commit 882d04c

File tree

9 files changed

+285
-11
lines changed

9 files changed

+285
-11
lines changed

src/Generators/TranslationsGenerator.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public function __construct()
1313
{
1414
parent::__construct();
1515

16-
$this->translationPath = Arr::get($this->paths, 'translations', 'resources/lang/en/validation.php');
16+
$this->translationPath = base_path(Arr::get($this->paths, 'translations', 'resources/lang/en/validation.php'));
1717
}
1818

1919
public function generate(): void
2020
{
2121
if (!file_exists($this->translationPath) && $this->isStubExists('validation')) {
2222
$this->createTranslate();
2323
}
24-
25-
if ($this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) {
24+
25+
if (file_exists($this->translationPath) && $this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) {
2626
$this->appendNotFoundException();
2727
}
2828
}
@@ -41,20 +41,20 @@ protected function createTranslate(): void
4141
file_put_contents($this->translationPath, $content);
4242

4343
$createMessage = "Created a new Translations dump on path: {$this->translationPath}";
44-
44+
4545
event(new SuccessCreateMessage($createMessage));
4646
}
4747

4848
protected function appendNotFoundException(): void
4949
{
5050
$content = file_get_contents($this->translationPath);
51-
51+
5252
$stubPath = config('entity-generator.stubs.translation_not_found');
53-
53+
5454
$stubContent = view($stubPath)->render();
5555

56-
$fixedContent = preg_replace('/\]\;\s*$/', "\n\t{$stubContent}", $content);
57-
56+
$fixedContent = preg_replace('/\]\;\s*$/', "\n {$stubContent}", $content);
57+
5858
file_put_contents($this->translationPath, $fixedContent);
5959
}
60-
}
60+
}

stubs/translation_not_found.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
'exceptions' => [
1111
'not_found' => ':Entity does not exist',
12-
]
12+
],
1313

1414
];

stubs/validation.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@
127127

128128
'exceptions' => [
129129
'not_found' => ':Entity does not exist',
130-
]
130+
],
131131

132132
];

tests/Support/FileSystemMock.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FileSystemMock
1717
public ?array $testClasses = null;
1818
public ?array $routes = null;
1919
public ?array $factories = null;
20+
public ?array $translations = null;
2021

2122
public function setStructure(): void
2223
{
@@ -106,6 +107,14 @@ public function setStructure(): void
106107
}
107108
}
108109

110+
if (!is_null($this->translations)) {
111+
$structure['resources']['lang']['en'] = [];
112+
113+
foreach ($this->translations as $translation => $content) {
114+
$structure['resources']['lang']['en'][$translation] = $content;
115+
}
116+
}
117+
109118
vfsStream::create($structure);
110119
}
111120
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Translation;
4+
5+
use RonasIT\Support\Tests\Support\FileSystemMock;
6+
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
7+
8+
trait TranslationMockTrait
9+
{
10+
use GeneratorMockTrait;
11+
12+
public function mockFilesystem(): void
13+
{
14+
$fileSystemMock = new FileSystemMock();
15+
16+
$fileSystemMock->translations = [];
17+
18+
$fileSystemMock->setStructure();
19+
}
20+
21+
public function mockFilesystemForAppend(): void
22+
{
23+
$validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php');
24+
25+
$fileSystemMock = new FileSystemMock();
26+
27+
$fileSystemMock->translations = ['validation.php' => $validation];
28+
29+
$fileSystemMock->setStructure();
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return [];

tests/TranslationGeneratorTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use Illuminate\Support\Facades\Lang;
7+
use RonasIT\Support\Events\SuccessCreateMessage;
8+
use RonasIT\Support\Events\WarningEvent;
9+
use RonasIT\Support\Generators\TranslationsGenerator;
10+
use RonasIT\Support\Tests\Support\Translation\TranslationMockTrait;
11+
12+
class TranslationGeneratorTest extends TestCase
13+
{
14+
use TranslationMockTrait;
15+
16+
public function testCreate()
17+
{
18+
Lang::shouldReceive('get')->andReturn([]);
19+
20+
$this->mockFilesystem();
21+
22+
app(TranslationsGenerator::class)
23+
->setModel('Post')
24+
->generate();
25+
26+
$this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php');
27+
28+
$this->assertEventPushed(
29+
className: SuccessCreateMessage::class,
30+
message: 'Created a new Translations dump on path: vfs://root/resources/lang/en/validation.php',
31+
);
32+
}
33+
34+
public function testCreateStubNotExist()
35+
{
36+
config(['entity-generator.stubs.validation' => 'incorrect_stub']);
37+
38+
app(TranslationsGenerator::class)
39+
->setModel('Post')
40+
->generate();
41+
42+
$this->assertFileDoesNotExist('resources/lang/en/validation.php');
43+
44+
$this->assertEventPushed(
45+
className: WarningEvent::class,
46+
message: 'Generation of validation has been skipped cause the view incorrect_stub from the config entity-generator.stubs.validation is not exists. Please check that config has the correct view name value.',
47+
);
48+
}
49+
50+
public function testAppendNotFoundException()
51+
{
52+
$this->mockFilesystemForAppend();
53+
54+
app(TranslationsGenerator::class)
55+
->setModel('Post')
56+
->generate();
57+
58+
$this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'resources/lang/en/validation.php');
59+
60+
Event::assertNothingDispatched();
61+
}
62+
63+
public function testAppendNotFoundExceptionStubNotExist()
64+
{
65+
config(['entity-generator.stubs.translation_not_found' => 'incorrect_stub']);
66+
67+
$this->mockFilesystemForAppend();
68+
69+
app(TranslationsGenerator::class)
70+
->setModel('Post')
71+
->generate();
72+
73+
$this->assertFileDoesNotExist('validation.php', 'resources/lang/en/validation.php');
74+
75+
$this->assertEventPushed(
76+
className: WarningEvent::class,
77+
message: 'Generation of translation not found has been skipped cause the view incorrect_stub from the config entity-generator.stubs.translation_not_found is not exists. Please check that config has the correct view name value.',
78+
);
79+
}
80+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Validation Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines contain the default error messages used by
11+
| the validator class. Some of these rules have multiple versions such
12+
| as the size rules. Feel free to tweak each of these messages here.
13+
|
14+
*/
15+
16+
'accepted' => 'The :attribute must be accepted.',
17+
'active_url' => 'The :attribute is not a valid URL.',
18+
'after' => 'The :attribute must be a date after :date.',
19+
'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
20+
'alpha' => 'The :attribute may only contain letters.',
21+
'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.',
22+
'alpha_num' => 'The :attribute may only contain letters and numbers.',
23+
'array' => 'The :attribute must be an array.',
24+
'before' => 'The :attribute must be a date before :date.',
25+
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
26+
'between' => [
27+
'numeric' => 'The :attribute must be between :min and :max.',
28+
'file' => 'The :attribute must be between :min and :max kilobytes.',
29+
'string' => 'The :attribute must be between :min and :max characters.',
30+
'array' => 'The :attribute must have between :min and :max items.',
31+
],
32+
'boolean' => 'The :attribute field must be true or false.',
33+
'confirmed' => 'The :attribute confirmation does not match.',
34+
'date' => 'The :attribute is not a valid date.',
35+
'date_format' => 'The :attribute does not match the format :format.',
36+
'different' => 'The :attribute and :other must be different.',
37+
'digits' => 'The :attribute must be :digits digits.',
38+
'digits_between' => 'The :attribute must be between :min and :max digits.',
39+
'dimensions' => 'The :attribute has invalid image dimensions.',
40+
'distinct' => 'The :attribute field has a duplicate value.',
41+
'email' => 'The :attribute must be a valid email address.',
42+
'exists' => 'The selected :attribute is invalid.',
43+
'file' => 'The :attribute must be a file.',
44+
'filled' => 'The :attribute field must have a value.',
45+
'image' => 'The :attribute must be an image.',
46+
'in' => 'The selected :attribute is invalid.',
47+
'in_array' => 'The :attribute field does not exist in :other.',
48+
'integer' => 'The :attribute must be an integer.',
49+
'ip' => 'The :attribute must be a valid IP address.',
50+
'ipv4' => 'The :attribute must be a valid IPv4 address.',
51+
'ipv6' => 'The :attribute must be a valid IPv6 address.',
52+
'json' => 'The :attribute must be a valid JSON string.',
53+
'max' => [
54+
'numeric' => 'The :attribute may not be greater than :max.',
55+
'file' => 'The :attribute may not be greater than :max kilobytes.',
56+
'string' => 'The :attribute may not be greater than :max characters.',
57+
'array' => 'The :attribute may not have more than :max items.',
58+
],
59+
'mimes' => 'The :attribute must be a file of type: :values.',
60+
'mimetypes' => 'The :attribute must be a file of type: :values.',
61+
'min' => [
62+
'numeric' => 'The :attribute must be at least :min.',
63+
'file' => 'The :attribute must be at least :min kilobytes.',
64+
'string' => 'The :attribute must be at least :min characters.',
65+
'array' => 'The :attribute must have at least :min items.',
66+
],
67+
'not_in' => 'The selected :attribute is invalid.',
68+
'numeric' => 'The :attribute must be a number.',
69+
'present' => 'The :attribute field must be present.',
70+
'regex' => 'The :attribute format is invalid.',
71+
'required' => 'The :attribute field is required.',
72+
'required_if' => 'The :attribute field is required when :other is :value.',
73+
'required_unless' => 'The :attribute field is required unless :other is in :values.',
74+
'required_with' => 'The :attribute field is required when :values is present.',
75+
'required_with_all' => 'The :attribute field is required when :values is present.',
76+
'required_without' => 'The :attribute field is required when :values is not present.',
77+
'required_without_all' => 'The :attribute field is required when none of :values are present.',
78+
'same' => 'The :attribute and :other must match.',
79+
'size' => [
80+
'numeric' => 'The :attribute must be :size.',
81+
'file' => 'The :attribute must be :size kilobytes.',
82+
'string' => 'The :attribute must be :size characters.',
83+
'array' => 'The :attribute must contain :size items.',
84+
],
85+
'string' => 'The :attribute must be a string.',
86+
'timezone' => 'The :attribute must be a valid zone.',
87+
'unique' => 'The :attribute has already been taken.',
88+
'uploaded' => 'The :attribute failed to upload.',
89+
'url' => 'The :attribute format is invalid.',
90+
91+
/*
92+
|--------------------------------------------------------------------------
93+
| Custom Validation Language Lines
94+
|--------------------------------------------------------------------------
95+
|
96+
| Here you may specify custom validation messages for attributes using the
97+
| convention "attribute.rule" to name the lines. This makes it quick to
98+
| specify a specific custom language line for a given attribute rule.
99+
|
100+
*/
101+
102+
'custom' => [
103+
'attribute-name' => [
104+
'rule-name' => 'custom-message',
105+
],
106+
],
107+
108+
/*
109+
|--------------------------------------------------------------------------
110+
| Custom Validation Attributes
111+
|--------------------------------------------------------------------------
112+
|
113+
| The following language lines are used to swap attribute place-holders
114+
| with something more reader friendly such as E-Mail Address instead
115+
| of "email". This simply helps us make messages a little cleaner.
116+
|
117+
*/
118+
119+
'attributes' => [],
120+
121+
/*
122+
|--------------------------------------------------------------------------
123+
| Custom Validation Attributes
124+
|--------------------------------------------------------------------------
125+
|
126+
| The following language lines are used to swap http error messages.
127+
|
128+
*/
129+
130+
'exceptions' => [
131+
'not_found' => ':Entity does not exist',
132+
],
133+
134+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Custom Validation Attributes
7+
|--------------------------------------------------------------------------
8+
|
9+
| The following language lines are used to swap http error messages.
10+
|
11+
*/
12+
13+
'exceptions' => [
14+
'not_found' => ':Entity does not exist',
15+
],
16+
17+
];

0 commit comments

Comments
 (0)