diff --git a/tests/RequestGeneratorTest.php b/tests/RequestGeneratorTest.php new file mode 100644 index 0000000..c93691c --- /dev/null +++ b/tests/RequestGeneratorTest.php @@ -0,0 +1,44 @@ +mockConfigurations(); + $this->mockViewsNamespace(); + $this->mockFilesystem(); + + app(RequestsGenerator::class) + ->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->rollbackToDefaultBasePath(); + + $this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Posts/GetPostRequest.php'); + $this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Posts/SearchPostsRequest.php'); + $this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Posts/DeletePostRequest.php'); + $this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Posts/UpdatePostRequest.php'); + $this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Posts/CreatePostRequest.php'); + } +} diff --git a/tests/Support/Request/RequestMockTrait.php b/tests/Support/Request/RequestMockTrait.php new file mode 100644 index 0000000..a8364b4 --- /dev/null +++ b/tests/Support/Request/RequestMockTrait.php @@ -0,0 +1,52 @@ +mockClass(RepositoryGenerator::class, [ + [ + 'method' => 'classExists', + 'arguments' => ['models', 'Post'], + 'result' => false + ], + ]); + } + + public function mockConfigurations(): void + { + config([ + 'entity-generator.stubs.request' => 'entity-generator::request', + 'entity-generator.paths' => [ + 'requests' => 'app/Http/Requests', + 'services' => 'app/Services', + ] + ]); + } + + public function mockFilesystem(): void + { + $structure = [ + 'app' => [ + 'Http' => [ + 'Requests' => [ + 'Posts' => [] + ], + ], + 'Services' => [ + 'PostService.php' => ' '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 0000000..33465cb --- /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 0000000..e1c0322 --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/get_request.php @@ -0,0 +1,29 @@ + 'array', + 'with.*' => 'string|required', + ]; + } + + public function validateResolved() + { + 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 0000000..cf005cc --- /dev/null +++ b/tests/fixtures/RequestGeneratorTest/search_request.php @@ -0,0 +1,24 @@ + 'integer|exists:users,id|required', + 'page' => 'integer', + 'per_page' => 'integer', + 'all' => 'integer', + 'is_published' => 'boolean', + 'desc' => '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 0000000..7d66b70 --- /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() + { + 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