From 964438f6780993ad35fbd66d89fa804e77e542ab Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Sat, 20 Jan 2024 15:27:16 +0400 Subject: [PATCH 1/9] test: add tests for console command. --- tests/CommandTest.php | 57 ++++++++++++++++++++++ tests/Support/Command/CommandMockTrait.php | 42 ++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/CommandTest.php create mode 100644 tests/Support/Command/CommandMockTrait.php diff --git a/tests/CommandTest.php b/tests/CommandTest.php new file mode 100644 index 00000000..0c6e094d --- /dev/null +++ b/tests/CommandTest.php @@ -0,0 +1,57 @@ +app); + $provider->boot(); + } + + public function testCallWithInvalidCrudOption() + { + $this->expectException(UnexpectedValueException::class); + $this->expectErrorMessage('Invalid method T'); + + $this->artisan('make:entity Post --methods=T'); + } + + public function testCallWithMissingModelService() + { + $this->mockConfigurations();; + + $this->expectException(ClassNotExistsException::class); + $this->expectErrorMessage('Cannot create API without entity.'); + + $this->artisan('make:entity Post --only-api'); + } + + public function testCallCommand() + { + $this->mockConfigurations(); + $this->mockFilesystem(); + + $this->artisan('make:entity Post --methods=CRUD') + ->assertSuccessful(); + } + + public function testMakeOnly() + { + $this->mockConfigurations(); + $this->mockFilesystem(); + + $this->artisan('make:entity Post --methods=CRUD --only-repository') + ->assertSuccessful(); + } +} diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php new file mode 100644 index 00000000..c825a9b4 --- /dev/null +++ b/tests/Support/Command/CommandMockTrait.php @@ -0,0 +1,42 @@ + 'entity-generator::service', + 'entity-generator.stubs.service_with_trait' => 'entity-generator::service_with_trait', + 'entity-generator.paths' => [ + 'repositories' => 'app/Repositories', + 'services' => 'app/Services', + 'models' => 'app/Models', + 'translations' => 'lang/en/validation.php' + ] + ]); + } + + public function mockFilesystem(): void + { + $structure = [ + 'app' => [ + 'Models' => [ + 'Post.php' => ' [] + ], + 'config' => [ + 'entity-generator.php' => '' + ] + ]; + + vfsStream::create($structure); + } +} \ No newline at end of file From f594a486a74e158aa685c3105d0036bdd7857fe4 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 14 Jan 2025 18:45:44 +0600 Subject: [PATCH 2/9] feat: add tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/NovaResourceGenerator.php | 3 +- tests/CommandTest.php | 94 ++++++-- tests/Support/Command/CommandMockTrait.php | 164 +++++++++++-- tests/Support/GeneratorMockTrait.php | 3 + tests/Support/Test/Post.php | 5 + tests/fixtures/CommandTest/controller.php | 56 +++++ .../fixtures/CommandTest/create_request.json | 11 + tests/fixtures/CommandTest/create_request.php | 13 + .../fixtures/CommandTest/create_response.json | 12 + tests/fixtures/CommandTest/delete_request.php | 21 ++ tests/fixtures/CommandTest/dump.sql | 3 + tests/fixtures/CommandTest/factory.php | 18 ++ tests/fixtures/CommandTest/get_request.php | 29 +++ tests/fixtures/CommandTest/migration.php | 24 ++ tests/fixtures/CommandTest/model.php | 16 ++ tests/fixtures/CommandTest/nova_dump.php | 3 + tests/fixtures/CommandTest/nova_resource.php | 59 +++++ tests/fixtures/CommandTest/nova_test.php | 227 ++++++++++++++++++ tests/fixtures/CommandTest/repository.php | 17 ++ tests/fixtures/CommandTest/resource.php | 20 ++ .../CommandTest/resource_collection.php | 10 + tests/fixtures/CommandTest/routes.php | 12 + tests/fixtures/CommandTest/search_request.php | 22 ++ tests/fixtures/CommandTest/seeder.php | 15 ++ tests/fixtures/CommandTest/service.php | 29 +++ tests/fixtures/CommandTest/test.php | 126 ++++++++++ .../fixtures/CommandTest/update_request.json | 11 + tests/fixtures/CommandTest/update_request.php | 26 ++ tests/fixtures/CommandTest/validation.php | 134 +++++++++++ 29 files changed, 1145 insertions(+), 38 deletions(-) create mode 100644 tests/fixtures/CommandTest/controller.php create mode 100644 tests/fixtures/CommandTest/create_request.json create mode 100644 tests/fixtures/CommandTest/create_request.php create mode 100644 tests/fixtures/CommandTest/create_response.json create mode 100644 tests/fixtures/CommandTest/delete_request.php create mode 100644 tests/fixtures/CommandTest/dump.sql create mode 100644 tests/fixtures/CommandTest/factory.php create mode 100644 tests/fixtures/CommandTest/get_request.php create mode 100644 tests/fixtures/CommandTest/migration.php create mode 100644 tests/fixtures/CommandTest/model.php create mode 100644 tests/fixtures/CommandTest/nova_dump.php create mode 100644 tests/fixtures/CommandTest/nova_resource.php create mode 100644 tests/fixtures/CommandTest/nova_test.php create mode 100644 tests/fixtures/CommandTest/repository.php create mode 100644 tests/fixtures/CommandTest/resource.php create mode 100644 tests/fixtures/CommandTest/resource_collection.php create mode 100644 tests/fixtures/CommandTest/routes.php create mode 100644 tests/fixtures/CommandTest/search_request.php create mode 100644 tests/fixtures/CommandTest/seeder.php create mode 100644 tests/fixtures/CommandTest/service.php create mode 100644 tests/fixtures/CommandTest/test.php create mode 100644 tests/fixtures/CommandTest/update_request.json create mode 100644 tests/fixtures/CommandTest/update_request.php create mode 100644 tests/fixtures/CommandTest/validation.php diff --git a/src/Generators/NovaResourceGenerator.php b/src/Generators/NovaResourceGenerator.php index ae6a0616..5a730a39 100644 --- a/src/Generators/NovaResourceGenerator.php +++ b/src/Generators/NovaResourceGenerator.php @@ -140,8 +140,9 @@ protected function getFieldsFromCommandLineArguments(): array protected function getFieldsFromDatabase(): array { - $modelClass = "App\\Models\\{$this->model}"; + $modelClass = $this->getModelClass($this->model); $model = app($modelClass); + $columns = DB::connection($model->getConnectionName()) ->getDoctrineSchemaManager() ->listTableColumns($model->getTable()); diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 0c6e094d..a0e1a9bc 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -2,7 +2,7 @@ namespace RonasIT\Support\Tests; -use RonasIT\Support\EntityGeneratorServiceProvider; +use Carbon\Carbon; use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Tests\Support\Command\CommandMockTrait; use UnexpectedValueException; @@ -11,47 +11,101 @@ class CommandTest extends TestCase { use CommandMockTrait; - public function setUp(): void - { - parent::setUp(); - - $provider = new EntityGeneratorServiceProvider($this->app); - $provider->boot(); - } - public function testCallWithInvalidCrudOption() { - $this->expectException(UnexpectedValueException::class); - $this->expectErrorMessage('Invalid method T'); + $this->assertExceptionThrew( + className: UnexpectedValueException::class, + message: 'Invalid method T', + ); $this->artisan('make:entity Post --methods=T'); } public function testCallWithMissingModelService() { - $this->mockConfigurations();; - - $this->expectException(ClassNotExistsException::class); - $this->expectErrorMessage('Cannot create API without entity.'); + $this->assertExceptionThrew( + className: ClassNotExistsException::class, + message: 'Cannot create API without entity.', + ); $this->artisan('make:entity Post --only-api'); } public function testCallCommand() { - $this->mockConfigurations(); + Carbon::setTestNow('2016-10-20 11:05:00'); + $this->mockFilesystem(); + $this->mockGenerator(); + $this->mockGettingModelInstance(); - $this->artisan('make:entity Post --methods=CRUD') + $this + ->artisan('make:entity Post --methods=CRUD') ->assertSuccessful(); + + $this->assertGeneratedFileEquals('migration.php', 'database/migrations/2016_10_20_110500_posts_create_table.php'); + $this->assertGeneratedFileEquals('factory.php', 'database/factories/PostFactory.php'); + $this->assertGeneratedFileEquals('seeder.php', 'database/seeders/PostSeeder.php'); + $this->assertGeneratedFileEquals('model.php', 'app/Models/Post.php'); + $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); + $this->assertGeneratedFileEquals('service.php', 'app/Services/PostService.php'); + $this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php'); + $this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Post/GetPostRequest.php'); + $this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Post/SearchPostsRequest.php'); + $this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Post/UpdatePostRequest.php'); + $this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Post/DeletePostRequest.php'); + $this->assertGeneratedFileEquals('controller.php', 'app/Http/Controllers/PostController.php'); + $this->assertGeneratedFileEquals('resource.php', 'app/Http/Resources/Post/PostResource.php'); + $this->assertGeneratedFileEquals('resource_collection.php', 'app/Http/Resources/Post/PostsCollectionResource.php'); + $this->assertGeneratedFileEquals('routes.php', 'routes/api.php'); + $this->assertGeneratedFileEquals('test.php', 'tests/PostTest.php'); + $this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/PostTest/dump.sql'); + $this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/PostTest/create_post_request.json'); + $this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/PostTest/create_post_response.json'); + $this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/PostTest/update_post_request.json'); + $this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php'); + $this->assertGeneratedFileEquals('nova_resource.php', 'app/Nova/PostResource.php'); + $this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostTest.php'); + $this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostTest/nova_post_dump.sql'); + $this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json'); + $this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json'); + $this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json'); } public function testMakeOnly() { - $this->mockConfigurations(); - $this->mockFilesystem(); + $this->mockFilesystemPostModelExists(); - $this->artisan('make:entity Post --methods=CRUD --only-repository') + $this + ->artisan('make:entity Post --methods=CRUD --only-repository') ->assertSuccessful(); + + $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); + $this->assertFileDoesNotExist('database/migrations/2016_10_20_110500_posts_create_table.php'); + $this->assertFileDoesNotExist('database/factories/PostFactory.php'); + $this->assertFileDoesNotExist('database/seeders/PostSeeder.php'); + $this->assertFileDoesNotExist('app/Models/Post.php'); + $this->assertFileDoesNotExist('app/Services/PostService.php'); + $this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php'); + $this->assertFileDoesNotExist('app/Http/Requests/Post/GetPostRequest.php'); + $this->assertFileDoesNotExist('app/Http/Requests/Post/SearchPostsRequest.php'); + $this->assertFileDoesNotExist('app/Http/Requests/Post/UpdatePostRequest.php'); + $this->assertFileDoesNotExist('app/Http/Requests/Post/DeletePostRequest.php'); + $this->assertFileDoesNotExist('app/Http/Controllers/PostController.php'); + $this->assertFileDoesNotExist('app/Http/Resources/Post/PostResource.php'); + $this->assertFileDoesNotExist('app/Http/Resources/Post/PostsCollectionResource.php'); + $this->assertFileDoesNotExist('routes/api.php'); + $this->assertFileDoesNotExist('tests/PostTest.php'); + $this->assertFileDoesNotExist('tests/fixtures/PostTest/dump.sql'); + $this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_request.json'); + $this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_response.json'); + $this->assertFileDoesNotExist('tests/fixtures/PostTest/update_post_request.json'); + $this->assertFileDoesNotExist('lang/en/validation.php'); + $this->assertFileDoesNotExist('app/Nova/PostResource.php'); + $this->assertFileDoesNotExist('tests/NovaPostTest.php'); + $this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/nova_post_dump.sql'); + $this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/create_post_request.json'); + $this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/create_post_response.json'); + $this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/update_post_request.json'); } } diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php index c825a9b4..c00724e1 100644 --- a/tests/Support/Command/CommandMockTrait.php +++ b/tests/Support/Command/CommandMockTrait.php @@ -2,31 +2,29 @@ namespace RonasIT\Support\Tests\Support\Command; +use Illuminate\Database\Connection; +use Illuminate\Support\Facades\DB; use org\bovigo\vfs\vfsStream; -use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; +use RonasIT\Support\Generators\NovaResourceGenerator; +use RonasIT\Support\Generators\NovaTestGenerator; +use RonasIT\Support\Generators\TestsGenerator; +use RonasIT\Support\Tests\Support\FileSystemMock; +use RonasIT\Support\Tests\Support\GeneratorMockTrait; +use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\SchemaManager; +use Mockery; +use RonasIT\Support\Tests\Support\Test\Post; trait CommandMockTrait { use GeneratorMockTrait; - public function mockConfigurations(): void - { - config([ - 'entity-generator.stubs.service' => 'entity-generator::service', - 'entity-generator.stubs.service_with_trait' => 'entity-generator::service_with_trait', - 'entity-generator.paths' => [ - 'repositories' => 'app/Repositories', - 'services' => 'app/Services', - 'models' => 'app/Models', - 'translations' => 'lang/en/validation.php' - ] - ]); - } - - public function mockFilesystem(): void + public function mockFilesystemPostModelExists(): void { $structure = [ 'app' => [ + 'Http' => [ + 'Controllers' => [], + ], 'Models' => [ 'Post.php' => ' [ 'entity-generator.php' => '' - ] + ], + ]; + + vfsStream::create($structure); + } + + public function mockFilesystem(): void + { + $structure = [ + 'database' => [ + 'migrations' => [], + 'factories' => [], + 'seeders' => [], + ], + 'lang' => [ + 'en' => [], + ], + 'app' => [ + 'Http' => [ + 'Controllers' => [], + 'Resources' => [], + 'Requests' => [], + ], + 'Nova' => [], + 'Models' => [], + 'Repositories' => [], + 'Services' => [], + ], + 'tests' => [ + 'fixtures' => [ + 'PostTest' => [], + ], + ], + 'routes' => [ + 'api.php' => $this->mockPhpFileContent(), + ], + 'config' => [ + 'entity-generator.php' => '', + ], ]; vfsStream::create($structure); } + + public function mockGenerator(): void + { + $this->mockClass(TestsGenerator::class, [ + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + ]); + + $this->mockClass(NovaResourceGenerator::class, [ + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + ]); + + $this->mockClass(NovaTestGenerator::class, [ + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'getModelClass', + arguments: ['Post'], + result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + ), + $this->functionCall( + name: 'loadNovaActions', + result: [], + ), + $this->functionCall( + name: 'loadNovaFields', + result: [], + ), + $this->functionCall( + name: 'loadNovaFilters', + result: [], + ), + ]); + + $this->mockNativeGeneratorFunctions( + $this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Test\Post', true]), + $this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]), + $this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]), + $this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Test\Post', true]), + ); + } + + public function mockGettingModelInstance(): void + { + $connectionMock = Mockery::mock(Connection::class)->makePartial(); + $connectionMock + ->expects('getDoctrineSchemaManager') + ->andReturn(new SchemaManager); + + $mock = Mockery::mock('alias:' . DB::class); + $mock + ->expects('connection') + ->with('pgsql') + ->andReturn($connectionMock); + + $mock->shouldReceive('beginTransaction', 'rollBack'); + + $this->app->instance('App\\Models\\Post', new Post()); + } } \ No newline at end of file diff --git a/tests/Support/GeneratorMockTrait.php b/tests/Support/GeneratorMockTrait.php index 5933ed8d..09ef73bc 100644 --- a/tests/Support/GeneratorMockTrait.php +++ b/tests/Support/GeneratorMockTrait.php @@ -3,9 +3,12 @@ namespace RonasIT\Support\Tests\Support; use Laravel\Nova\NovaServiceProvider; +use RonasIT\Support\Traits\MockTrait; trait GeneratorMockTrait { + use MockTrait; + public function mockNativeGeneratorFunctions(...$functionCalls): void { $this->mockNativeFunction('\RonasIT\Support\Generators', $functionCalls); diff --git a/tests/Support/Test/Post.php b/tests/Support/Test/Post.php index b93b9c6d..5f4498ee 100644 --- a/tests/Support/Test/Post.php +++ b/tests/Support/Test/Post.php @@ -21,6 +21,11 @@ class Post extends Model 'updated_at' ]; + public function getConnectionName(): string + { + return 'pgsql'; + } + public function user() { return $this->belongsTo(User::class); diff --git a/tests/fixtures/CommandTest/controller.php b/tests/fixtures/CommandTest/controller.php new file mode 100644 index 00000000..922be5ab --- /dev/null +++ b/tests/fixtures/CommandTest/controller.php @@ -0,0 +1,56 @@ +onlyValidated(); + + $result = $service->create($data); + + return PostResource::make($result); + } + + public function get(GetPostRequest $request, PostService $service, $id): PostResource + { + $result = $service + ->with($request->input('with', [])) + ->withCount($request->input('with_count', [])) + ->find($id); + + return PostResource::make($result); + } + + public function search(SearchPostsRequest $request, PostService $service): PostsCollectionResource + { + $result = $service->search($request->onlyValidated()); + + return PostsCollectionResource::make($result); + } + + public function update(UpdatePostRequest $request, PostService $service, $id): Response + { + $service->update($id, $request->onlyValidated()); + + return response('', Response::HTTP_NO_CONTENT); + } + + public function delete(DeletePostRequest $request, PostService $service, $id): Response + { + $service->delete($id); + + return response('', Response::HTTP_NO_CONTENT); + } +} diff --git a/tests/fixtures/CommandTest/create_request.json b/tests/fixtures/CommandTest/create_request.json new file mode 100644 index 00000000..4689eebc --- /dev/null +++ b/tests/fixtures/CommandTest/create_request.json @@ -0,0 +1,11 @@ +{ + "title": "some title", + "body": "some body", + "data": { + "title": "1", + "body": "2" + }, + "drafted": false, + "user_id": 1, + "posted_at": "2016-10-20 11:05:00" +} \ No newline at end of file diff --git a/tests/fixtures/CommandTest/create_request.php b/tests/fixtures/CommandTest/create_request.php new file mode 100644 index 00000000..2eaba91c --- /dev/null +++ b/tests/fixtures/CommandTest/create_request.php @@ -0,0 +1,13 @@ +exists($this->route('id'))) { + throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); + } + } +} \ No newline at end of file diff --git a/tests/fixtures/CommandTest/dump.sql b/tests/fixtures/CommandTest/dump.sql new file mode 100644 index 00000000..552c5964 --- /dev/null +++ b/tests/fixtures/CommandTest/dump.sql @@ -0,0 +1,3 @@ +INSERT INTO "posts"(id, title, body, data, drafted, user_id, posted_at, created_at, updated_at) VALUES + (1, 'some title', 'some body', '{"title":"1","body":"2"}', 'false', 1, '2016-10-20 11:05:00', '2016-10-20 11:05:00', '2016-10-20 11:05:00'); + diff --git a/tests/fixtures/CommandTest/factory.php b/tests/fixtures/CommandTest/factory.php new file mode 100644 index 00000000..037aaded --- /dev/null +++ b/tests/fixtures/CommandTest/factory.php @@ -0,0 +1,18 @@ + '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/CommandTest/migration.php b/tests/fixtures/CommandTest/migration.php new file mode 100644 index 00000000..8a219c67 --- /dev/null +++ b/tests/fixtures/CommandTest/migration.php @@ -0,0 +1,24 @@ +increments('id'); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('posts'); + } +}; diff --git a/tests/fixtures/CommandTest/model.php b/tests/fixtures/CommandTest/model.php new file mode 100644 index 00000000..4a643d9b --- /dev/null +++ b/tests/fixtures/CommandTest/model.php @@ -0,0 +1,16 @@ +required() + ->sortable(), + Text::make('Title') + ->required() + ->sortable(), + Text::make('Created At') + ->required() + ->sortable(), + ]; + } + + public function cards(Request $request): array + { + return []; + } + + public function filters(Request $request): array + { + return []; + } + + public function lenses(Request $request): array + { + return []; + } + + public function actions(Request $request): array + { + return []; + } +} diff --git a/tests/fixtures/CommandTest/nova_test.php b/tests/fixtures/CommandTest/nova_test.php new file mode 100644 index 00000000..6a7a0b75 --- /dev/null +++ b/tests/fixtures/CommandTest/nova_test.php @@ -0,0 +1,227 @@ +skipDocumentationCollecting(); + } + + public function testCreate(): void + { + $data = $this->getJsonFixture('create_post_request.json'); + + $response = $this->novaActingAs(self::$user)->novaCreateResourceAPICall(Post::class, $data); + + $response->assertCreated(); + + $this->assertEqualsFixture('create_post_response.json', $response->json()); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture('create_posts_state.json', true); + } + + public function testCreateNoAuth(): void + { + $response = $this->novaCreateResourceAPICall(Post::class); + + $response->assertUnauthorized(); + + self::$postState->assertNotChanged(); + } + + public function testCreateValidationError(): void + { + $response = $this->novaActingAs(self::$user)->novaCreateResourceAPICall(Post::class); + + $response->assertUnprocessable(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture('create_validation_response.json', $response->json(), true); + + self::$postState->assertNotChanged(); + } + + public function testUpdate(): void + { + $data = $this->getJsonFixture('update_post_request.json'); + + $response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall(Post::class, 1, $data); + + $response->assertNoContent(); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture('update_posts_state.json', true); + } + + public function testUpdateNotExists(): void + { + $data = $this->getJsonFixture('update_post_request.json'); + + $response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall(Post::class, 0, $data); + + $response->assertNotFound(); + } + + public function testUpdateNoAuth(): void + { + $response = $this->novaUpdateResourceAPICall(Post::class, 1); + + $response->assertUnauthorized(); + } + + public function testUpdateValidationError(): void + { + $response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall(Post::class, 4); + + $response->assertUnprocessable(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture('update_validation_response.json', $response->json(), true); + } + + public function testGetUpdatableFields(): void + { + $response = $this->novaActingAs(self::$user)->novaGetUpdatableFieldsAPICall(Post::class, 1); + + $response->assertOk(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture('get_updatable_fields_response.json', $response->json(), true); + } + + public function testDelete(): void + { + $response = $this->novaActingAs(self::$user)->novaDeleteResourceAPICall(Post::class, [1, 2]); + + $response->assertOk(); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture('delete_posts_state.json', true); + } + + public function testDeleteNotExists(): void + { + $response = $this->novaActingAs(self::$user)->novaDeleteResourceAPICall(Post::class, [0]); + + $response->assertNotFound(); + } + + public function testDeleteNoAuth(): void + { + $response = $this->novaDeleteResourceAPICall(Post::class, [1, 2]); + + $response->assertUnauthorized(); + } + + public function testGet(): void + { + $response = $this->novaActingAs(self::$user)->novaGetResourceAPICall(Post::class, 1); + + $response->assertOk(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture('get_post_response.json', $response->json(), true); + } + + public function testGetNotExists(): void + { + $response = $this->novaActingAs(self::$user)->novaGetResourceAPICall(Post::class, 0); + + $response->assertNotFound(); + } + + public function testGetNoAuth(): void + { + $response = $this->novaGetResourceAPICall(Post::class, 1); + + $response->assertUnauthorized(); + } + + public function testSearchUnauthorized(): void + { + $response = $this->novaSearchResourceAPICall(Post::class); + + $response->assertUnauthorized(); + } + + public function testGetFieldsVisibleOnCreate(): void + { + $response = $this->novaActingAs(self::$user)->novaGetCreationFieldsAPICall(Post::class); + + $response->assertOk(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture('get_fields_visible_on_create_response.json', $response->json(), true); + } + + public static function getRunPostActionsData(): array + { + return [ + ]; + } + + #[DataProvider('getRunPostActionsData')] + public function testRunPostActions($action, $request, $state): void + { + $response = $this->novaActingAs(self::$user)->novaRunActionAPICall(Post::class, $action, $request); + + $response->assertOk(); + + $this->assertEmpty($response->getContent()); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture($state, true); + } + + public static function getPostActionsData(): array + { + return [ + ]; + } + + #[DataProvider('getPostActionsData')] + public function testGetPostActions(array $resources, string $fixture): void + { + $response = $this->novaActingAs(self::$user)->novaGetActionsAPICall(Post::class, $resources); + + $response->assertOk(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture($fixture, $response->json(), true); + } + + public static function getPostFiltersData(): array + { + return [ + ]; + } + + #[DataProvider('getPostFiltersData')] + public function testFilterPost(array $request, string $fixture): void + { + $response = $this->novaActingAs(self::$user)->novaSearchResourceAPICall(Post::class, $request); + + $response->assertOk(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture($fixture, $response->json(), true); + } +} diff --git a/tests/fixtures/CommandTest/repository.php b/tests/fixtures/CommandTest/repository.php new file mode 100644 index 00000000..1e635191 --- /dev/null +++ b/tests/fixtures/CommandTest/repository.php @@ -0,0 +1,17 @@ +setModel(Post::class); + } +} diff --git a/tests/fixtures/CommandTest/resource.php b/tests/fixtures/CommandTest/resource.php new file mode 100644 index 00000000..8e6f7037 --- /dev/null +++ b/tests/fixtures/CommandTest/resource.php @@ -0,0 +1,20 @@ +group(function () { + Route::post('posts', 'create'); + Route::put('posts/{id}', 'update'); + Route::delete('posts/{id}', 'delete'); + Route::get('posts/{id}', 'get'); + Route::get('posts', 'search'); +}); \ No newline at end of file diff --git a/tests/fixtures/CommandTest/search_request.php b/tests/fixtures/CommandTest/search_request.php new file mode 100644 index 00000000..d905e6b1 --- /dev/null +++ b/tests/fixtures/CommandTest/search_request.php @@ -0,0 +1,22 @@ + 'integer', + 'per_page' => 'integer', + 'order_by' => 'string', + 'desc' => 'boolean', + 'all' => 'boolean', + 'with' => 'array', + 'query' => 'string|nullable', + 'with.*' => 'string', + ]; + } +} \ No newline at end of file diff --git a/tests/fixtures/CommandTest/seeder.php b/tests/fixtures/CommandTest/seeder.php new file mode 100644 index 00000000..9d1c3e1b --- /dev/null +++ b/tests/fixtures/CommandTest/seeder.php @@ -0,0 +1,15 @@ +create(); + + } +} \ No newline at end of file diff --git a/tests/fixtures/CommandTest/service.php b/tests/fixtures/CommandTest/service.php new file mode 100644 index 00000000..5c6caf0b --- /dev/null +++ b/tests/fixtures/CommandTest/service.php @@ -0,0 +1,29 @@ +setRepository(PostRepository::class); + } + + public function search(array $filters = []): LengthAwarePaginator + { + return $this + ->with(Arr::get($filters, 'with', [])) + ->withCount(Arr::get($filters, 'with_count', [])) + ->searchQuery($filters) + ->getSearchResults(); + } +} diff --git a/tests/fixtures/CommandTest/test.php b/tests/fixtures/CommandTest/test.php new file mode 100644 index 00000000..6b71a841 --- /dev/null +++ b/tests/fixtures/CommandTest/test.php @@ -0,0 +1,126 @@ +getJsonFixture('create_post_request.json'); + + $response = $this->json('post', '/posts', $data); + + $response->assertCreated(); + + // TODO: Need to remove last argument after first successful start + $this->assertEqualsFixture('create_post_response.json', $response->json(), true); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture('create_post_state.json', true); + } + + public function testUpdate() + { + $data = $this->getJsonFixture('update_post_request.json'); + + $response = $this->json('put', '/posts/1', $data); + + $response->assertNoContent(); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture('update_post_state.json', true); + } + + public function testUpdateNotExists() + { + $data = $this->getJsonFixture('update_post_request.json'); + + $response = $this->json('put', '/posts/0', $data); + + $response->assertNotFound(); + + self::$postState->assertNotChanged(); + } + + public function testDelete() + { + $response = $this->json('delete', '/posts/1'); + + $response->assertNoContent(); + + // TODO: Need to remove last argument after first successful start + self::$postState->assertChangesEqualsFixture('delete_post_state.json', true); + } + + public function testDeleteNotExists() + { + $response = $this->json('delete', '/posts/0'); + + $response->assertNotFound(); + + self::$postState->assertNotChanged(); + } + + public function testGet() + { + $response = $this->json('get', '/posts/1'); + + $response->assertOk(); + + // TODO: Need to remove after first successful start + $this->exportJson('get_post.json', $response->json()); + + $this->assertEqualsFixture('get_post.json', $response->json()); + } + + public function testGetNotExists() + { + $response = $this->json('get', '/posts/0'); + + $response->assertNotFound(); + } + + public static function getSearchFilters(): array + { + return [ + [ + 'filter' => ['all' => 1], + 'fixture' => 'search_all.json', + ], + [ + 'filter' => [ + 'page' => 2, + 'per_page' => 2, + ], + 'fixture' => 'search_by_page_per_page.json', + ], + ]; + } + + #[DataProvider('getSearchFilters')] + public function testSearch(array $filter, string $fixture) + { + $response = $this->actingAs(self::$user)->json('get', '/posts', $filter); + + $response->assertOk(); + + // TODO: Need to remove after first successful start + $this->exportJson($fixture, $response->json()); + + $this->assertEqualsFixture($fixture, $response->json()); + } + +} diff --git a/tests/fixtures/CommandTest/update_request.json b/tests/fixtures/CommandTest/update_request.json new file mode 100644 index 00000000..4689eebc --- /dev/null +++ b/tests/fixtures/CommandTest/update_request.json @@ -0,0 +1,11 @@ +{ + "title": "some title", + "body": "some body", + "data": { + "title": "1", + "body": "2" + }, + "drafted": false, + "user_id": 1, + "posted_at": "2016-10-20 11:05:00" +} \ No newline at end of file diff --git a/tests/fixtures/CommandTest/update_request.php b/tests/fixtures/CommandTest/update_request.php new file mode 100644 index 00000000..87c9197c --- /dev/null +++ b/tests/fixtures/CommandTest/update_request.php @@ -0,0 +1,26 @@ +exists($this->route('id'))) { + throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); + } + } +} \ No newline at end of file diff --git a/tests/fixtures/CommandTest/validation.php b/tests/fixtures/CommandTest/validation.php new file mode 100644 index 00000000..69dcf685 --- /dev/null +++ b/tests/fixtures/CommandTest/validation.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 d326fe5779ef56dae3c7b91a57ef1a6d4cd76295 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 3 Feb 2025 16:31:51 +0600 Subject: [PATCH 3/9] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- config/entity-generator.php | 2 +- src/Commands/MakeEntityCommand.php | 13 ---- tests/CommandTest.php | 11 +++- tests/Support/Command/CommandMockTrait.php | 63 ++++++------------- .../Support/Command/Factories/PostFactory.php | 26 ++++++++ tests/Support/Command/Models/Post.php | 27 ++++++++ tests/Support/FileSystemMock.php | 13 +++- tests/TranslationGeneratorTest.php | 6 +- tests/fixtures/CommandTest/factory.php | 2 +- tests/fixtures/CommandTest/validation.php | 2 +- 10 files changed, 98 insertions(+), 67 deletions(-) create mode 100644 tests/Support/Command/Factories/PostFactory.php create mode 100644 tests/Support/Command/Models/Post.php diff --git a/config/entity-generator.php b/config/entity-generator.php index 2ecdfe47..c636ef7e 100644 --- a/config/entity-generator.php +++ b/config/entity-generator.php @@ -13,7 +13,7 @@ 'tests' => 'tests', 'routes' => 'routes/api.php', 'factories' => 'database/factories', - 'translations' => 'resources/lang/en/validation.php', + 'translations' => 'lang/en/validation.php', 'resources' => 'app/Http/Resources', 'nova' => 'app/Nova', ], diff --git a/src/Commands/MakeEntityCommand.php b/src/Commands/MakeEntityCommand.php index f300ff5a..6ec5f173 100644 --- a/src/Commands/MakeEntityCommand.php +++ b/src/Commands/MakeEntityCommand.php @@ -208,19 +208,6 @@ protected function outputNewConfig($packageConfigs, $projectConfigs) $newConfig = array_merge($flattenedPackageConfigs, $flattenedProjectConfigs); - $translations = 'lang/en/validation.php'; - $translations = (version_compare(app()->version(), '9', '>=')) ? $translations : "resources/{$translations}"; - - if ($newConfig['paths.translations'] !== $translations) { - $newConfig['paths.translations'] = $translations; - } - - $factories = 'database/factories'; - - if ($newConfig['paths.factories'] !== $factories) { - $newConfig['paths.factories'] = $factories; - } - $differences = array_diff_key($newConfig, $flattenedProjectConfigs); foreach ($differences as $differenceKey => $differenceValue) { diff --git a/tests/CommandTest.php b/tests/CommandTest.php index a0e1a9bc..672406d3 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use RonasIT\Support\Exceptions\ClassNotExistsException; +use RonasIT\Support\Generators\TestsGenerator; use RonasIT\Support\Tests\Support\Command\CommandMockTrait; use UnexpectedValueException; @@ -33,6 +34,14 @@ className: ClassNotExistsException::class, public function testCallCommand() { + config([ + 'entity-generator.paths.factories' => 'RonasIT\Support\Tests\Support\Command\Factories', + ]); + +// $this->mockClass(TestsGenerator::class, [ +// $this->classExistsMethodCall(['factories', 'PostFactory']), +// ]); + Carbon::setTestNow('2016-10-20 11:05:00'); $this->mockFilesystem(); @@ -44,7 +53,7 @@ public function testCallCommand() ->assertSuccessful(); $this->assertGeneratedFileEquals('migration.php', 'database/migrations/2016_10_20_110500_posts_create_table.php'); - $this->assertGeneratedFileEquals('factory.php', 'database/factories/PostFactory.php'); + $this->assertGeneratedFileEquals('factory.php', 'RonasIT/Support/Tests/Support/Command/Factories/PostFactory.php'); $this->assertGeneratedFileEquals('seeder.php', 'database/seeders/PostSeeder.php'); $this->assertGeneratedFileEquals('model.php', 'app/Models/Post.php'); $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php index c00724e1..6692b2aa 100644 --- a/tests/Support/Command/CommandMockTrait.php +++ b/tests/Support/Command/CommandMockTrait.php @@ -8,11 +8,11 @@ use RonasIT\Support\Generators\NovaResourceGenerator; use RonasIT\Support\Generators\NovaTestGenerator; use RonasIT\Support\Generators\TestsGenerator; +use RonasIT\Support\Tests\Support\Command\Models\Post; use RonasIT\Support\Tests\Support\FileSystemMock; use RonasIT\Support\Tests\Support\GeneratorMockTrait; use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\SchemaManager; use Mockery; -use RonasIT\Support\Tests\Support\Test\Post; trait CommandMockTrait { @@ -40,40 +40,13 @@ public function mockFilesystemPostModelExists(): void public function mockFilesystem(): void { - $structure = [ - 'database' => [ - 'migrations' => [], - 'factories' => [], - 'seeders' => [], - ], - 'lang' => [ - 'en' => [], - ], - 'app' => [ - 'Http' => [ - 'Controllers' => [], - 'Resources' => [], - 'Requests' => [], - ], - 'Nova' => [], - 'Models' => [], - 'Repositories' => [], - 'Services' => [], - ], - 'tests' => [ - 'fixtures' => [ - 'PostTest' => [], - ], - ], - 'routes' => [ - 'api.php' => $this->mockPhpFileContent(), - ], - 'config' => [ - 'entity-generator.php' => '', - ], - ]; + $fileSystemMock = new FileSystemMock; - vfsStream::create($structure); + $fileSystemMock->routes = [ 'api.php' => $this->mockPhpFileContent()]; + $fileSystemMock->config = ['entity-generator.php' => '']; + $fileSystemMock->translations = []; + + $fileSystemMock->setStructure(); } public function mockGenerator(): void @@ -82,22 +55,22 @@ public function mockGenerator(): void $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), ]); @@ -105,7 +78,7 @@ public function mockGenerator(): void $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), ]); @@ -113,22 +86,22 @@ public function mockGenerator(): void $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'getModelClass', arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post', + result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', ), $this->functionCall( name: 'loadNovaActions', @@ -145,10 +118,10 @@ public function mockGenerator(): void ]); $this->mockNativeGeneratorFunctions( - $this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Test\Post', true]), + $this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]), $this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]), $this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]), - $this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Test\Post', true]), + $this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]), ); } diff --git a/tests/Support/Command/Factories/PostFactory.php b/tests/Support/Command/Factories/PostFactory.php new file mode 100644 index 00000000..762847cd --- /dev/null +++ b/tests/Support/Command/Factories/PostFactory.php @@ -0,0 +1,26 @@ + 'some title', + 'body' => 'some body', + 'posted_at' => Carbon::now(), + 'drafted' => false, + 'data' => [ + 'title' => '1', + 'body' => '2', + ], + ]; + } +} diff --git a/tests/Support/Command/Models/Post.php b/tests/Support/Command/Models/Post.php new file mode 100644 index 00000000..a6fd2c06 --- /dev/null +++ b/tests/Support/Command/Models/Post.php @@ -0,0 +1,27 @@ +translations)) { - $structure['resources']['lang']['en'] = []; + $structure['lang']['en'] = []; foreach ($this->translations as $translation => $content) { - $structure['resources']['lang']['en'][$translation] = $content; + $structure['lang']['en'][$translation] = $content; + } + } + + if (!is_null($this->config)) { + $structure['config'] = []; + + foreach ($this->config as $config => $content) { + $structure['config'][$config] = $content; } } diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 8e9b6152..c6e077af 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -25,11 +25,11 @@ public function testCreate() ->setModel('Post') ->generate(); - $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); + $this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php'); $this->assertEventPushed( className: SuccessCreateMessage::class, - message: 'Created a new Translations dump on path: vfs://root/resources/lang/en/validation.php', + message: 'Created a new Translations dump on path: vfs://root/lang/en/validation.php', ); } @@ -57,7 +57,7 @@ public function testAppendNotFoundException() ->setModel('Post') ->generate(); - $this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php'); + $this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php'); Event::assertNothingDispatched(); } diff --git a/tests/fixtures/CommandTest/factory.php b/tests/fixtures/CommandTest/factory.php index 037aaded..6bdac4c3 100644 --- a/tests/fixtures/CommandTest/factory.php +++ b/tests/fixtures/CommandTest/factory.php @@ -1,6 +1,6 @@ [], - /* + /* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- From 685d4c24e12fdd24ed1701123bb9703aa30aaa0f Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 3 Feb 2025 17:47:59 +0600 Subject: [PATCH 4/9] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/CommandTest.php | 10 +-- tests/Support/Command/CommandMockTrait.php | 74 ++----------------- tests/fixtures/CommandTest/factory.php | 2 +- .../CommandTest/make_only_repository.php | 17 +++++ tests/fixtures/CommandTest/model.php | 2 +- tests/fixtures/CommandTest/nova_resource.php | 2 +- tests/fixtures/CommandTest/repository.php | 2 +- tests/fixtures/CommandTest/resource.php | 2 +- tests/fixtures/CommandTest/seeder.php | 2 +- tests/fixtures/CommandTest/test.php | 2 +- 10 files changed, 32 insertions(+), 83 deletions(-) create mode 100644 tests/fixtures/CommandTest/make_only_repository.php diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 672406d3..781ae91a 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,7 +4,6 @@ use Carbon\Carbon; use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Generators\TestsGenerator; use RonasIT\Support\Tests\Support\Command\CommandMockTrait; use UnexpectedValueException; @@ -35,13 +34,10 @@ className: ClassNotExistsException::class, public function testCallCommand() { config([ + 'entity-generator.paths.models' => 'RonasIT\Support\Tests\Support\Command\Models', 'entity-generator.paths.factories' => 'RonasIT\Support\Tests\Support\Command\Factories', ]); -// $this->mockClass(TestsGenerator::class, [ -// $this->classExistsMethodCall(['factories', 'PostFactory']), -// ]); - Carbon::setTestNow('2016-10-20 11:05:00'); $this->mockFilesystem(); @@ -55,7 +51,7 @@ public function testCallCommand() $this->assertGeneratedFileEquals('migration.php', 'database/migrations/2016_10_20_110500_posts_create_table.php'); $this->assertGeneratedFileEquals('factory.php', 'RonasIT/Support/Tests/Support/Command/Factories/PostFactory.php'); $this->assertGeneratedFileEquals('seeder.php', 'database/seeders/PostSeeder.php'); - $this->assertGeneratedFileEquals('model.php', 'app/Models/Post.php'); + $this->assertGeneratedFileEquals('model.php', 'RonasIT/Support/Tests/Support/Command/Models/Post.php'); $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); $this->assertGeneratedFileEquals('service.php', 'app/Services/PostService.php'); $this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php'); @@ -89,7 +85,7 @@ public function testMakeOnly() ->artisan('make:entity Post --methods=CRUD --only-repository') ->assertSuccessful(); - $this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php'); + $this->assertGeneratedFileEquals('make_only_repository.php', 'app/Repositories/PostRepository.php'); $this->assertFileDoesNotExist('database/migrations/2016_10_20_110500_posts_create_table.php'); $this->assertFileDoesNotExist('database/factories/PostFactory.php'); $this->assertFileDoesNotExist('database/seeders/PostSeeder.php'); diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php index 6692b2aa..5a03bbbc 100644 --- a/tests/Support/Command/CommandMockTrait.php +++ b/tests/Support/Command/CommandMockTrait.php @@ -4,10 +4,7 @@ use Illuminate\Database\Connection; use Illuminate\Support\Facades\DB; -use org\bovigo\vfs\vfsStream; -use RonasIT\Support\Generators\NovaResourceGenerator; use RonasIT\Support\Generators\NovaTestGenerator; -use RonasIT\Support\Generators\TestsGenerator; use RonasIT\Support\Tests\Support\Command\Models\Post; use RonasIT\Support\Tests\Support\FileSystemMock; use RonasIT\Support\Tests\Support\GeneratorMockTrait; @@ -20,22 +17,12 @@ trait CommandMockTrait public function mockFilesystemPostModelExists(): void { - $structure = [ - 'app' => [ - 'Http' => [ - 'Controllers' => [], - ], - 'Models' => [ - 'Post.php' => ' [] - ], - 'config' => [ - 'entity-generator.php' => '' - ], - ]; + $fileSystemMock = new FileSystemMock; + + $fileSystemMock->models = ['Post.php' => $this->mockPhpFileContent()]; + $fileSystemMock->config = ['entity-generator.php' => '']; - vfsStream::create($structure); + $fileSystemMock->setStructure(); } public function mockFilesystem(): void @@ -51,58 +38,7 @@ public function mockFilesystem(): void public function mockGenerator(): void { - $this->mockClass(TestsGenerator::class, [ - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - ]); - - $this->mockClass(NovaResourceGenerator::class, [ - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - ]); - $this->mockClass(NovaTestGenerator::class, [ - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), - $this->functionCall( - name: 'getModelClass', - arguments: ['Post'], - result: 'RonasIT\\Support\\Tests\\Support\\Command\\Models\\Post', - ), $this->functionCall( name: 'loadNovaActions', result: [], diff --git a/tests/fixtures/CommandTest/factory.php b/tests/fixtures/CommandTest/factory.php index 6bdac4c3..9b31f8a5 100644 --- a/tests/fixtures/CommandTest/factory.php +++ b/tests/fixtures/CommandTest/factory.php @@ -4,7 +4,7 @@ use Faker\Generator as Faker; use Illuminate\Database\Eloquent\Factories\Factory; -use App\Models\Post; +use RonasIT\Support\Tests\Support\Command\Models\Post; class PostFactory extends Factory { diff --git a/tests/fixtures/CommandTest/make_only_repository.php b/tests/fixtures/CommandTest/make_only_repository.php new file mode 100644 index 00000000..1e635191 --- /dev/null +++ b/tests/fixtures/CommandTest/make_only_repository.php @@ -0,0 +1,17 @@ +setModel(Post::class); + } +} diff --git a/tests/fixtures/CommandTest/model.php b/tests/fixtures/CommandTest/model.php index 4a643d9b..4dce8795 100644 --- a/tests/fixtures/CommandTest/model.php +++ b/tests/fixtures/CommandTest/model.php @@ -1,6 +1,6 @@ Date: Mon, 3 Feb 2025 17:50:50 +0600 Subject: [PATCH 5/9] feat: remove test refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/ServiceProviderTest.php | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 tests/ServiceProviderTest.php diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php deleted file mode 100644 index 17a37a64..00000000 --- a/tests/ServiceProviderTest.php +++ /dev/null @@ -1,29 +0,0 @@ -app); - $provider->boot(); - - $finder = view()->getFinder(); - - $this->assertArrayHasKey('make:entity', Artisan::all()); - - $this->assertTrue(in_array( - needle: '/app/vendor/orchestra/testbench-core/laravel/resources/views', - haystack: $finder->getPaths(), - )); - - $this->assertEquals( - expected: ['/app/src/../config/entity-generator.php' => 'vfs://root/config/entity-generator.php'], - actual: EntityGeneratorServiceProvider::$publishes['RonasIT\Support\EntityGeneratorServiceProvider'], - ); - } -} From 0843ed0e9aa224073e46607c0d6b55115de4d2b5 Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 5 Feb 2025 16:11:53 +0600 Subject: [PATCH 6/9] fix: test refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/fixtures/CommandTest/seeder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/CommandTest/seeder.php b/tests/fixtures/CommandTest/seeder.php index f21feda7..79314eae 100644 --- a/tests/fixtures/CommandTest/seeder.php +++ b/tests/fixtures/CommandTest/seeder.php @@ -3,13 +3,13 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; -use RonasIT\Support\Tests\Support\Command\Models\Post; +use RonasIT\Support\Tests\Support\Command\Factories\PostFactory; class PostSeeder extends Seeder { public function run() { - Post::factory()->create(); + PostFactory::new()->create(); } } \ No newline at end of file From dd8b074b4b57bd0ec4d12add590a4eac4cef5464 Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 6 Feb 2025 15:50:07 +0600 Subject: [PATCH 7/9] Update tests/Support/Command/CommandMockTrait.php --- tests/Support/Command/CommandMockTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php index 5a03bbbc..c1c7b0d6 100644 --- a/tests/Support/Command/CommandMockTrait.php +++ b/tests/Support/Command/CommandMockTrait.php @@ -17,7 +17,7 @@ trait CommandMockTrait public function mockFilesystemPostModelExists(): void { - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->models = ['Post.php' => $this->mockPhpFileContent()]; $fileSystemMock->config = ['entity-generator.php' => '']; From c393d3cbff487e8cf2d06a7fed0ac079ae764a96 Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 6 Feb 2025 15:50:16 +0600 Subject: [PATCH 8/9] Update tests/Support/Command/CommandMockTrait.php --- tests/Support/Command/CommandMockTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php index c1c7b0d6..17746c39 100644 --- a/tests/Support/Command/CommandMockTrait.php +++ b/tests/Support/Command/CommandMockTrait.php @@ -27,7 +27,7 @@ public function mockFilesystemPostModelExists(): void public function mockFilesystem(): void { - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->routes = [ 'api.php' => $this->mockPhpFileContent()]; $fileSystemMock->config = ['entity-generator.php' => '']; From 8e92bfb54fc255762e48929eb861b212d968723c Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 6 Feb 2025 15:50:35 +0600 Subject: [PATCH 9/9] Update tests/Support/Command/CommandMockTrait.php --- tests/Support/Command/CommandMockTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/Command/CommandMockTrait.php b/tests/Support/Command/CommandMockTrait.php index 17746c39..b1c94412 100644 --- a/tests/Support/Command/CommandMockTrait.php +++ b/tests/Support/Command/CommandMockTrait.php @@ -66,7 +66,7 @@ public function mockGettingModelInstance(): void $connectionMock = Mockery::mock(Connection::class)->makePartial(); $connectionMock ->expects('getDoctrineSchemaManager') - ->andReturn(new SchemaManager); + ->andReturn(new SchemaManager()); $mock = Mockery::mock('alias:' . DB::class); $mock