From b61599f296732727a3a3f84b34f129ba137da1cf Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Fri, 19 Jan 2024 20:23:19 +0400 Subject: [PATCH 1/8] test: add tests for translation file generation generator. --- src/Generators/TranslationsGenerator.php | 6 +- stubs/validation.blade.php | 2 +- .../Translation/TranslationMockTrait.php | 35 +++++ tests/TranslationGeneratorTest.php | 26 ++++ .../TranslationGeneratorTest/translations.php | 134 ++++++++++++++++++ 5 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 tests/Support/Translation/TranslationMockTrait.php create mode 100644 tests/TranslationGeneratorTest.php create mode 100644 tests/fixtures/TranslationGeneratorTest/translations.php diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index 30b54a38..adc324af 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -13,7 +13,7 @@ public function __construct() { parent::__construct(); - $this->translationPath = Arr::get($this->paths, 'translations', 'resources/lang/en/validation.php'); + $this->translationPath = base_path(Arr::get($this->paths, 'translations', 'resources/lang/en/validation.php')); } public function generate(): void @@ -29,7 +29,7 @@ public function generate(): void protected function isTranslationMissed($translation) : bool { - return __($translation) === 'validation.exceptions.not_found'; + return __($translation) !== 'validation.exceptions.not_found'; } protected function createTranslate(): void @@ -57,4 +57,4 @@ protected function appendNotFoundException(): void file_put_contents($this->translationPath, $fixedContent); } -} \ No newline at end of file +} diff --git a/stubs/validation.blade.php b/stubs/validation.blade.php index 7ada09a4..c6d83ebc 100644 --- a/stubs/validation.blade.php +++ b/stubs/validation.blade.php @@ -127,6 +127,6 @@ 'exceptions' => [ 'not_found' => ':Entity does not exist', - ] + ], ]; diff --git a/tests/Support/Translation/TranslationMockTrait.php b/tests/Support/Translation/TranslationMockTrait.php new file mode 100644 index 00000000..1049b510 --- /dev/null +++ b/tests/Support/Translation/TranslationMockTrait.php @@ -0,0 +1,35 @@ + 'entity-generator::validation', + 'entity-generator.stubs.translation_not_found' => 'entity-generator::translation_not_found', + 'entity-generator.paths' => [ + 'translations' => 'resources/lang/en/validation.php', + ] + ]); + } + + public function mockFilesystem(): void + { + $structure = [ + 'resources' => [ + 'lang' => [ + 'en' => [] + ] + ] + ]; + + vfsStream::create($structure); + } +} diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php new file mode 100644 index 00000000..af00512f --- /dev/null +++ b/tests/TranslationGeneratorTest.php @@ -0,0 +1,26 @@ +mockViewsNamespace(); + $this->mockConfigurations(); + $this->mockFilesystem(); + + app(TranslationsGenerator::class) + ->setModel('Post') + ->generate(); + + $this->rollbackToDefaultBasePath(); + + $this->assertGeneratedFileEquals('translations.php', 'resources/lang/en/validation.php'); + } +} diff --git a/tests/fixtures/TranslationGeneratorTest/translations.php b/tests/fixtures/TranslationGeneratorTest/translations.php new file mode 100644 index 00000000..c453c6ba --- /dev/null +++ b/tests/fixtures/TranslationGeneratorTest/translations.php @@ -0,0 +1,134 @@ + 'The :attribute must be accepted.', + 'active_url' => 'The :attribute is not a valid URL.', + 'after' => 'The :attribute must be a date after :date.', + 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', + 'alpha' => 'The :attribute may only contain letters.', + 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', + 'alpha_num' => 'The :attribute may only contain letters and numbers.', + 'array' => 'The :attribute must be an array.', + 'before' => 'The :attribute must be a date before :date.', + 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', + 'between' => [ + 'numeric' => 'The :attribute must be between :min and :max.', + 'file' => 'The :attribute must be between :min and :max kilobytes.', + 'string' => 'The :attribute must be between :min and :max characters.', + 'array' => 'The :attribute must have between :min and :max items.', + ], + 'boolean' => 'The :attribute field must be true or false.', + 'confirmed' => 'The :attribute confirmation does not match.', + 'date' => 'The :attribute is not a valid date.', + 'date_format' => 'The :attribute does not match the format :format.', + 'different' => 'The :attribute and :other must be different.', + 'digits' => 'The :attribute must be :digits digits.', + 'digits_between' => 'The :attribute must be between :min and :max digits.', + 'dimensions' => 'The :attribute has invalid image dimensions.', + 'distinct' => 'The :attribute field has a duplicate value.', + 'email' => 'The :attribute must be a valid email address.', + 'exists' => 'The selected :attribute is invalid.', + 'file' => 'The :attribute must be a file.', + 'filled' => 'The :attribute field must have a value.', + 'image' => 'The :attribute must be an image.', + 'in' => 'The selected :attribute is invalid.', + 'in_array' => 'The :attribute field does not exist in :other.', + 'integer' => 'The :attribute must be an integer.', + 'ip' => 'The :attribute must be a valid IP address.', + 'ipv4' => 'The :attribute must be a valid IPv4 address.', + 'ipv6' => 'The :attribute must be a valid IPv6 address.', + 'json' => 'The :attribute must be a valid JSON string.', + 'max' => [ + 'numeric' => 'The :attribute may not be greater than :max.', + 'file' => 'The :attribute may not be greater than :max kilobytes.', + 'string' => 'The :attribute may not be greater than :max characters.', + 'array' => 'The :attribute may not have more than :max items.', + ], + 'mimes' => 'The :attribute must be a file of type: :values.', + 'mimetypes' => 'The :attribute must be a file of type: :values.', + 'min' => [ + 'numeric' => 'The :attribute must be at least :min.', + 'file' => 'The :attribute must be at least :min kilobytes.', + 'string' => 'The :attribute must be at least :min characters.', + 'array' => 'The :attribute must have at least :min items.', + ], + 'not_in' => 'The selected :attribute is invalid.', + 'numeric' => 'The :attribute must be a number.', + 'present' => 'The :attribute field must be present.', + 'regex' => 'The :attribute format is invalid.', + 'required' => 'The :attribute field is required.', + 'required_if' => 'The :attribute field is required when :other is :value.', + 'required_unless' => 'The :attribute field is required unless :other is in :values.', + 'required_with' => 'The :attribute field is required when :values is present.', + 'required_with_all' => 'The :attribute field is required when :values is present.', + 'required_without' => 'The :attribute field is required when :values is not present.', + 'required_without_all' => 'The :attribute field is required when none of :values are present.', + 'same' => 'The :attribute and :other must match.', + 'size' => [ + 'numeric' => 'The :attribute must be :size.', + 'file' => 'The :attribute must be :size kilobytes.', + 'string' => 'The :attribute must be :size characters.', + 'array' => 'The :attribute must contain :size items.', + ], + 'string' => 'The :attribute must be a string.', + 'timezone' => 'The :attribute must be a valid zone.', + 'unique' => 'The :attribute has already been taken.', + 'uploaded' => 'The :attribute failed to upload.', + 'url' => 'The :attribute format is invalid.', + + /* + |-------------------------------------------------------------------------- + | Custom Validation Language Lines + |-------------------------------------------------------------------------- + | + | Here you may specify custom validation messages for attributes using the + | convention "attribute.rule" to name the lines. This makes it quick to + | specify a specific custom language line for a given attribute rule. + | + */ + + 'custom' => [ + 'attribute-name' => [ + 'rule-name' => 'custom-message', + ], + ], + + /* + |-------------------------------------------------------------------------- + | Custom Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap attribute place-holders + | with something more reader friendly such as E-Mail Address instead + | of "email". This simply helps us make messages a little cleaner. + | + */ + + 'attributes' => [], + + /* + |-------------------------------------------------------------------------- + | Custom Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap http error messages. + | + */ + + 'exceptions' => [ + 'not_found' => ':Entity does not exist', + ], + +]; From 3ab39e9d398e611a8b12b6e819375e93bc9af9bc Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Fri, 19 Jan 2024 21:04:35 +0400 Subject: [PATCH 2/8] chore: remove fixture export. --- tests/TestGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestGeneratorTest.php b/tests/TestGeneratorTest.php index 16a3f647..18bf3fee 100644 --- a/tests/TestGeneratorTest.php +++ b/tests/TestGeneratorTest.php @@ -42,7 +42,7 @@ public function testCreateTests() $this->rollbackToDefaultBasePath(); - $this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/PostTest/dump.sql', true); + $this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/PostTest/dump.sql'); $this->assertGeneratedFileEquals('create_post_request.json', 'tests/fixtures/PostTest/create_post_request.json'); $this->assertGeneratedFileEquals('create_post_response.json', 'tests/fixtures/PostTest/create_post_response.json'); $this->assertGeneratedFileEquals('update_post_request.json', 'tests/fixtures/PostTest/update_post_request.json'); From edf5296f90955f478aae8e66e3cb0324d50387a4 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 13 Jan 2025 18:21:17 +0600 Subject: [PATCH 3/8] feat: add tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/TranslationsGenerator.php | 14 +- stubs/translation_not_found.blade.php | 2 +- stubs/validation.blade.php | 2 +- .../Translation/TranslationMockTrait.php | 35 ++-- .../validation_without_exceptions.php | 120 +++++++++++++ tests/TranslationGeneratorTest.php | 68 +++++++- .../TranslationGeneratorTest/validation.json | 158 ++++++++++++++++++ .../{translations.php => validation.php} | 2 +- 8 files changed, 372 insertions(+), 29 deletions(-) create mode 100644 tests/Support/Translation/validation_without_exceptions.php create mode 100644 tests/fixtures/TranslationGeneratorTest/validation.json rename tests/fixtures/TranslationGeneratorTest/{translations.php => validation.php} (99%) diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index 751baa4a..383083aa 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -21,15 +21,15 @@ public function generate(): void if (!file_exists($this->translationPath) && $this->isStubExists('validation')) { $this->createTranslate(); } - - if ($this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) { + + if (file_exists($this->translationPath) && $this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) { $this->appendNotFoundException(); } } protected function isTranslationMissed($translation) : bool { - return __($translation) !== 'validation.exceptions.not_found'; + return __($translation) === 'validation.exceptions.not_found'; } protected function createTranslate(): void @@ -41,20 +41,20 @@ protected function createTranslate(): void file_put_contents($this->translationPath, $content); $createMessage = "Created a new Translations dump on path: {$this->translationPath}"; - + event(new SuccessCreateMessage($createMessage)); } protected function appendNotFoundException(): void { $content = file_get_contents($this->translationPath); - + $stubPath = config('entity-generator.stubs.translation_not_found'); - + $stubContent = view($stubPath)->render(); $fixedContent = preg_replace('/\]\;\s*$/', "\n\t{$stubContent}", $content); - + file_put_contents($this->translationPath, $fixedContent); } } diff --git a/stubs/translation_not_found.blade.php b/stubs/translation_not_found.blade.php index bac7b934..e0fb43d8 100644 --- a/stubs/translation_not_found.blade.php +++ b/stubs/translation_not_found.blade.php @@ -9,6 +9,6 @@ 'exceptions' => [ 'not_found' => ':Entity does not exist', - ] + ], ]; diff --git a/stubs/validation.blade.php b/stubs/validation.blade.php index c6d83ebc..8eb54522 100644 --- a/stubs/validation.blade.php +++ b/stubs/validation.blade.php @@ -116,7 +116,7 @@ 'attributes' => [], - /* + /* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- diff --git a/tests/Support/Translation/TranslationMockTrait.php b/tests/Support/Translation/TranslationMockTrait.php index 1049b510..ace5ed79 100644 --- a/tests/Support/Translation/TranslationMockTrait.php +++ b/tests/Support/Translation/TranslationMockTrait.php @@ -3,31 +3,38 @@ namespace RonasIT\Support\Tests\Support\Translation; use org\bovigo\vfs\vfsStream; -use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; +use RonasIT\Support\Tests\Support\GeneratorMockTrait; +use RonasIT\Support\Traits\MockTrait; trait TranslationMockTrait { - use GeneratorMockTrait; + use GeneratorMockTrait, MockTrait; - public function mockConfigurations(): void + public function mockFilesystem(): void { - config([ - 'entity-generator.stubs.validation' => 'entity-generator::validation', - 'entity-generator.stubs.translation_not_found' => 'entity-generator::translation_not_found', - 'entity-generator.paths' => [ - 'translations' => 'resources/lang/en/validation.php', - ] - ]); + $structure = [ + 'resources' => [ + 'lang' => [ + 'en' => [], + ], + ], + ]; + + vfsStream::create($structure); } - public function mockFilesystem(): void + public function mockFilesystemForAppend(): void { + $validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php'); + $structure = [ 'resources' => [ 'lang' => [ - 'en' => [] - ] - ] + 'en' => [ + 'validation.php' => $validation, + ], + ], + ], ]; vfsStream::create($structure); diff --git a/tests/Support/Translation/validation_without_exceptions.php b/tests/Support/Translation/validation_without_exceptions.php new file mode 100644 index 00000000..6f90f6cd --- /dev/null +++ b/tests/Support/Translation/validation_without_exceptions.php @@ -0,0 +1,120 @@ + 'The :attribute must be accepted.', + 'active_url' => 'The :attribute is not a valid URL.', + 'after' => 'The :attribute must be a date after :date.', + 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', + 'alpha' => 'The :attribute may only contain letters.', + 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', + 'alpha_num' => 'The :attribute may only contain letters and numbers.', + 'array' => 'The :attribute must be an array.', + 'before' => 'The :attribute must be a date before :date.', + 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', + 'between' => [ + 'numeric' => 'The :attribute must be between :min and :max.', + 'file' => 'The :attribute must be between :min and :max kilobytes.', + 'string' => 'The :attribute must be between :min and :max characters.', + 'array' => 'The :attribute must have between :min and :max items.', + ], + 'boolean' => 'The :attribute field must be true or false.', + 'confirmed' => 'The :attribute confirmation does not match.', + 'date' => 'The :attribute is not a valid date.', + 'date_format' => 'The :attribute does not match the format :format.', + 'different' => 'The :attribute and :other must be different.', + 'digits' => 'The :attribute must be :digits digits.', + 'digits_between' => 'The :attribute must be between :min and :max digits.', + 'dimensions' => 'The :attribute has invalid image dimensions.', + 'distinct' => 'The :attribute field has a duplicate value.', + 'email' => 'The :attribute must be a valid email address.', + 'exists' => 'The selected :attribute is invalid.', + 'file' => 'The :attribute must be a file.', + 'filled' => 'The :attribute field must have a value.', + 'image' => 'The :attribute must be an image.', + 'in' => 'The selected :attribute is invalid.', + 'in_array' => 'The :attribute field does not exist in :other.', + 'integer' => 'The :attribute must be an integer.', + 'ip' => 'The :attribute must be a valid IP address.', + 'ipv4' => 'The :attribute must be a valid IPv4 address.', + 'ipv6' => 'The :attribute must be a valid IPv6 address.', + 'json' => 'The :attribute must be a valid JSON string.', + 'max' => [ + 'numeric' => 'The :attribute may not be greater than :max.', + 'file' => 'The :attribute may not be greater than :max kilobytes.', + 'string' => 'The :attribute may not be greater than :max characters.', + 'array' => 'The :attribute may not have more than :max items.', + ], + 'mimes' => 'The :attribute must be a file of type: :values.', + 'mimetypes' => 'The :attribute must be a file of type: :values.', + 'min' => [ + 'numeric' => 'The :attribute must be at least :min.', + 'file' => 'The :attribute must be at least :min kilobytes.', + 'string' => 'The :attribute must be at least :min characters.', + 'array' => 'The :attribute must have at least :min items.', + ], + 'not_in' => 'The selected :attribute is invalid.', + 'numeric' => 'The :attribute must be a number.', + 'present' => 'The :attribute field must be present.', + 'regex' => 'The :attribute format is invalid.', + 'required' => 'The :attribute field is required.', + 'required_if' => 'The :attribute field is required when :other is :value.', + 'required_unless' => 'The :attribute field is required unless :other is in :values.', + 'required_with' => 'The :attribute field is required when :values is present.', + 'required_with_all' => 'The :attribute field is required when :values is present.', + 'required_without' => 'The :attribute field is required when :values is not present.', + 'required_without_all' => 'The :attribute field is required when none of :values are present.', + 'same' => 'The :attribute and :other must match.', + 'size' => [ + 'numeric' => 'The :attribute must be :size.', + 'file' => 'The :attribute must be :size kilobytes.', + 'string' => 'The :attribute must be :size characters.', + 'array' => 'The :attribute must contain :size items.', + ], + 'string' => 'The :attribute must be a string.', + 'timezone' => 'The :attribute must be a valid zone.', + 'unique' => 'The :attribute has already been taken.', + 'uploaded' => 'The :attribute failed to upload.', + 'url' => 'The :attribute format is invalid.', + + /* + |-------------------------------------------------------------------------- + | Custom Validation Language Lines + |-------------------------------------------------------------------------- + | + | Here you may specify custom validation messages for attributes using the + | convention "attribute.rule" to name the lines. This makes it quick to + | specify a specific custom language line for a given attribute rule. + | + */ + + 'custom' => [ + 'attribute-name' => [ + 'rule-name' => 'custom-message', + ], + ], + + /* + |-------------------------------------------------------------------------- + | Custom Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap attribute place-holders + | with something more reader friendly such as E-Mail Address instead + | of "email". This simply helps us make messages a little cleaner. + | + */ + + 'attributes' => [], +]; diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index af00512f..2ea8f083 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -2,6 +2,10 @@ namespace RonasIT\Support\Tests; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Lang; +use RonasIT\Support\Events\SuccessCreateMessage; +use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Generators\TranslationsGenerator; use RonasIT\Support\Tests\Support\Translation\TranslationMockTrait; @@ -9,18 +13,72 @@ class TranslationGeneratorTest extends TestCase { use TranslationMockTrait; - public function testCreateWithTrait() + public function testCreate() { - $this->mockViewsNamespace(); - $this->mockConfigurations(); + $translations = $this->getJsonFixture('validation.json'); + + Lang::shouldReceive('get')->andReturn($translations); + + $this->mockFilesystem(); + + app(TranslationsGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Translations dump on path: vfs://root/resources/lang/en/validation.php', + ); + } + + public function testCreateStubNotExist() + { + config(['entity-generator.stubs.validation' => 'incorrect_stub']); + $this->mockFilesystem(); app(TranslationsGenerator::class) ->setModel('Post') ->generate(); - $this->rollbackToDefaultBasePath(); + $this->assertFileDoesNotExist('resources/lang/en/validation.php'); + + $this->assertEventPushed( + 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.', + ); + } + + public function testAppendNotFoundException() + { + $this->mockFilesystemForAppend(); + + app(TranslationsGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); + + Event::assertNothingDispatched(); + } + + public function testAppendNotFoundExceptionStubNotExist() + { + config(['entity-generator.stubs.translation_not_found' => 'incorrect_stub']); + + $this->mockFilesystemForAppend(); + + app(TranslationsGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertFileDoesNotExist('validation.php', 'resources/lang/en/validation.php'); - $this->assertGeneratedFileEquals('translations.php', 'resources/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.', + ); } } diff --git a/tests/fixtures/TranslationGeneratorTest/validation.json b/tests/fixtures/TranslationGeneratorTest/validation.json new file mode 100644 index 00000000..0e1eb78d --- /dev/null +++ b/tests/fixtures/TranslationGeneratorTest/validation.json @@ -0,0 +1,158 @@ +{ + "accepted": "The :attribute field must be accepted.", + "accepted_if": "The :attribute field must be accepted when :other is :value.", + "active_url": "The :attribute field must be a valid URL.", + "after": "The :attribute field must be a date after :date.", + "after_or_equal": "The :attribute field must be a date after or equal to :date.", + "alpha": "The :attribute field must only contain letters.", + "alpha_dash": "The :attribute field must only contain letters, numbers, dashes, and underscores.", + "alpha_num": "The :attribute field must only contain letters and numbers.", + "array": "The :attribute field must be an array.", + "ascii": "The :attribute field must only contain single-byte alphanumeric characters and symbols.", + "before": "The :attribute field must be a date before :date.", + "before_or_equal": "The :attribute field must be a date before or equal to :date.", + "between": { + "array": "The :attribute field must have between :min and :max items.", + "file": "The :attribute field must be between :min and :max kilobytes.", + "numeric": "The :attribute field must be between :min and :max.", + "string": "The :attribute field must be between :min and :max characters." + }, + "boolean": "The :attribute field must be true or false.", + "can": "The :attribute field contains an unauthorized value.", + "confirmed": "The :attribute field confirmation does not match.", + "contains": "The :attribute field is missing a required value.", + "current_password": "The password is incorrect.", + "date": "The :attribute field must be a valid date.", + "date_equals": "The :attribute field must be a date equal to :date.", + "date_format": "The :attribute field must match the format :format.", + "decimal": "The :attribute field must have :decimal decimal places.", + "declined": "The :attribute field must be declined.", + "declined_if": "The :attribute field must be declined when :other is :value.", + "different": "The :attribute field and :other must be different.", + "digits": "The :attribute field must be :digits digits.", + "digits_between": "The :attribute field must be between :min and :max digits.", + "dimensions": "The :attribute field has invalid image dimensions.", + "distinct": "The :attribute field has a duplicate value.", + "doesnt_end_with": "The :attribute field must not end with one of the following: :values.", + "doesnt_start_with": "The :attribute field must not start with one of the following: :values.", + "email": "The :attribute field must be a valid email address.", + "ends_with": "The :attribute field must end with one of the following: :values.", + "enum": "The selected :attribute is invalid.", + "exists": "The selected :attribute is invalid.", + "extensions": "The :attribute field must have one of the following extensions: :values.", + "file": "The :attribute field must be a file.", + "filled": "The :attribute field must have a value.", + "gt": { + "array": "The :attribute field must have more than :value items.", + "file": "The :attribute field must be greater than :value kilobytes.", + "numeric": "The :attribute field must be greater than :value.", + "string": "The :attribute field must be greater than :value characters." + }, + "gte": { + "array": "The :attribute field must have :value items or more.", + "file": "The :attribute field must be greater than or equal to :value kilobytes.", + "numeric": "The :attribute field must be greater than or equal to :value.", + "string": "The :attribute field must be greater than or equal to :value characters." + }, + "hex_color": "The :attribute field must be a valid hexadecimal color.", + "image": "The :attribute field must be an image.", + "in": "The selected :attribute is invalid.", + "in_array": "The :attribute field must exist in :other.", + "integer": "The :attribute field must be an integer.", + "ip": "The :attribute field must be a valid IP address.", + "ipv4": "The :attribute field must be a valid IPv4 address.", + "ipv6": "The :attribute field must be a valid IPv6 address.", + "json": "The :attribute field must be a valid JSON string.", + "list": "The :attribute field must be a list.", + "lowercase": "The :attribute field must be lowercase.", + "lt": { + "array": "The :attribute field must have less than :value items.", + "file": "The :attribute field must be less than :value kilobytes.", + "numeric": "The :attribute field must be less than :value.", + "string": "The :attribute field must be less than :value characters." + }, + "lte": { + "array": "The :attribute field must not have more than :value items.", + "file": "The :attribute field must be less than or equal to :value kilobytes.", + "numeric": "The :attribute field must be less than or equal to :value.", + "string": "The :attribute field must be less than or equal to :value characters." + }, + "mac_address": "The :attribute field must be a valid MAC address.", + "max": { + "array": "The :attribute field must not have more than :max items.", + "file": "The :attribute field must not be greater than :max kilobytes.", + "numeric": "The :attribute field must not be greater than :max.", + "string": "The :attribute field must not be greater than :max characters." + }, + "max_digits": "The :attribute field must not have more than :max digits.", + "mimes": "The :attribute field must be a file of type: :values.", + "mimetypes": "The :attribute field must be a file of type: :values.", + "min": { + "array": "The :attribute field must have at least :min items.", + "file": "The :attribute field must be at least :min kilobytes.", + "numeric": "The :attribute field must be at least :min.", + "string": "The :attribute field must be at least :min characters." + }, + "min_digits": "The :attribute field must have at least :min digits.", + "missing": "The :attribute field must be missing.", + "missing_if": "The :attribute field must be missing when :other is :value.", + "missing_unless": "The :attribute field must be missing unless :other is :value.", + "missing_with": "The :attribute field must be missing when :values is present.", + "missing_with_all": "The :attribute field must be missing when :values are present.", + "multiple_of": "The :attribute field must be a multiple of :value.", + "not_in": "The selected :attribute is invalid.", + "not_regex": "The :attribute field format is invalid.", + "numeric": "The :attribute field must be a number.", + "password": { + "letters": "The :attribute field must contain at least one letter.", + "mixed": "The :attribute field must contain at least one uppercase and one lowercase letter.", + "numbers": "The :attribute field must contain at least one number.", + "symbols": "The :attribute field must contain at least one symbol.", + "uncompromised": "The given :attribute has appeared in a data leak. Please choose a different :attribute." + }, + "present": "The :attribute field must be present.", + "present_if": "The :attribute field must be present when :other is :value.", + "present_unless": "The :attribute field must be present unless :other is :value.", + "present_with": "The :attribute field must be present when :values is present.", + "present_with_all": "The :attribute field must be present when :values are present.", + "prohibited": "The :attribute field is prohibited.", + "prohibited_if": "The :attribute field is prohibited when :other is :value.", + "prohibited_unless": "The :attribute field is prohibited unless :other is in :values.", + "prohibits": "The :attribute field prohibits :other from being present.", + "regex": "The :attribute field format is invalid.", + "required": "The :attribute field is required.", + "required_array_keys": "The :attribute field must contain entries for: :values.", + "required_if": "The :attribute field is required when :other is :value.", + "required_if_accepted": "The :attribute field is required when :other is accepted.", + "required_if_declined": "The :attribute field is required when :other is declined.", + "required_unless": "The :attribute field is required unless :other is in :values.", + "required_with": "The :attribute field is required when :values is present.", + "required_with_all": "The :attribute field is required when :values are present.", + "required_without": "The :attribute field is required when :values is not present.", + "required_without_all": "The :attribute field is required when none of :values are present.", + "same": "The :attribute field must match :other.", + "size": { + "array": "The :attribute field must contain :size items.", + "file": "The :attribute field must be :size kilobytes.", + "numeric": "The :attribute field must be :size.", + "string": "The :attribute field must be :size characters." + }, + "starts_with": "The :attribute field must start with one of the following: :values.", + "string": "The :attribute field must be a string.", + "timezone": "The :attribute field must be a valid timezone.", + "unique": "The :attribute has already been taken.", + "uploaded": "The :attribute failed to upload.", + "uppercase": "The :attribute field must be uppercase.", + "url": "The :attribute field must be a valid URL.", + "ulid": "The :attribute field must be a valid ULID.", + "uuid": "The :attribute field must be a valid UUID.", + "custom": { + "attribute-name": { + "rule-name": "custom-message" + } + }, + "attributes": {}, + "exceptions": { + "not_found": ":Entity does not exist" + } +} \ No newline at end of file diff --git a/tests/fixtures/TranslationGeneratorTest/translations.php b/tests/fixtures/TranslationGeneratorTest/validation.php similarity index 99% rename from tests/fixtures/TranslationGeneratorTest/translations.php rename to tests/fixtures/TranslationGeneratorTest/validation.php index c453c6ba..69dcf685 100644 --- a/tests/fixtures/TranslationGeneratorTest/translations.php +++ b/tests/fixtures/TranslationGeneratorTest/validation.php @@ -118,7 +118,7 @@ 'attributes' => [], - /* + /* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- From 3ba3ec5b4763d3ac7a5f7ef45233b8013d6a2e87 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 3 Feb 2025 11:20:57 +0600 Subject: [PATCH 4/8] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/Support/FileSystemMock.php | 9 +++++ .../Translation/TranslationMockTrait.php | 35 +++++++------------ tests/TranslationGeneratorTest.php | 2 -- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/tests/Support/FileSystemMock.php b/tests/Support/FileSystemMock.php index 7d9b6f23..da7cc5a4 100644 --- a/tests/Support/FileSystemMock.php +++ b/tests/Support/FileSystemMock.php @@ -17,6 +17,7 @@ class FileSystemMock public ?array $testClasses = null; public ?array $routes = null; public ?array $factories = null; + public ?array $translations = null; public function setStructure(): void { @@ -106,6 +107,14 @@ public function setStructure(): void } } + if (!is_null($this->translations)) { + $structure['resources']['lang']['en'] = []; + + foreach ($this->translations as $translation => $content) { + $structure['resources']['lang']['en'][$translation] = $content; + } + } + vfsStream::create($structure); } } diff --git a/tests/Support/Translation/TranslationMockTrait.php b/tests/Support/Translation/TranslationMockTrait.php index ace5ed79..d9d0a5ee 100644 --- a/tests/Support/Translation/TranslationMockTrait.php +++ b/tests/Support/Translation/TranslationMockTrait.php @@ -2,41 +2,30 @@ namespace RonasIT\Support\Tests\Support\Translation; -use org\bovigo\vfs\vfsStream; +use RonasIT\Support\Tests\Support\FileSystemMock; use RonasIT\Support\Tests\Support\GeneratorMockTrait; -use RonasIT\Support\Traits\MockTrait; trait TranslationMockTrait { - use GeneratorMockTrait, MockTrait; + use GeneratorMockTrait; public function mockFilesystem(): void { - $structure = [ - 'resources' => [ - 'lang' => [ - 'en' => [], - ], - ], - ]; - - vfsStream::create($structure); + $fileSystemMock = new FileSystemMock; + + $fileSystemMock->translations = []; + + $fileSystemMock->setStructure(); } public function mockFilesystemForAppend(): void { $validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php'); - $structure = [ - 'resources' => [ - 'lang' => [ - 'en' => [ - 'validation.php' => $validation, - ], - ], - ], - ]; - - vfsStream::create($structure); + $fileSystemMock = new FileSystemMock; + + $fileSystemMock->translations = ['validation.php' => $validation]; + + $fileSystemMock->setStructure(); } } diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 2ea8f083..8e9b6152 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -37,8 +37,6 @@ public function testCreateStubNotExist() { config(['entity-generator.stubs.validation' => 'incorrect_stub']); - $this->mockFilesystem(); - app(TranslationsGenerator::class) ->setModel('Post') ->generate(); From 94c145aa1ee06ad912e5e0364f1f9443d4c6f593 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 3 Feb 2025 11:44:47 +0600 Subject: [PATCH 5/8] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- stubs/validation.blade.php | 2 +- tests/TranslationGeneratorTest.php | 2 +- .../TranslationGeneratorTest/validation.php | 2 +- .../validation_append_not_found_exception.php | 134 ++++++++++++++++++ 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php diff --git a/stubs/validation.blade.php b/stubs/validation.blade.php index 8eb54522..c6d83ebc 100644 --- a/stubs/validation.blade.php +++ b/stubs/validation.blade.php @@ -116,7 +116,7 @@ 'attributes' => [], - /* + /* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 8e9b6152..9474a5e3 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -57,7 +57,7 @@ public function testAppendNotFoundException() ->setModel('Post') ->generate(); - $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); + $this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'resources/lang/en/validation.php'); Event::assertNothingDispatched(); } diff --git a/tests/fixtures/TranslationGeneratorTest/validation.php b/tests/fixtures/TranslationGeneratorTest/validation.php index 69dcf685..c453c6ba 100644 --- a/tests/fixtures/TranslationGeneratorTest/validation.php +++ b/tests/fixtures/TranslationGeneratorTest/validation.php @@ -118,7 +118,7 @@ 'attributes' => [], - /* + /* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- diff --git a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php new file mode 100644 index 00000000..69dcf685 --- /dev/null +++ b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php @@ -0,0 +1,134 @@ + 'The :attribute must be accepted.', + 'active_url' => 'The :attribute is not a valid URL.', + 'after' => 'The :attribute must be a date after :date.', + 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', + 'alpha' => 'The :attribute may only contain letters.', + 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', + 'alpha_num' => 'The :attribute may only contain letters and numbers.', + 'array' => 'The :attribute must be an array.', + 'before' => 'The :attribute must be a date before :date.', + 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', + 'between' => [ + 'numeric' => 'The :attribute must be between :min and :max.', + 'file' => 'The :attribute must be between :min and :max kilobytes.', + 'string' => 'The :attribute must be between :min and :max characters.', + 'array' => 'The :attribute must have between :min and :max items.', + ], + 'boolean' => 'The :attribute field must be true or false.', + 'confirmed' => 'The :attribute confirmation does not match.', + 'date' => 'The :attribute is not a valid date.', + 'date_format' => 'The :attribute does not match the format :format.', + 'different' => 'The :attribute and :other must be different.', + 'digits' => 'The :attribute must be :digits digits.', + 'digits_between' => 'The :attribute must be between :min and :max digits.', + 'dimensions' => 'The :attribute has invalid image dimensions.', + 'distinct' => 'The :attribute field has a duplicate value.', + 'email' => 'The :attribute must be a valid email address.', + 'exists' => 'The selected :attribute is invalid.', + 'file' => 'The :attribute must be a file.', + 'filled' => 'The :attribute field must have a value.', + 'image' => 'The :attribute must be an image.', + 'in' => 'The selected :attribute is invalid.', + 'in_array' => 'The :attribute field does not exist in :other.', + 'integer' => 'The :attribute must be an integer.', + 'ip' => 'The :attribute must be a valid IP address.', + 'ipv4' => 'The :attribute must be a valid IPv4 address.', + 'ipv6' => 'The :attribute must be a valid IPv6 address.', + 'json' => 'The :attribute must be a valid JSON string.', + 'max' => [ + 'numeric' => 'The :attribute may not be greater than :max.', + 'file' => 'The :attribute may not be greater than :max kilobytes.', + 'string' => 'The :attribute may not be greater than :max characters.', + 'array' => 'The :attribute may not have more than :max items.', + ], + 'mimes' => 'The :attribute must be a file of type: :values.', + 'mimetypes' => 'The :attribute must be a file of type: :values.', + 'min' => [ + 'numeric' => 'The :attribute must be at least :min.', + 'file' => 'The :attribute must be at least :min kilobytes.', + 'string' => 'The :attribute must be at least :min characters.', + 'array' => 'The :attribute must have at least :min items.', + ], + 'not_in' => 'The selected :attribute is invalid.', + 'numeric' => 'The :attribute must be a number.', + 'present' => 'The :attribute field must be present.', + 'regex' => 'The :attribute format is invalid.', + 'required' => 'The :attribute field is required.', + 'required_if' => 'The :attribute field is required when :other is :value.', + 'required_unless' => 'The :attribute field is required unless :other is in :values.', + 'required_with' => 'The :attribute field is required when :values is present.', + 'required_with_all' => 'The :attribute field is required when :values is present.', + 'required_without' => 'The :attribute field is required when :values is not present.', + 'required_without_all' => 'The :attribute field is required when none of :values are present.', + 'same' => 'The :attribute and :other must match.', + 'size' => [ + 'numeric' => 'The :attribute must be :size.', + 'file' => 'The :attribute must be :size kilobytes.', + 'string' => 'The :attribute must be :size characters.', + 'array' => 'The :attribute must contain :size items.', + ], + 'string' => 'The :attribute must be a string.', + 'timezone' => 'The :attribute must be a valid zone.', + 'unique' => 'The :attribute has already been taken.', + 'uploaded' => 'The :attribute failed to upload.', + 'url' => 'The :attribute format is invalid.', + + /* + |-------------------------------------------------------------------------- + | Custom Validation Language Lines + |-------------------------------------------------------------------------- + | + | Here you may specify custom validation messages for attributes using the + | convention "attribute.rule" to name the lines. This makes it quick to + | specify a specific custom language line for a given attribute rule. + | + */ + + 'custom' => [ + 'attribute-name' => [ + 'rule-name' => 'custom-message', + ], + ], + + /* + |-------------------------------------------------------------------------- + | Custom Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap attribute place-holders + | with something more reader friendly such as E-Mail Address instead + | of "email". This simply helps us make messages a little cleaner. + | + */ + + 'attributes' => [], + + /* + |-------------------------------------------------------------------------- + | Custom Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap http error messages. + | + */ + + 'exceptions' => [ + 'not_found' => ':Entity does not exist', + ], + +]; From 6067363649a6a2322624a22d0dde6f52807ba180 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 3 Feb 2025 13:11:15 +0600 Subject: [PATCH 6/8] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/TranslationsGenerator.php | 2 +- tests/TranslationGeneratorTest.php | 2 +- .../validation_append_not_found_exception.php | 134 ------------------ 3 files changed, 2 insertions(+), 136 deletions(-) delete mode 100644 tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index 383083aa..f36dbfe5 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -53,7 +53,7 @@ protected function appendNotFoundException(): void $stubContent = view($stubPath)->render(); - $fixedContent = preg_replace('/\]\;\s*$/', "\n\t{$stubContent}", $content); + $fixedContent = preg_replace('/\]\;\s*$/', "\n {$stubContent}", $content); file_put_contents($this->translationPath, $fixedContent); } diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 9474a5e3..8e9b6152 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -57,7 +57,7 @@ public function testAppendNotFoundException() ->setModel('Post') ->generate(); - $this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'resources/lang/en/validation.php'); + $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); Event::assertNothingDispatched(); } diff --git a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php deleted file mode 100644 index 69dcf685..00000000 --- a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php +++ /dev/null @@ -1,134 +0,0 @@ - 'The :attribute must be accepted.', - 'active_url' => 'The :attribute is not a valid URL.', - 'after' => 'The :attribute must be a date after :date.', - 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', - 'alpha' => 'The :attribute may only contain letters.', - 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', - 'alpha_num' => 'The :attribute may only contain letters and numbers.', - 'array' => 'The :attribute must be an array.', - 'before' => 'The :attribute must be a date before :date.', - 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', - 'between' => [ - 'numeric' => 'The :attribute must be between :min and :max.', - 'file' => 'The :attribute must be between :min and :max kilobytes.', - 'string' => 'The :attribute must be between :min and :max characters.', - 'array' => 'The :attribute must have between :min and :max items.', - ], - 'boolean' => 'The :attribute field must be true or false.', - 'confirmed' => 'The :attribute confirmation does not match.', - 'date' => 'The :attribute is not a valid date.', - 'date_format' => 'The :attribute does not match the format :format.', - 'different' => 'The :attribute and :other must be different.', - 'digits' => 'The :attribute must be :digits digits.', - 'digits_between' => 'The :attribute must be between :min and :max digits.', - 'dimensions' => 'The :attribute has invalid image dimensions.', - 'distinct' => 'The :attribute field has a duplicate value.', - 'email' => 'The :attribute must be a valid email address.', - 'exists' => 'The selected :attribute is invalid.', - 'file' => 'The :attribute must be a file.', - 'filled' => 'The :attribute field must have a value.', - 'image' => 'The :attribute must be an image.', - 'in' => 'The selected :attribute is invalid.', - 'in_array' => 'The :attribute field does not exist in :other.', - 'integer' => 'The :attribute must be an integer.', - 'ip' => 'The :attribute must be a valid IP address.', - 'ipv4' => 'The :attribute must be a valid IPv4 address.', - 'ipv6' => 'The :attribute must be a valid IPv6 address.', - 'json' => 'The :attribute must be a valid JSON string.', - 'max' => [ - 'numeric' => 'The :attribute may not be greater than :max.', - 'file' => 'The :attribute may not be greater than :max kilobytes.', - 'string' => 'The :attribute may not be greater than :max characters.', - 'array' => 'The :attribute may not have more than :max items.', - ], - 'mimes' => 'The :attribute must be a file of type: :values.', - 'mimetypes' => 'The :attribute must be a file of type: :values.', - 'min' => [ - 'numeric' => 'The :attribute must be at least :min.', - 'file' => 'The :attribute must be at least :min kilobytes.', - 'string' => 'The :attribute must be at least :min characters.', - 'array' => 'The :attribute must have at least :min items.', - ], - 'not_in' => 'The selected :attribute is invalid.', - 'numeric' => 'The :attribute must be a number.', - 'present' => 'The :attribute field must be present.', - 'regex' => 'The :attribute format is invalid.', - 'required' => 'The :attribute field is required.', - 'required_if' => 'The :attribute field is required when :other is :value.', - 'required_unless' => 'The :attribute field is required unless :other is in :values.', - 'required_with' => 'The :attribute field is required when :values is present.', - 'required_with_all' => 'The :attribute field is required when :values is present.', - 'required_without' => 'The :attribute field is required when :values is not present.', - 'required_without_all' => 'The :attribute field is required when none of :values are present.', - 'same' => 'The :attribute and :other must match.', - 'size' => [ - 'numeric' => 'The :attribute must be :size.', - 'file' => 'The :attribute must be :size kilobytes.', - 'string' => 'The :attribute must be :size characters.', - 'array' => 'The :attribute must contain :size items.', - ], - 'string' => 'The :attribute must be a string.', - 'timezone' => 'The :attribute must be a valid zone.', - 'unique' => 'The :attribute has already been taken.', - 'uploaded' => 'The :attribute failed to upload.', - 'url' => 'The :attribute format is invalid.', - - /* - |-------------------------------------------------------------------------- - | Custom Validation Language Lines - |-------------------------------------------------------------------------- - | - | Here you may specify custom validation messages for attributes using the - | convention "attribute.rule" to name the lines. This makes it quick to - | specify a specific custom language line for a given attribute rule. - | - */ - - 'custom' => [ - 'attribute-name' => [ - 'rule-name' => 'custom-message', - ], - ], - - /* - |-------------------------------------------------------------------------- - | Custom Validation Attributes - |-------------------------------------------------------------------------- - | - | The following language lines are used to swap attribute place-holders - | with something more reader friendly such as E-Mail Address instead - | of "email". This simply helps us make messages a little cleaner. - | - */ - - 'attributes' => [], - - /* - |-------------------------------------------------------------------------- - | Custom Validation Attributes - |-------------------------------------------------------------------------- - | - | The following language lines are used to swap http error messages. - | - */ - - 'exceptions' => [ - 'not_found' => ':Entity does not exist', - ], - -]; From 216a37bf313a37f6122a88dab29c9094ed1e5605 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 4 Feb 2025 11:00:31 +0600 Subject: [PATCH 7/8] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- .../Translation/TranslationMockTrait.php | 4 +- .../validation_without_exceptions.php | 119 +----------------- tests/TranslationGeneratorTest.php | 2 +- .../validation_append_not_found_exception.php | 17 +++ 4 files changed, 21 insertions(+), 121 deletions(-) create mode 100644 tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php diff --git a/tests/Support/Translation/TranslationMockTrait.php b/tests/Support/Translation/TranslationMockTrait.php index d9d0a5ee..fa8ec1d2 100644 --- a/tests/Support/Translation/TranslationMockTrait.php +++ b/tests/Support/Translation/TranslationMockTrait.php @@ -11,7 +11,7 @@ trait TranslationMockTrait public function mockFilesystem(): void { - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->translations = []; @@ -22,7 +22,7 @@ public function mockFilesystemForAppend(): void { $validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php'); - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->translations = ['validation.php' => $validation]; diff --git a/tests/Support/Translation/validation_without_exceptions.php b/tests/Support/Translation/validation_without_exceptions.php index 6f90f6cd..ae48a802 100644 --- a/tests/Support/Translation/validation_without_exceptions.php +++ b/tests/Support/Translation/validation_without_exceptions.php @@ -1,120 +1,3 @@ 'The :attribute must be accepted.', - 'active_url' => 'The :attribute is not a valid URL.', - 'after' => 'The :attribute must be a date after :date.', - 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', - 'alpha' => 'The :attribute may only contain letters.', - 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', - 'alpha_num' => 'The :attribute may only contain letters and numbers.', - 'array' => 'The :attribute must be an array.', - 'before' => 'The :attribute must be a date before :date.', - 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', - 'between' => [ - 'numeric' => 'The :attribute must be between :min and :max.', - 'file' => 'The :attribute must be between :min and :max kilobytes.', - 'string' => 'The :attribute must be between :min and :max characters.', - 'array' => 'The :attribute must have between :min and :max items.', - ], - 'boolean' => 'The :attribute field must be true or false.', - 'confirmed' => 'The :attribute confirmation does not match.', - 'date' => 'The :attribute is not a valid date.', - 'date_format' => 'The :attribute does not match the format :format.', - 'different' => 'The :attribute and :other must be different.', - 'digits' => 'The :attribute must be :digits digits.', - 'digits_between' => 'The :attribute must be between :min and :max digits.', - 'dimensions' => 'The :attribute has invalid image dimensions.', - 'distinct' => 'The :attribute field has a duplicate value.', - 'email' => 'The :attribute must be a valid email address.', - 'exists' => 'The selected :attribute is invalid.', - 'file' => 'The :attribute must be a file.', - 'filled' => 'The :attribute field must have a value.', - 'image' => 'The :attribute must be an image.', - 'in' => 'The selected :attribute is invalid.', - 'in_array' => 'The :attribute field does not exist in :other.', - 'integer' => 'The :attribute must be an integer.', - 'ip' => 'The :attribute must be a valid IP address.', - 'ipv4' => 'The :attribute must be a valid IPv4 address.', - 'ipv6' => 'The :attribute must be a valid IPv6 address.', - 'json' => 'The :attribute must be a valid JSON string.', - 'max' => [ - 'numeric' => 'The :attribute may not be greater than :max.', - 'file' => 'The :attribute may not be greater than :max kilobytes.', - 'string' => 'The :attribute may not be greater than :max characters.', - 'array' => 'The :attribute may not have more than :max items.', - ], - 'mimes' => 'The :attribute must be a file of type: :values.', - 'mimetypes' => 'The :attribute must be a file of type: :values.', - 'min' => [ - 'numeric' => 'The :attribute must be at least :min.', - 'file' => 'The :attribute must be at least :min kilobytes.', - 'string' => 'The :attribute must be at least :min characters.', - 'array' => 'The :attribute must have at least :min items.', - ], - 'not_in' => 'The selected :attribute is invalid.', - 'numeric' => 'The :attribute must be a number.', - 'present' => 'The :attribute field must be present.', - 'regex' => 'The :attribute format is invalid.', - 'required' => 'The :attribute field is required.', - 'required_if' => 'The :attribute field is required when :other is :value.', - 'required_unless' => 'The :attribute field is required unless :other is in :values.', - 'required_with' => 'The :attribute field is required when :values is present.', - 'required_with_all' => 'The :attribute field is required when :values is present.', - 'required_without' => 'The :attribute field is required when :values is not present.', - 'required_without_all' => 'The :attribute field is required when none of :values are present.', - 'same' => 'The :attribute and :other must match.', - 'size' => [ - 'numeric' => 'The :attribute must be :size.', - 'file' => 'The :attribute must be :size kilobytes.', - 'string' => 'The :attribute must be :size characters.', - 'array' => 'The :attribute must contain :size items.', - ], - 'string' => 'The :attribute must be a string.', - 'timezone' => 'The :attribute must be a valid zone.', - 'unique' => 'The :attribute has already been taken.', - 'uploaded' => 'The :attribute failed to upload.', - 'url' => 'The :attribute format is invalid.', - - /* - |-------------------------------------------------------------------------- - | Custom Validation Language Lines - |-------------------------------------------------------------------------- - | - | Here you may specify custom validation messages for attributes using the - | convention "attribute.rule" to name the lines. This makes it quick to - | specify a specific custom language line for a given attribute rule. - | - */ - - 'custom' => [ - 'attribute-name' => [ - 'rule-name' => 'custom-message', - ], - ], - - /* - |-------------------------------------------------------------------------- - | Custom Validation Attributes - |-------------------------------------------------------------------------- - | - | The following language lines are used to swap attribute place-holders - | with something more reader friendly such as E-Mail Address instead - | of "email". This simply helps us make messages a little cleaner. - | - */ - - 'attributes' => [], -]; +return []; diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 8e9b6152..9474a5e3 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -57,7 +57,7 @@ public function testAppendNotFoundException() ->setModel('Post') ->generate(); - $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); + $this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'resources/lang/en/validation.php'); Event::assertNothingDispatched(); } diff --git a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php new file mode 100644 index 00000000..ae0c21f2 --- /dev/null +++ b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php @@ -0,0 +1,17 @@ + [ + 'not_found' => ':Entity does not exist', + ], + +]; From 3252fcb55bc31fcb958ccb4d2172ba333deab03e Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 4 Feb 2025 14:03:46 +0600 Subject: [PATCH 8/8] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/TranslationGeneratorTest.php | 4 +- .../TranslationGeneratorTest/validation.json | 158 ------------------ 2 files changed, 1 insertion(+), 161 deletions(-) delete mode 100644 tests/fixtures/TranslationGeneratorTest/validation.json diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 9474a5e3..33b53bb2 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -15,9 +15,7 @@ class TranslationGeneratorTest extends TestCase public function testCreate() { - $translations = $this->getJsonFixture('validation.json'); - - Lang::shouldReceive('get')->andReturn($translations); + Lang::shouldReceive('get')->andReturn([]); $this->mockFilesystem(); diff --git a/tests/fixtures/TranslationGeneratorTest/validation.json b/tests/fixtures/TranslationGeneratorTest/validation.json deleted file mode 100644 index 0e1eb78d..00000000 --- a/tests/fixtures/TranslationGeneratorTest/validation.json +++ /dev/null @@ -1,158 +0,0 @@ -{ - "accepted": "The :attribute field must be accepted.", - "accepted_if": "The :attribute field must be accepted when :other is :value.", - "active_url": "The :attribute field must be a valid URL.", - "after": "The :attribute field must be a date after :date.", - "after_or_equal": "The :attribute field must be a date after or equal to :date.", - "alpha": "The :attribute field must only contain letters.", - "alpha_dash": "The :attribute field must only contain letters, numbers, dashes, and underscores.", - "alpha_num": "The :attribute field must only contain letters and numbers.", - "array": "The :attribute field must be an array.", - "ascii": "The :attribute field must only contain single-byte alphanumeric characters and symbols.", - "before": "The :attribute field must be a date before :date.", - "before_or_equal": "The :attribute field must be a date before or equal to :date.", - "between": { - "array": "The :attribute field must have between :min and :max items.", - "file": "The :attribute field must be between :min and :max kilobytes.", - "numeric": "The :attribute field must be between :min and :max.", - "string": "The :attribute field must be between :min and :max characters." - }, - "boolean": "The :attribute field must be true or false.", - "can": "The :attribute field contains an unauthorized value.", - "confirmed": "The :attribute field confirmation does not match.", - "contains": "The :attribute field is missing a required value.", - "current_password": "The password is incorrect.", - "date": "The :attribute field must be a valid date.", - "date_equals": "The :attribute field must be a date equal to :date.", - "date_format": "The :attribute field must match the format :format.", - "decimal": "The :attribute field must have :decimal decimal places.", - "declined": "The :attribute field must be declined.", - "declined_if": "The :attribute field must be declined when :other is :value.", - "different": "The :attribute field and :other must be different.", - "digits": "The :attribute field must be :digits digits.", - "digits_between": "The :attribute field must be between :min and :max digits.", - "dimensions": "The :attribute field has invalid image dimensions.", - "distinct": "The :attribute field has a duplicate value.", - "doesnt_end_with": "The :attribute field must not end with one of the following: :values.", - "doesnt_start_with": "The :attribute field must not start with one of the following: :values.", - "email": "The :attribute field must be a valid email address.", - "ends_with": "The :attribute field must end with one of the following: :values.", - "enum": "The selected :attribute is invalid.", - "exists": "The selected :attribute is invalid.", - "extensions": "The :attribute field must have one of the following extensions: :values.", - "file": "The :attribute field must be a file.", - "filled": "The :attribute field must have a value.", - "gt": { - "array": "The :attribute field must have more than :value items.", - "file": "The :attribute field must be greater than :value kilobytes.", - "numeric": "The :attribute field must be greater than :value.", - "string": "The :attribute field must be greater than :value characters." - }, - "gte": { - "array": "The :attribute field must have :value items or more.", - "file": "The :attribute field must be greater than or equal to :value kilobytes.", - "numeric": "The :attribute field must be greater than or equal to :value.", - "string": "The :attribute field must be greater than or equal to :value characters." - }, - "hex_color": "The :attribute field must be a valid hexadecimal color.", - "image": "The :attribute field must be an image.", - "in": "The selected :attribute is invalid.", - "in_array": "The :attribute field must exist in :other.", - "integer": "The :attribute field must be an integer.", - "ip": "The :attribute field must be a valid IP address.", - "ipv4": "The :attribute field must be a valid IPv4 address.", - "ipv6": "The :attribute field must be a valid IPv6 address.", - "json": "The :attribute field must be a valid JSON string.", - "list": "The :attribute field must be a list.", - "lowercase": "The :attribute field must be lowercase.", - "lt": { - "array": "The :attribute field must have less than :value items.", - "file": "The :attribute field must be less than :value kilobytes.", - "numeric": "The :attribute field must be less than :value.", - "string": "The :attribute field must be less than :value characters." - }, - "lte": { - "array": "The :attribute field must not have more than :value items.", - "file": "The :attribute field must be less than or equal to :value kilobytes.", - "numeric": "The :attribute field must be less than or equal to :value.", - "string": "The :attribute field must be less than or equal to :value characters." - }, - "mac_address": "The :attribute field must be a valid MAC address.", - "max": { - "array": "The :attribute field must not have more than :max items.", - "file": "The :attribute field must not be greater than :max kilobytes.", - "numeric": "The :attribute field must not be greater than :max.", - "string": "The :attribute field must not be greater than :max characters." - }, - "max_digits": "The :attribute field must not have more than :max digits.", - "mimes": "The :attribute field must be a file of type: :values.", - "mimetypes": "The :attribute field must be a file of type: :values.", - "min": { - "array": "The :attribute field must have at least :min items.", - "file": "The :attribute field must be at least :min kilobytes.", - "numeric": "The :attribute field must be at least :min.", - "string": "The :attribute field must be at least :min characters." - }, - "min_digits": "The :attribute field must have at least :min digits.", - "missing": "The :attribute field must be missing.", - "missing_if": "The :attribute field must be missing when :other is :value.", - "missing_unless": "The :attribute field must be missing unless :other is :value.", - "missing_with": "The :attribute field must be missing when :values is present.", - "missing_with_all": "The :attribute field must be missing when :values are present.", - "multiple_of": "The :attribute field must be a multiple of :value.", - "not_in": "The selected :attribute is invalid.", - "not_regex": "The :attribute field format is invalid.", - "numeric": "The :attribute field must be a number.", - "password": { - "letters": "The :attribute field must contain at least one letter.", - "mixed": "The :attribute field must contain at least one uppercase and one lowercase letter.", - "numbers": "The :attribute field must contain at least one number.", - "symbols": "The :attribute field must contain at least one symbol.", - "uncompromised": "The given :attribute has appeared in a data leak. Please choose a different :attribute." - }, - "present": "The :attribute field must be present.", - "present_if": "The :attribute field must be present when :other is :value.", - "present_unless": "The :attribute field must be present unless :other is :value.", - "present_with": "The :attribute field must be present when :values is present.", - "present_with_all": "The :attribute field must be present when :values are present.", - "prohibited": "The :attribute field is prohibited.", - "prohibited_if": "The :attribute field is prohibited when :other is :value.", - "prohibited_unless": "The :attribute field is prohibited unless :other is in :values.", - "prohibits": "The :attribute field prohibits :other from being present.", - "regex": "The :attribute field format is invalid.", - "required": "The :attribute field is required.", - "required_array_keys": "The :attribute field must contain entries for: :values.", - "required_if": "The :attribute field is required when :other is :value.", - "required_if_accepted": "The :attribute field is required when :other is accepted.", - "required_if_declined": "The :attribute field is required when :other is declined.", - "required_unless": "The :attribute field is required unless :other is in :values.", - "required_with": "The :attribute field is required when :values is present.", - "required_with_all": "The :attribute field is required when :values are present.", - "required_without": "The :attribute field is required when :values is not present.", - "required_without_all": "The :attribute field is required when none of :values are present.", - "same": "The :attribute field must match :other.", - "size": { - "array": "The :attribute field must contain :size items.", - "file": "The :attribute field must be :size kilobytes.", - "numeric": "The :attribute field must be :size.", - "string": "The :attribute field must be :size characters." - }, - "starts_with": "The :attribute field must start with one of the following: :values.", - "string": "The :attribute field must be a string.", - "timezone": "The :attribute field must be a valid timezone.", - "unique": "The :attribute has already been taken.", - "uploaded": "The :attribute failed to upload.", - "uppercase": "The :attribute field must be uppercase.", - "url": "The :attribute field must be a valid URL.", - "ulid": "The :attribute field must be a valid ULID.", - "uuid": "The :attribute field must be a valid UUID.", - "custom": { - "attribute-name": { - "rule-name": "custom-message" - } - }, - "attributes": {}, - "exceptions": { - "not_found": ":Entity does not exist" - } -} \ No newline at end of file