Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"ronasit/laravel-helpers": ">=3.4",
"laravel/legacy-factories": ">=1.4.0",
"ext-json": "*",
"doctrine/dbal": "^4.2"
"doctrine/dbal": "^4.2",
"winter/laravel-config-writer": ">=1.2.1"
},
"require-dev": {
"fakerphp/faker": "^1.24.0",
Expand Down
67 changes: 64 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion config/entity-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
'migration' => 'entity-generator::migration',
'dump' => 'entity-generator::dumps.pgsql',
'test' => 'entity-generator::test',
'translation_not_found' => 'entity-generator::translation_not_found',
'validation' => 'entity-generator::validation',
'resource' => 'entity-generator::resource',
'collection_resource' => 'entity-generator::collection_resource',
Expand Down
31 changes: 16 additions & 15 deletions src/Generators/TranslationsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Arr;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use Winter\LaravelConfigWriter\ArrayFile;

class TranslationsGenerator extends EntityGenerator
{
Expand All @@ -18,18 +20,21 @@ public function __construct()

public function generate(): void
{
if (!file_exists($this->translationPath) && $this->isStubExists('validation')) {
$isTranslationFileExists = file_exists($this->translationPath);

if (!$isTranslationFileExists && $this->isStubExists('validation')) {
$this->createTranslate();

return;
}

if (file_exists($this->translationPath) && $this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) {
$this->appendNotFoundException();
if ($isTranslationFileExists) {
$this->setTranslationFileValue('exceptions.not_found', ':Entity does not exist');

return;
}
}

protected function isTranslationMissed($translation) : bool
{
return __($translation) === 'validation.exceptions.not_found';
event(new WarningEvent("{$this->translationPath} file and its stub missing. Create the file or check the entity-generator.stubs.validation config"));
}

protected function createTranslate(): void
Expand All @@ -45,16 +50,12 @@ protected function createTranslate(): void
event(new SuccessCreateMessage($createMessage));
}

protected function appendNotFoundException(): void
protected function setTranslationFileValue(string $key, string $value): void
{
$content = file_get_contents($this->translationPath);

$stubPath = config('entity-generator.stubs.translation_not_found');

$stubContent = view($stubPath)->render();
$config = ArrayFile::open($this->translationPath);

$fixedContent = preg_replace('/\]\;\s*$/', "\n {$stubContent}", $content);
$config->set($key, $value);

file_put_contents($this->translationPath, $fixedContent);
$config->write();
}
}
4 changes: 2 additions & 2 deletions tests/Support/Translation/TranslationMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function mockFilesystem(): void
$fileSystemMock->setStructure();
}

public function mockFilesystemForAppend(): void
public function mockFilesystemForAppend(string $validationStub): void
{
$validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php');
$validation = file_get_contents(getcwd() . "/tests/Support/Translation/{$validationStub}.php");

$fileSystemMock = new FileSystemMock();

Expand Down
16 changes: 16 additions & 0 deletions tests/Support/Translation/validation_with_exceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap http error messages.
|
*/

'exceptions' => [
'not_owner' => ':Entity is not owner',
],
];
22 changes: 11 additions & 11 deletions tests/TranslationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,36 @@ public function testCreateStubNotExist()
className: WarningEvent::class,
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.',
);

$this->assertEventPushed(
className: WarningEvent::class,
message: 'vfs://root/lang/en/validation.php file and its stub missing. Create the file or check the entity-generator.stubs.validation config',
);
}

public function testAppendNotFoundException()
{
$this->mockFilesystemForAppend();
$this->mockFilesystemForAppend('validation_without_exceptions');

app(TranslationsGenerator::class)
->setModel('Post')
->generate();

$this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'lang/en/validation.php');

Event::assertNothingDispatched();
Event::assertNotDispatched(WarningEvent::class);
}

public function testAppendNotFoundExceptionStubNotExist()
public function testAppendValidationExceptionsExist()
{
config(['entity-generator.stubs.translation_not_found' => 'incorrect_stub']);

$this->mockFilesystemForAppend();
$this->mockFilesystemForAppend('validation_with_exceptions');

app(TranslationsGenerator::class)
->setModel('Post')
->generate();

$this->assertFileDoesNotExist('validation.php', 'resources/lang/en/validation.php');
$this->assertGeneratedFileEquals('validation_append_not_found_with_exceptions.php', 'lang/en/validation.php');

$this->assertEventPushed(
className: WarningEvent::class,
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.',
);
Event::assertNotDispatched(WarningEvent::class);
}
}
1 change: 0 additions & 1 deletion tests/fixtures/CommandTest/changed_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"migration": "entity-generator::migration",
"dump": "entity-generator::dumps.pgsql",
"test": "entity-generator::test",
"translation_not_found": "entity-generator::translation_not_found",
"validation": "entity-generator::validation",
"resource": "entity-generator::resource",
"collection_resource": "entity-generator::collection_resource",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
<?php
<?php

return [
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap http error messages.
|
*/

'exceptions' => [
'not_found' => ':Entity does not exist',
],

];
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
Expand All @@ -8,7 +11,7 @@
*/

'exceptions' => [
'not_owner' => ':Entity is not owner',
'not_found' => ':Entity does not exist',
],

];