diff --git a/stubs/request.blade.php b/stubs/request.blade.php index 603354f7..979bf6b9 100644 --- a/stubs/request.blade.php +++ b/stubs/request.blade.php @@ -22,6 +22,9 @@ public function rules(): array return []; @endif } +@endif +@if($method !== $requestsGenerator::DELETE_METHOD && $needToValidate) + @endif @if($needToValidate) public function validateResolved(): void diff --git a/tests/RequestGeneratorTest.php b/tests/RequestGeneratorTest.php new file mode 100644 index 00000000..d99b42b3 --- /dev/null +++ b/tests/RequestGeneratorTest.php @@ -0,0 +1,62 @@ +setModel('Post') + ->setRelations([ + 'belongsTo' => ['User'], + 'hasMany' => ['Comments'], + 'hasOne' => [], + 'belongsToMany' => [] + ]) + ->setFields([ + 'boolean-required' => ['is_published'], + 'integer' => ['user_id'], + 'boolean' => ['is_draft'], + ]) + ->setCrudOptions(['C', 'R', 'U', 'D']) + ->generate(); + + $this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Post/GetPostRequest.php'); + $this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Post/SearchPostsRequest.php'); + $this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Post/DeletePostRequest.php'); + $this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Post/UpdatePostRequest.php'); + $this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php'); + + $this->assertEventPushedChain([ + SuccessCreateMessage::class => [ + 'Created a new Request: GetPostRequest', + 'Created a new Request: SearchPostsRequest', + 'Created a new Request: DeletePostRequest', + 'Created a new Request: CreatePostRequest', + 'Created a new Request: UpdatePostRequest', + ], + ]); + } + + public function testCreateRequestStubNotExist() + { + config(['entity-generator.stubs.request' => 'incorrect_stub']); + + app(RequestsGenerator::class) + ->setModel('Post') + ->setCrudOptions(['C']) + ->generate(); + + $this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php'); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'Generation of request has been skipped cause the view incorrect_stub from the config entity-generator.stubs.request is not exists. Please check that config has the correct view name value.', + ); + } +} diff --git a/tests/fixtures/RequestGeneratorTest/create_request.php b/tests/fixtures/RequestGeneratorTest/create_request.php new file mode 100644 index 00000000..122aefdb --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/create_request.php @@ -0,0 +1,17 @@ + 'integer|exists:users,id|required', + 'is_draft' => 'boolean', + 'is_published' => 'boolean|present', + ]; + } +} \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/delete_request.php b/tests/fixtures/RequestGeneratorTest/delete_request.php new file mode 100644 index 00000000..2b98c521 --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/delete_request.php @@ -0,0 +1,21 @@ +exists($this->route('id'))) { + throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); + } + } +} \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/get_request.php b/tests/fixtures/RequestGeneratorTest/get_request.php new file mode 100644 index 00000000..4440a220 --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/get_request.php @@ -0,0 +1,29 @@ + 'array', + 'with.*' => 'string|required', + ]; + } + + public function validateResolved(): void + { + parent::validateResolved(); + + $service = app(PostService::class); + + if (!$service->exists($this->route('id'))) { + throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); + } + } +} \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/search_request.php b/tests/fixtures/RequestGeneratorTest/search_request.php new file mode 100644 index 00000000..75a646ea --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/search_request.php @@ -0,0 +1,24 @@ + 'integer|exists:users,id|required', + 'page' => 'integer', + 'per_page' => 'integer', + 'is_published' => 'boolean', + 'desc' => 'boolean', + 'all' => 'boolean', + 'with' => 'array', + 'order_by' => 'string', + 'query' => 'string|nullable', + 'with.*' => 'string', + ]; + } +} \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/update_request.php b/tests/fixtures/RequestGeneratorTest/update_request.php new file mode 100644 index 00000000..a1eae966 --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/update_request.php @@ -0,0 +1,30 @@ + 'integer|exists:users,id|required', + 'is_draft' => 'boolean', + 'is_published' => 'boolean', + ]; + } + + public function validateResolved(): void + { + parent::validateResolved(); + + $service = app(PostService::class); + + if (!$service->exists($this->route('id'))) { + throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); + } + } +} \ No newline at end of file