From 6dbec555f2a33922e7e997f9fda6e3746fdcceaa Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Thu, 21 Dec 2023 21:03:03 +0400 Subject: [PATCH 1/6] tests: add requests generator tests. --- tests/RequestGeneratorTest.php | 44 ++++++++++++++++ tests/Support/Request/RequestMockTrait.php | 52 +++++++++++++++++++ .../RequestGeneratorTest/create_request.php | 17 ++++++ .../RequestGeneratorTest/delete_request.php | 21 ++++++++ .../RequestGeneratorTest/get_request.php | 29 +++++++++++ .../RequestGeneratorTest/search_request.php | 24 +++++++++ .../RequestGeneratorTest/update_request.php | 30 +++++++++++ 7 files changed, 217 insertions(+) create mode 100644 tests/RequestGeneratorTest.php create mode 100644 tests/Support/Request/RequestMockTrait.php create mode 100644 tests/fixtures/RequestGeneratorTest/create_request.php create mode 100644 tests/fixtures/RequestGeneratorTest/delete_request.php create mode 100644 tests/fixtures/RequestGeneratorTest/get_request.php create mode 100644 tests/fixtures/RequestGeneratorTest/search_request.php create mode 100644 tests/fixtures/RequestGeneratorTest/update_request.php diff --git a/tests/RequestGeneratorTest.php b/tests/RequestGeneratorTest.php new file mode 100644 index 00000000..c93691c8 --- /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 00000000..a8364b40 --- /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 00000000..33465cba --- /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..e1c03223 --- /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 00000000..cf005cc2 --- /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 00000000..7d66b70f --- /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 From e37a9e10a4fd0d4a19dbdd33375d9a58db86973b Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 10 Jan 2025 16:00:32 +0600 Subject: [PATCH 2/6] fix: conflicts refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/RequestGeneratorTest.php | 2 -- tests/Support/Request/RequestMockTrait.php | 17 +++-------------- tests/TestCase.php | 11 +++++++++++ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/RequestGeneratorTest.php b/tests/RequestGeneratorTest.php index c93691c8..cb13a975 100644 --- a/tests/RequestGeneratorTest.php +++ b/tests/RequestGeneratorTest.php @@ -2,8 +2,6 @@ namespace RonasIT\Support\Tests; -use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Generators\RepositoryGenerator; use RonasIT\Support\Generators\RequestsGenerator; use RonasIT\Support\Tests\Support\Request\RequestMockTrait; diff --git a/tests/Support/Request/RequestMockTrait.php b/tests/Support/Request/RequestMockTrait.php index a8364b40..99fc0002 100644 --- a/tests/Support/Request/RequestMockTrait.php +++ b/tests/Support/Request/RequestMockTrait.php @@ -3,23 +3,12 @@ namespace RonasIT\Support\Tests\Support\Request; use org\bovigo\vfs\vfsStream; -use RonasIT\Support\Generators\RepositoryGenerator; -use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; +use RonasIT\Support\Tests\Support\GeneratorMockTrait; +use RonasIT\Support\Traits\MockTrait; trait RequestMockTrait { - use GeneratorMockTrait; - - public function mockGeneratorForCreation(): void - { - $this->mockClass(RepositoryGenerator::class, [ - [ - 'method' => 'classExists', - 'arguments' => ['models', 'Post'], - 'result' => false - ], - ]); - } + use GeneratorMockTrait, MockTrait; public function mockConfigurations(): void { diff --git a/tests/TestCase.php b/tests/TestCase.php index a5c0f83c..e3cd41a8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\Concerns\InteractsWithViews; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\View; use Illuminate\Support\Str; use Orchestra\Testbench\TestCase as BaseTestCase; use org\bovigo\vfs\vfsStream; @@ -113,4 +114,14 @@ protected function assertExceptionThrew(string $className, string $message): voi $this->expectException($className); $this->expectExceptionMessage($message); } + + public function mockViewsNamespace(): void + { + View::addNamespace('entity-generator', getcwd() . '/stubs'); + } + + public function rollbackToDefaultBasePath(): void + { + $this->app->setBasePath(getcwd()); + } } From 040af6ad55e37961310147ad6a9c75ce459fc545 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 10 Jan 2025 18:55:22 +0600 Subject: [PATCH 3/6] feat: add tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- stubs/request.blade.php | 3 ++ tests/RequestGeneratorTest.php | 51 +++++++++++++++---- tests/Support/Request/RequestMockTrait.php | 11 ---- tests/TestCase.php | 10 ---- .../RequestGeneratorTest/create_request.php | 2 +- .../RequestGeneratorTest/delete_request.php | 4 +- .../RequestGeneratorTest/get_request.php | 4 +- .../RequestGeneratorTest/search_request.php | 4 +- .../RequestGeneratorTest/update_request.php | 4 +- 9 files changed, 53 insertions(+), 40 deletions(-) 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 index cb13a975..14e1fab6 100644 --- a/tests/RequestGeneratorTest.php +++ b/tests/RequestGeneratorTest.php @@ -2,6 +2,9 @@ namespace RonasIT\Support\Tests; +use Illuminate\Support\Facades\Event; +use RonasIT\Support\Events\SuccessCreateMessage; +use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Generators\RequestsGenerator; use RonasIT\Support\Tests\Support\Request\RequestMockTrait; @@ -9,12 +12,15 @@ class RequestGeneratorTest extends TestCase { use RequestMockTrait; - public function testCreateRequests() + public function setUp(): void { - $this->mockConfigurations(); - $this->mockViewsNamespace(); - $this->mockFilesystem(); + parent::setUp(); + + Event::fake(); + } + public function testCreateRequests() + { app(RequestsGenerator::class) ->setModel('Post') ->setRelations([ @@ -31,12 +37,37 @@ public function testCreateRequests() ->setCrudOptions(['C', 'R', 'U', 'D']) ->generate(); - $this->rollbackToDefaultBasePath(); + $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->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'); + $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/Support/Request/RequestMockTrait.php b/tests/Support/Request/RequestMockTrait.php index 99fc0002..b0f452ae 100644 --- a/tests/Support/Request/RequestMockTrait.php +++ b/tests/Support/Request/RequestMockTrait.php @@ -10,17 +10,6 @@ trait RequestMockTrait { use GeneratorMockTrait, MockTrait; - 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 = [ diff --git a/tests/TestCase.php b/tests/TestCase.php index e3cd41a8..f55c488c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -114,14 +114,4 @@ protected function assertExceptionThrew(string $className, string $message): voi $this->expectException($className); $this->expectExceptionMessage($message); } - - public function mockViewsNamespace(): void - { - View::addNamespace('entity-generator', getcwd() . '/stubs'); - } - - public function rollbackToDefaultBasePath(): void - { - $this->app->setBasePath(getcwd()); - } } diff --git a/tests/fixtures/RequestGeneratorTest/create_request.php b/tests/fixtures/RequestGeneratorTest/create_request.php index 3632332c..122aefdb 100644 --- a/tests/fixtures/RequestGeneratorTest/create_request.php +++ b/tests/fixtures/RequestGeneratorTest/create_request.php @@ -1,6 +1,6 @@ 'integer|exists:users,id|required', 'page' => 'integer', 'per_page' => 'integer', - 'all' => 'integer', 'is_published' => 'boolean', 'desc' => 'boolean', + 'all' => 'boolean', 'with' => 'array', 'order_by' => 'string', 'query' => 'string|nullable', diff --git a/tests/fixtures/RequestGeneratorTest/update_request.php b/tests/fixtures/RequestGeneratorTest/update_request.php index 7d66b70f..a1eae966 100644 --- a/tests/fixtures/RequestGeneratorTest/update_request.php +++ b/tests/fixtures/RequestGeneratorTest/update_request.php @@ -1,6 +1,6 @@ Date: Fri, 10 Jan 2025 22:41:50 +0600 Subject: [PATCH 4/6] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/RequestGeneratorTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/RequestGeneratorTest.php b/tests/RequestGeneratorTest.php index 14e1fab6..418e350a 100644 --- a/tests/RequestGeneratorTest.php +++ b/tests/RequestGeneratorTest.php @@ -2,7 +2,6 @@ namespace RonasIT\Support\Tests; -use Illuminate\Support\Facades\Event; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Generators\RequestsGenerator; @@ -12,13 +11,6 @@ class RequestGeneratorTest extends TestCase { use RequestMockTrait; - public function setUp(): void - { - parent::setUp(); - - Event::fake(); - } - public function testCreateRequests() { app(RequestsGenerator::class) From 673485eed60c9454c1c770e246cd9edaff6e8de7 Mon Sep 17 00:00:00 2001 From: roman Date: Thu, 16 Jan 2025 14:39:36 +0600 Subject: [PATCH 5/6] refactor: tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/RequestGeneratorTest.php | 3 --- tests/Support/Request/RequestMockTrait.php | 30 ---------------------- 2 files changed, 33 deletions(-) delete mode 100644 tests/Support/Request/RequestMockTrait.php diff --git a/tests/RequestGeneratorTest.php b/tests/RequestGeneratorTest.php index 418e350a..d99b42b3 100644 --- a/tests/RequestGeneratorTest.php +++ b/tests/RequestGeneratorTest.php @@ -5,12 +5,9 @@ use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Generators\RequestsGenerator; -use RonasIT\Support\Tests\Support\Request\RequestMockTrait; class RequestGeneratorTest extends TestCase { - use RequestMockTrait; - public function testCreateRequests() { app(RequestsGenerator::class) diff --git a/tests/Support/Request/RequestMockTrait.php b/tests/Support/Request/RequestMockTrait.php deleted file mode 100644 index b0f452ae..00000000 --- a/tests/Support/Request/RequestMockTrait.php +++ /dev/null @@ -1,30 +0,0 @@ - [ - 'Http' => [ - 'Requests' => [ - 'Posts' => [] - ], - ], - 'Services' => [ - 'PostService.php' => ' Date: Thu, 16 Jan 2025 14:43:34 +0600 Subject: [PATCH 6/6] refactor: tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/TestCase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index cdae3411..00e70300 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,7 +6,6 @@ use Illuminate\Foundation\Testing\Concerns\InteractsWithViews; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Event; -use Illuminate\Support\Facades\View; use Illuminate\Support\Str; use Orchestra\Testbench\TestCase as BaseTestCase; use org\bovigo\vfs\vfsStream;