From 8c7116b39f8928c5a51835f248fbf237c28d4bc1 Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Wed, 20 Dec 2023 23:14:21 +0400 Subject: [PATCH 01/14] test: add model generation test. --- src/Generators/ModelGenerator.php | 6 +- stubs/relation.blade.php | 2 +- tests/ControllerGeneratorTest.php | 12 ---- tests/FactoryGeneratorTest.php | 12 ---- tests/MigrationGeneratorTest.php | 12 ---- tests/ModelGeneratorTest.php | 71 +++++++++++++++++++ tests/NovaResourceGeneratorTest.php | 12 ---- tests/NovaTestGeneratorTest.php | 12 ---- .../Controller/ControllerMockTrait.php | 3 +- tests/Support/Factory/FactoryMockTrait.php | 3 +- tests/Support/Model/ModelMockTrait.php | 64 +++++++++++++++++ tests/Support/Model/RelationModelMock.php | 11 +++ .../NovaResource/NovaResourceMockTrait.php | 3 +- tests/Support/NovaTest/NovaTestMockTrait.php | 3 +- tests/Support/Shared/GeneratorMockTrait.php | 3 + tests/TestCase.php | 12 ++++ .../comment_relation_model.php | 16 +++++ .../fixtures/ModelGeneratorTest/new_model.php | 31 ++++++++ .../user_relation_model.php | 16 +++++ 19 files changed, 232 insertions(+), 72 deletions(-) create mode 100644 tests/ModelGeneratorTest.php create mode 100644 tests/Support/Model/ModelMockTrait.php create mode 100644 tests/Support/Model/RelationModelMock.php create mode 100644 tests/fixtures/ModelGeneratorTest/comment_relation_model.php create mode 100644 tests/fixtures/ModelGeneratorTest/new_model.php create mode 100644 tests/fixtures/ModelGeneratorTest/user_relation_model.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 2de0708c..c8c49fcd 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -58,8 +58,8 @@ public function prepareRelatedModels(): void if (!$this->classExists('models', $relation)) { $this->throwFailureException( ClassNotExistsException::class, - "Cannot create {$relation} Model cause {$relation} Model does not exists.", - "Create a {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'." + "Cannot create {$this->model} Model cause relation model {$relation} does not exist.", + "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'." ); } @@ -115,7 +115,7 @@ protected function getCasts($fields): array $result = []; foreach ($fields as $fieldType => $names) { - if (empty($casts[$fieldType])) { + if (!array_key_exists($fieldType, $casts)) { continue; } diff --git a/stubs/relation.blade.php b/stubs/relation.blade.php index 72fec665..7ad7c409 100644 --- a/stubs/relation.blade.php +++ b/stubs/relation.blade.php @@ -1,4 +1,4 @@ public function {{$name}}() { return $this->{{$type}}({{$entity}}::class); - } + } \ No newline at end of file diff --git a/tests/ControllerGeneratorTest.php b/tests/ControllerGeneratorTest.php index ca0bf9c6..cdbe876c 100644 --- a/tests/ControllerGeneratorTest.php +++ b/tests/ControllerGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use Illuminate\Contracts\Filesystem\FileNotFoundException; -use org\bovigo\vfs\vfsStream; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Exceptions\ClassAlreadyExistsException; use RonasIT\Support\Exceptions\ClassNotExistsException; @@ -14,17 +13,6 @@ class ControllerGeneratorTest extends TestCase { use ControllerMockTrait; - public function setUp(): void - { - parent::setUp(); - - vfsStream::setup(); - - $this->generatedFileBasePath = vfsStream::url('root'); - - $this->app->setBasePath($this->generatedFileBasePath); - } - public function testControllerAlreadyExists() { $this->getFiredEvents([SuccessCreateMessage::class]); diff --git a/tests/FactoryGeneratorTest.php b/tests/FactoryGeneratorTest.php index bcc56b6c..701877a0 100644 --- a/tests/FactoryGeneratorTest.php +++ b/tests/FactoryGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use Illuminate\View\ViewException; -use org\bovigo\vfs\vfsStream; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Exceptions\ClassAlreadyExistsException; use RonasIT\Support\Exceptions\ClassNotExistsException; @@ -16,17 +15,6 @@ class FactoryGeneratorTest extends TestCase { use FactoryMockTrait; - public function setUp(): void - { - parent::setUp(); - - vfsStream::setup(); - - $this->generatedFileBasePath = vfsStream::url('root'); - - $this->app->setBasePath($this->generatedFileBasePath); - } - public function testModelNotExists() { $this->getFiredEvents([SuccessCreateMessage::class]); diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index de33a539..42990e53 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use Illuminate\Support\Carbon; -use org\bovigo\vfs\vfsStream; use ReflectionClass; use RonasIT\Support\Exceptions\UnknownFieldTypeException; use RonasIT\Support\Generators\MigrationGenerator; @@ -13,17 +12,6 @@ class MigrationGeneratorTest extends TestCase { use MigrationMockTrait; - public function setUp(): void - { - parent::setUp(); - - vfsStream::setup(); - - $this->generatedFileBasePath = vfsStream::url('root'); - - $this->app->setBasePath($this->generatedFileBasePath); - } - public function testSetUnknownFieldType() { $this->mockViewsNamespace(); diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php new file mode 100644 index 00000000..76b63409 --- /dev/null +++ b/tests/ModelGeneratorTest.php @@ -0,0 +1,71 @@ +mockGeneratorForExistingModel(); + + $this->expectException(ClassAlreadyExistsException::class); + $this->expectErrorMessage('Cannot create Post Model cause Post Model already exists. Remove Post Model.'); + + app(ModelGenerator::class) + ->setModel('Post') + ->generate(); + } + + public function testRelationModelMissing() + { + $this->mockGeneratorForMissingRelationModel(); + + $this->expectException(ClassNotExistsException::class); + $this->expectErrorMessage("Cannot create Post Model cause relation model Comment does not exist. " + . "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'."); + + app(ModelGenerator::class) + ->setModel('Post') + ->setRelations([ + 'hasOne' => ['Comment'], + 'hasMany' => [], + 'belongsTo' => [], + 'belongsToMany' => [], + ]) + ->generate(); + } + + public function testCreateModel() + { + $this->setupConfigurations(); + $this->mockViewsNamespace(); + $this->mockFilesystem(); + + app(ModelGenerator::class) + ->setModel('Post') + ->setfields([ + 'integer-required' => ['media_id'], + 'boolean-required' => ['is_published'], + ]) + ->setRelations([ + 'hasOne' => ['Comment'], + 'hasMany' => ['User'], + 'belongsTo' => [], + 'belongsToMany' => [], + ]) + ->generate(); + + $this->rollbackToDefaultBasePath(); + + $this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php'); + $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/Comment.php'); + $this->assertGeneratedFileEquals('user_relation_model.php', 'app/Models/User.php'); + } +} diff --git a/tests/NovaResourceGeneratorTest.php b/tests/NovaResourceGeneratorTest.php index a6f16ae7..9999d599 100644 --- a/tests/NovaResourceGeneratorTest.php +++ b/tests/NovaResourceGeneratorTest.php @@ -6,7 +6,6 @@ use Doctrine\DBAL\Types\DateTimeType; use Doctrine\DBAL\Types\IntegerType; use Doctrine\DBAL\Types\StringType; -use org\bovigo\vfs\vfsStream; use ReflectionClass; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Exceptions\ClassAlreadyExistsException; @@ -19,17 +18,6 @@ class NovaResourceGeneratorTest extends TestCase { use NovaResourceMockTrait; - public function setUp(): void - { - parent::setUp(); - - vfsStream::setup(); - - $this->generatedFileBasePath = vfsStream::url('root'); - - $this->app->setBasePath($this->generatedFileBasePath); - } - public function testCreateWithMissingNovaPackage() { $this->expectsEvents([SuccessCreateMessage::class]); diff --git a/tests/NovaTestGeneratorTest.php b/tests/NovaTestGeneratorTest.php index 0c0712fb..1edc48bf 100644 --- a/tests/NovaTestGeneratorTest.php +++ b/tests/NovaTestGeneratorTest.php @@ -2,7 +2,6 @@ namespace RonasIT\Support\Tests; -use org\bovigo\vfs\vfsStream; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Exceptions\ClassAlreadyExistsException; use RonasIT\Support\Exceptions\ClassNotExistsException; @@ -13,17 +12,6 @@ class NovaTestGeneratorTest extends TestCase { use NovaTestMockTrait; - public function setUp(): void - { - parent::setUp(); - - vfsStream::setup(); - - $this->generatedFileBasePath = vfsStream::url('root'); - - $this->app->setBasePath($this->generatedFileBasePath); - } - public function testCreateNovaTestsResourceNotExists() { $mock = $this->mockClassExistsFunction(); diff --git a/tests/Support/Controller/ControllerMockTrait.php b/tests/Support/Controller/ControllerMockTrait.php index 7a22ae06..05426655 100644 --- a/tests/Support/Controller/ControllerMockTrait.php +++ b/tests/Support/Controller/ControllerMockTrait.php @@ -5,11 +5,10 @@ use org\bovigo\vfs\vfsStream; use RonasIT\Support\Generators\ControllerGenerator; use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; -use RonasIT\Support\Traits\MockClassTrait; trait ControllerMockTrait { - use GeneratorMockTrait, MockClassTrait; + use GeneratorMockTrait; public function mockControllerGeneratorForExistingController(): void { diff --git a/tests/Support/Factory/FactoryMockTrait.php b/tests/Support/Factory/FactoryMockTrait.php index 32e170d7..ede9f709 100644 --- a/tests/Support/Factory/FactoryMockTrait.php +++ b/tests/Support/Factory/FactoryMockTrait.php @@ -6,11 +6,10 @@ use ReflectionClass; use RonasIT\Support\Generators\FactoryGenerator; use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; -use RonasIT\Support\Traits\MockClassTrait; trait FactoryMockTrait { - use GeneratorMockTrait, MockClassTrait; + use GeneratorMockTrait; public function getFactoryGeneratorMockForMissingModel(): void { diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php new file mode 100644 index 00000000..bfddb974 --- /dev/null +++ b/tests/Support/Model/ModelMockTrait.php @@ -0,0 +1,64 @@ +mockClass(ModelGenerator::class, [ + [ + 'method' => 'classExists', + 'arguments' => ['models', 'Post'], + 'result' => true + ] + ]); + } + + public function mockGeneratorForMissingRelationModel() + { + $this->mockClass(ModelGenerator::class, [ + [ + 'method' => 'classExists', + 'arguments' => ['models', 'Post'], + 'result' => false + ], + [ + 'method' => 'classExists', + 'arguments' => ['models', 'Comment'], + 'result' => false + ] + ]); + } + + public function setupConfigurations(): void + { + config([ + 'entity-generator.stubs.model' => 'entity-generator::model', + 'entity-generator.stubs.relation' => 'entity-generator::relation', + 'entity-generator.paths' => [ + 'models' => 'app/Models', + ] + ]); + } + + public function mockFilesystem(): void + { + $structure = [ + 'app' => [ + 'Models' => [ + 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php'), + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php') + ] + ], + ]; + + vfsStream::create($structure); + } +} diff --git a/tests/Support/Model/RelationModelMock.php b/tests/Support/Model/RelationModelMock.php new file mode 100644 index 00000000..f8cf9a4f --- /dev/null +++ b/tests/Support/Model/RelationModelMock.php @@ -0,0 +1,11 @@ +getMockBuilder($className); diff --git a/tests/TestCase.php b/tests/TestCase.php index 75530d12..a262382c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\Concerns\InteractsWithViews; use Illuminate\Support\Str; use Orchestra\Testbench\TestCase as BaseTestCase; +use org\bovigo\vfs\vfsStream; use phpmock\Mock; use RonasIT\Support\Traits\FixturesTrait; @@ -16,6 +17,17 @@ class TestCase extends BaseTestCase protected $globalExportMode = false; protected $generatedFileBasePath; + public function setUp(): void + { + parent::setUp(); + + vfsStream::setup(); + + $this->generatedFileBasePath = vfsStream::url('root'); + + $this->app->setBasePath($this->generatedFileBasePath); + } + public function tearDown(): void { parent::tearDown(); diff --git a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php new file mode 100644 index 00000000..da860504 --- /dev/null +++ b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php @@ -0,0 +1,16 @@ +belongsTo(Post::class); + } +} diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php new file mode 100644 index 00000000..c62efefd --- /dev/null +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -0,0 +1,31 @@ +hasOne(Comment::class); + } + public function users() + { + return $this->hasMany(User::class); + } + protected $casts = [ + 'is_published' => 'boolean', + ]; +} \ No newline at end of file diff --git a/tests/fixtures/ModelGeneratorTest/user_relation_model.php b/tests/fixtures/ModelGeneratorTest/user_relation_model.php new file mode 100644 index 00000000..da860504 --- /dev/null +++ b/tests/fixtures/ModelGeneratorTest/user_relation_model.php @@ -0,0 +1,16 @@ +belongsTo(Post::class); + } +} From d2e27c04163ba7c10f913e823e2468cf43451c14 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 20 Nov 2024 16:21:27 +0500 Subject: [PATCH 02/14] chore: add migration generator tests --- src/Generators/ModelGenerator.php | 24 +++++++++---------- tests/ModelGeneratorTest.php | 12 ++++------ tests/Support/Model/ModelMockTrait.php | 8 +++---- .../comment_relation_model.php | 5 ---- .../user_relation_model.php | 5 ---- 5 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index c8c49fcd..ae847d83 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -10,18 +10,18 @@ class ModelGenerator extends EntityGenerator { - CONST PLURAL_NUMBER_REQUIRED = [ + protected const array PLURAL_NUMBER_REQUIRED = [ 'belongsToMany', - 'hasMany' + 'hasMany', ]; public function generate(): void { if ($this->classExists('models', $this->model)) { $this->throwFailureException( - ClassAlreadyExistsException::class, - "Cannot create {$this->model} Model cause {$this->model} Model already exists.", - "Remove {$this->model} Model." + exceptionClass: ClassAlreadyExistsException::class, + failureMessage: "Cannot create {$this->model} Model cause {$this->model} Model already exists.", + recommendedMessage: "Remove {$this->model} Model.", ); } @@ -40,7 +40,7 @@ protected function getNewModelContent(): string 'fields' => Arr::collapse($this->fields), 'relations' => $this->prepareRelations(), 'casts' => $this->getCasts($this->fields), - 'namespace' => $this->getOrCreateNamespace('models') + 'namespace' => $this->getOrCreateNamespace('models'), ]); } @@ -68,7 +68,7 @@ public function prepareRelatedModels(): void $newRelation = $this->getStub('relation', [ 'name' => $this->getRelationName($this->model, $types[$type]), 'type' => $types[$type], - 'entity' => $this->model + 'entity' => $this->model, ]); $fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content); @@ -78,9 +78,9 @@ public function prepareRelatedModels(): void } } - public function getModelContent($model): string + public function getModelContent(string $model): string { - $modelPath = base_path($this->paths['models'] . "/{$model}.php"); + $modelPath = base_path("{$this->paths['models']}/{$model}.php"); return file_get_contents($modelPath); } @@ -95,7 +95,7 @@ public function prepareRelations(): array $result[] = [ 'name' => $this->getRelationName($relation, $type), 'type' => $type, - 'entity' => $relation + 'entity' => $relation, ]; } } @@ -104,7 +104,7 @@ public function prepareRelations(): array return $result; } - protected function getCasts($fields): array + protected function getCasts(array $fields): array { $casts = [ 'boolean-required' => 'boolean', @@ -127,7 +127,7 @@ protected function getCasts($fields): array return $result; } - private function getRelationName($relation, $type): string + private function getRelationName(string $relation, string $type): string { $relationName = Str::snake($relation); diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index 76b63409..35a3c9fe 100644 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -6,17 +6,18 @@ use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Generators\ModelGenerator; use RonasIT\Support\Tests\Support\Model\ModelMockTrait; +use RonasIT\Support\Traits\MockTrait; class ModelGeneratorTest extends TestCase { - use ModelMockTrait; + use ModelMockTrait, MockTrait; public function testModelAlreadyExists() { $this->mockGeneratorForExistingModel(); $this->expectException(ClassAlreadyExistsException::class); - $this->expectErrorMessage('Cannot create Post Model cause Post Model already exists. Remove Post Model.'); + $this->expectExceptionMessage('Cannot create Post Model cause Post Model already exists. Remove Post Model.'); app(ModelGenerator::class) ->setModel('Post') @@ -25,10 +26,8 @@ public function testModelAlreadyExists() public function testRelationModelMissing() { - $this->mockGeneratorForMissingRelationModel(); - $this->expectException(ClassNotExistsException::class); - $this->expectErrorMessage("Cannot create Post Model cause relation model Comment does not exist. " + $this->expectExceptionMessage("Cannot create Post Model cause relation model Comment does not exist. " . "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'."); app(ModelGenerator::class) @@ -45,7 +44,6 @@ public function testRelationModelMissing() public function testCreateModel() { $this->setupConfigurations(); - $this->mockViewsNamespace(); $this->mockFilesystem(); app(ModelGenerator::class) @@ -62,8 +60,6 @@ public function testCreateModel() ]) ->generate(); - $this->rollbackToDefaultBasePath(); - $this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php'); $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/Comment.php'); $this->assertGeneratedFileEquals('user_relation_model.php', 'app/Models/User.php'); diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index bfddb974..a4bc3f8d 100644 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -4,7 +4,7 @@ use org\bovigo\vfs\vfsStream; use RonasIT\Support\Generators\ModelGenerator; -use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; +use RonasIT\Support\Tests\Support\GeneratorMockTrait; trait ModelMockTrait { @@ -14,7 +14,7 @@ public function mockGeneratorForExistingModel() { $this->mockClass(ModelGenerator::class, [ [ - 'method' => 'classExists', + 'function' => 'classExists', 'arguments' => ['models', 'Post'], 'result' => true ] @@ -25,12 +25,12 @@ public function mockGeneratorForMissingRelationModel() { $this->mockClass(ModelGenerator::class, [ [ - 'method' => 'classExists', + 'function' => 'classExists', 'arguments' => ['models', 'Post'], 'result' => false ], [ - 'method' => 'classExists', + 'function' => 'classExists', 'arguments' => ['models', 'Comment'], 'result' => false ] diff --git a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php index da860504..f8cf9a4f 100644 --- a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php @@ -8,9 +8,4 @@ public function some_relation() { } - - public function post() - { - return $this->belongsTo(Post::class); - } } diff --git a/tests/fixtures/ModelGeneratorTest/user_relation_model.php b/tests/fixtures/ModelGeneratorTest/user_relation_model.php index da860504..f8cf9a4f 100644 --- a/tests/fixtures/ModelGeneratorTest/user_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/user_relation_model.php @@ -8,9 +8,4 @@ public function some_relation() { } - - public function post() - { - return $this->belongsTo(Post::class); - } } From fe2e1f39e44adaa9c24c6171c8cd8dc4b70a3e17 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 9 Dec 2024 09:14:02 +0600 Subject: [PATCH 03/14] feat: fix tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/fixtures/ModelGeneratorTest/comment_relation_model.php | 5 +++++ tests/fixtures/ModelGeneratorTest/user_relation_model.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php index f8cf9a4f..da860504 100644 --- a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php @@ -8,4 +8,9 @@ public function some_relation() { } + + public function post() + { + return $this->belongsTo(Post::class); + } } diff --git a/tests/fixtures/ModelGeneratorTest/user_relation_model.php b/tests/fixtures/ModelGeneratorTest/user_relation_model.php index f8cf9a4f..da860504 100644 --- a/tests/fixtures/ModelGeneratorTest/user_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/user_relation_model.php @@ -8,4 +8,9 @@ public function some_relation() { } + + public function post() + { + return $this->belongsTo(Post::class); + } } From 14abfa0a230905bf2e0d044cc5eaf23f4d8d2ff6 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 17 Dec 2024 17:27:12 +0600 Subject: [PATCH 04/14] feat: change test refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/fixtures/ModelGeneratorTest/new_model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php index c62efefd..80f1e297 100644 --- a/tests/fixtures/ModelGeneratorTest/new_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -8,7 +8,7 @@ class Post extends Model { - use ModelTrait, HasFactory; + use HasFactory, ModelTrait; protected $fillable = [ 'media_id', From 3ae2cdcd9836b561aa7e601c00a58c370c82f1e2 Mon Sep 17 00:00:00 2001 From: roman Date: Thu, 19 Dec 2024 17:52:54 +0600 Subject: [PATCH 05/14] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/ModelGeneratorTest.php | 15 ++++++---- tests/Support/Model/ModelMockTrait.php | 29 ++++++------------- .../NovaResource/NovaResourceMockTrait.php | 0 tests/Support/NovaTest/NovaTestMockTrait.php | 0 tests/Support/Shared/GeneratorMockTrait.php | 0 5 files changed, 18 insertions(+), 26 deletions(-) delete mode 100644 tests/Support/NovaResource/NovaResourceMockTrait.php delete mode 100644 tests/Support/NovaTest/NovaTestMockTrait.php delete mode 100644 tests/Support/Shared/GeneratorMockTrait.php diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index 35a3c9fe..c34d925b 100644 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -16,8 +16,10 @@ public function testModelAlreadyExists() { $this->mockGeneratorForExistingModel(); - $this->expectException(ClassAlreadyExistsException::class); - $this->expectExceptionMessage('Cannot create Post Model cause Post Model already exists. Remove Post Model.'); + $this->assertExceptionThrew( + className: ClassAlreadyExistsException::class, + message: 'Cannot create Post Model cause Post Model already exists. Remove Post Model.' + ); app(ModelGenerator::class) ->setModel('Post') @@ -26,9 +28,11 @@ public function testModelAlreadyExists() public function testRelationModelMissing() { - $this->expectException(ClassNotExistsException::class); - $this->expectExceptionMessage("Cannot create Post Model cause relation model Comment does not exist. " - . "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'."); + $this->assertExceptionThrew( + className: ClassNotExistsException::class, + message: "Cannot create Post Model cause relation model Comment does not exist. " + . "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'.", + ); app(ModelGenerator::class) ->setModel('Post') @@ -43,7 +47,6 @@ public function testRelationModelMissing() public function testCreateModel() { - $this->setupConfigurations(); $this->mockFilesystem(); app(ModelGenerator::class) diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index a4bc3f8d..17ed0be9 100644 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -10,41 +10,30 @@ trait ModelMockTrait { use GeneratorMockTrait; - public function mockGeneratorForExistingModel() + public function mockGeneratorForExistingModel(): void { $this->mockClass(ModelGenerator::class, [ [ 'function' => 'classExists', 'arguments' => ['models', 'Post'], - 'result' => true - ] + 'result' => true, + ], ]); } - public function mockGeneratorForMissingRelationModel() + public function mockGeneratorForMissingRelationModel(): void { $this->mockClass(ModelGenerator::class, [ [ 'function' => 'classExists', 'arguments' => ['models', 'Post'], - 'result' => false + 'result' => false, ], [ 'function' => 'classExists', 'arguments' => ['models', 'Comment'], - 'result' => false - ] - ]); - } - - public function setupConfigurations(): void - { - config([ - 'entity-generator.stubs.model' => 'entity-generator::model', - 'entity-generator.stubs.relation' => 'entity-generator::relation', - 'entity-generator.paths' => [ - 'models' => 'app/Models', - ] + 'result' => false, + ], ]); } @@ -54,8 +43,8 @@ public function mockFilesystem(): void 'app' => [ 'Models' => [ 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php'), - 'User.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php') - ] + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php'), + ], ], ]; diff --git a/tests/Support/NovaResource/NovaResourceMockTrait.php b/tests/Support/NovaResource/NovaResourceMockTrait.php deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/Support/NovaTest/NovaTestMockTrait.php b/tests/Support/NovaTest/NovaTestMockTrait.php deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/Support/Shared/GeneratorMockTrait.php b/tests/Support/Shared/GeneratorMockTrait.php deleted file mode 100644 index e69de29b..00000000 From d185d9cf3719a2b28e96ba16d4c8be56aadc72a9 Mon Sep 17 00:00:00 2001 From: roman Date: Thu, 19 Dec 2024 17:54:17 +0600 Subject: [PATCH 06/14] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/ModelGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index c34d925b..d1904e88 100644 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -18,7 +18,7 @@ public function testModelAlreadyExists() $this->assertExceptionThrew( className: ClassAlreadyExistsException::class, - message: 'Cannot create Post Model cause Post Model already exists. Remove Post Model.' + message: 'Cannot create Post Model cause Post Model already exists. Remove Post Model.', ); app(ModelGenerator::class) From f3165306f2c80724f01da688c34e008355e025a1 Mon Sep 17 00:00:00 2001 From: roman Date: Thu, 19 Dec 2024 17:55:34 +0600 Subject: [PATCH 07/14] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/Support/Controller/ControllerMockTrait.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/Support/Controller/ControllerMockTrait.php diff --git a/tests/Support/Controller/ControllerMockTrait.php b/tests/Support/Controller/ControllerMockTrait.php deleted file mode 100644 index e69de29b..00000000 From 4b5bd345dc8f4b7dc4e1689c2c841fc0e64d6300 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 10 Jan 2025 16:31:17 +0600 Subject: [PATCH 08/14] fix: model blade refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/ModelGenerator.php | 6 +++--- stubs/model.blade.php | 9 +++++---- tests/Support/Model/ModelMockTrait.php | 16 ---------------- tests/Support/Model/RelationModelMock.php | 1 - .../comment_relation_model.php | 1 - tests/fixtures/ModelGeneratorTest/new_model.php | 8 +++++--- .../ModelGeneratorTest/user_relation_model.php | 1 - 7 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 1a1c44d0..2a5b73be 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -59,9 +59,9 @@ public function prepareRelatedModels(): void foreach ($relationsByType as $relation) { if (!$this->classExists('models', $relation)) { $this->throwFailureException( - ClassNotExistsException::class, - "Cannot create {$this->model} Model cause relation model {$relation} does not exist.", - "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'." + exceptionClass: ClassNotExistsException::class, + failureMessage: "Cannot create {$this->model} Model cause relation model {$relation} does not exist.", + recommendedMessage: "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'.", ); } diff --git a/stubs/model.blade.php b/stubs/model.blade.php index 03d1b338..03d304ba 100644 --- a/stubs/model.blade.php +++ b/stubs/model.blade.php @@ -15,16 +15,17 @@ class {{$entity}} extends Model ]; protected $hidden = ['pivot']; -@foreach($relations as $relation) - @include(config('entity-generator.stubs.relation'), $relation) -@endforeach @if(!empty($casts)) - protected $casts = [ @foreach($casts as $fieldName => $cast) '{{$fieldName}}' => '{{$cast}}', @endforeach ]; @endif +@foreach($relations as $relation) + + @include(config('entity-generator.stubs.relation'), $relation) + +@endforeach } \ No newline at end of file diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index 17ed0be9..efded464 100644 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -21,22 +21,6 @@ public function mockGeneratorForExistingModel(): void ]); } - public function mockGeneratorForMissingRelationModel(): void - { - $this->mockClass(ModelGenerator::class, [ - [ - 'function' => 'classExists', - 'arguments' => ['models', 'Post'], - 'result' => false, - ], - [ - 'function' => 'classExists', - 'arguments' => ['models', 'Comment'], - 'result' => false, - ], - ]); - } - public function mockFilesystem(): void { $structure = [ diff --git a/tests/Support/Model/RelationModelMock.php b/tests/Support/Model/RelationModelMock.php index f8cf9a4f..d8ecef13 100644 --- a/tests/Support/Model/RelationModelMock.php +++ b/tests/Support/Model/RelationModelMock.php @@ -6,6 +6,5 @@ class RelationModelMock { public function some_relation() { - } } diff --git a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php index da860504..c77a9c88 100644 --- a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php @@ -6,7 +6,6 @@ class RelationModelMock { public function some_relation() { - } public function post() diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php index 80f1e297..94a1064b 100644 --- a/tests/fixtures/ModelGeneratorTest/new_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -17,15 +17,17 @@ class Post extends Model protected $hidden = ['pivot']; + protected $casts = [ + 'is_published' => 'boolean', + ]; + public function comment() { return $this->hasOne(Comment::class); } + public function users() { return $this->hasMany(User::class); } - protected $casts = [ - 'is_published' => 'boolean', - ]; } \ No newline at end of file diff --git a/tests/fixtures/ModelGeneratorTest/user_relation_model.php b/tests/fixtures/ModelGeneratorTest/user_relation_model.php index da860504..c77a9c88 100644 --- a/tests/fixtures/ModelGeneratorTest/user_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/user_relation_model.php @@ -6,7 +6,6 @@ class RelationModelMock { public function some_relation() { - } public function post() From 391eb09b535315dee09c4b146f8d7141aef190e9 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 10 Jan 2025 17:27:59 +0600 Subject: [PATCH 09/14] fix: tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/fixtures/ModelGeneratorTest/new_model.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php index 94a1064b..4aa5bb05 100644 --- a/tests/fixtures/ModelGeneratorTest/new_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -2,13 +2,12 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use RonasIT\Support\Traits\ModelTrait; class Post extends Model { - use HasFactory, ModelTrait; + use ModelTrait; protected $fillable = [ 'media_id', From e35ed3efc65b2c4c455add0d7f210930b7def9c5 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 10 Jan 2025 20:52:11 +0600 Subject: [PATCH 10/14] feat: add tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/EntityGenerator.php | 4 +- src/Generators/ModelGenerator.php | 2 +- stubs/model.blade.php | 2 +- tests/ModelGeneratorTest.php | 94 ++++++++++++++++++- ...new_model_without_fields_and_relations.php | 16 ++++ 5 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/ModelGeneratorTest/new_model_without_fields_and_relations.php diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index faf4c2f6..e12283f9 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -213,14 +213,14 @@ protected function getModelClass(string $model): string return "{$modelNamespace}\\{$model}"; } - protected function isStubExists(string $stubName): bool + protected function isStubExists(string $stubName, ?string $generationType = null): bool { $config = "entity-generator.stubs.{$stubName}"; $stubPath = config($config); if (!view()->exists($stubPath)) { - $generationType = Str::replace('_', ' ', $stubName); + $generationType ??= Str::replace('_', ' ', $stubName); $message = "Generation of {$generationType} has been skipped cause the view {$stubPath} from the config {$config} is not exists. Please check that config has the correct view name value."; diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 2a5b73be..64caaba1 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -25,7 +25,7 @@ public function generate(): void ); } - if ($this->isStubExists('model') && ($this->isStubExists('relation') || empty($this->relations))) { + if ($this->isStubExists('model') && (collect($this->relations)->every(fn ($relation) => empty($relation)) || $this->isStubExists('relation', 'model'))) { $this->prepareRelatedModels(); $modelContent = $this->getNewModelContent(); diff --git a/stubs/model.blade.php b/stubs/model.blade.php index cf673990..a7fe5a49 100644 --- a/stubs/model.blade.php +++ b/stubs/model.blade.php @@ -14,8 +14,8 @@ class {{$entity}} extends Model ]; protected $hidden = ['pivot']; - @if(!empty($casts)) + protected $casts = [ @foreach($casts as $fieldName => $cast) '{{$fieldName}}' => '{{$cast}}', diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index d1904e88..573d7ba4 100644 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.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\Exceptions\ClassAlreadyExistsException; use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Generators\ModelGenerator; @@ -12,6 +15,13 @@ class ModelGeneratorTest extends TestCase { use ModelMockTrait, MockTrait; + public function setUp(): void + { + parent::setUp(); + + Event::fake(); + } + public function testModelAlreadyExists() { $this->mockGeneratorForExistingModel(); @@ -51,7 +61,7 @@ public function testCreateModel() app(ModelGenerator::class) ->setModel('Post') - ->setfields([ + ->setFields([ 'integer-required' => ['media_id'], 'boolean-required' => ['is_published'], ]) @@ -66,5 +76,87 @@ public function testCreateModel() $this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php'); $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/Comment.php'); $this->assertGeneratedFileEquals('user_relation_model.php', 'app/Models/User.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Model: Post', + ); + } + + public function testCreateModelStubNotExist() + { + $this->mockFilesystem(); + + config(['entity-generator.stubs.model' => 'incorrect_stub']); + + app(ModelGenerator::class) + ->setModel('Post') + ->setFields([]) + ->setRelations([ + 'hasOne' => [], + 'hasMany' => [], + 'belongsTo' => [], + 'belongsToMany' => [], + ]) + ->generate(); + + $this->assertFileDoesNotExist('app/Models/Post.php'); + $this->assertFileDoesNotExist('app/Models/Comment.php'); + $this->assertFileDoesNotExist('app/Models/User.php'); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.model is not exists. Please check that config has the correct view name value.', + ); + } + + public function testCreateModelWithoutRelationsRelationStubNotExist() + { + $this->mockFilesystem(); + + config(['entity-generator.stubs.relation' => 'incorrect_stub']); + + app(ModelGenerator::class) + ->setModel('Post') + ->setFields([]) + ->setRelations([ + 'hasOne' => [], + 'hasMany' => [], + 'belongsTo' => [], + 'belongsToMany' => [], + ]) + ->generate(); + + $this->assertGeneratedFileEquals('new_model_without_fields_and_relations.php', 'app/Models/Post.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Model: Post', + ); + } + + public function testCreateModelWithRelationsRelationStubNotExist() + { + $this->mockFilesystem(); + + config(['entity-generator.stubs.relation' => 'incorrect_stub']); + + app(ModelGenerator::class) + ->setModel('Post') + ->setFields([]) + ->setRelations([ + 'hasOne' => ['Comment'], + 'hasMany' => ['User'], + 'belongsTo' => [], + 'belongsToMany' => [], + ]) + ->generate(); + + $this->assertFileDoesNotExist('new_model.php', 'app/Models/Post.php'); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.relation is not exists. Please check that config has the correct view name value.', + ); } } diff --git a/tests/fixtures/ModelGeneratorTest/new_model_without_fields_and_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_without_fields_and_relations.php new file mode 100644 index 00000000..4a643d9b --- /dev/null +++ b/tests/fixtures/ModelGeneratorTest/new_model_without_fields_and_relations.php @@ -0,0 +1,16 @@ + Date: Fri, 10 Jan 2025 22:37:42 +0600 Subject: [PATCH 11/14] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/ModelGeneratorTest.php | 12 +++--------- tests/Support/Model/ModelMockTrait.php | 12 ------------ tests/TestCase.php | 2 ++ 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index 573d7ba4..5d34ac25 100644 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.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\Exceptions\ClassAlreadyExistsException; @@ -15,16 +14,11 @@ class ModelGeneratorTest extends TestCase { use ModelMockTrait, MockTrait; - public function setUp(): void - { - parent::setUp(); - - Event::fake(); - } - public function testModelAlreadyExists() { - $this->mockGeneratorForExistingModel(); + $this->mockClass(ModelGenerator::class, [ + $this->classExistsMethodCall(['models', 'Post']), + ]); $this->assertExceptionThrew( className: ClassAlreadyExistsException::class, diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index efded464..f0b2fcca 100644 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -3,24 +3,12 @@ namespace RonasIT\Support\Tests\Support\Model; use org\bovigo\vfs\vfsStream; -use RonasIT\Support\Generators\ModelGenerator; use RonasIT\Support\Tests\Support\GeneratorMockTrait; trait ModelMockTrait { use GeneratorMockTrait; - public function mockGeneratorForExistingModel(): void - { - $this->mockClass(ModelGenerator::class, [ - [ - 'function' => 'classExists', - 'arguments' => ['models', 'Post'], - 'result' => true, - ], - ]); - } - public function mockFilesystem(): void { $structure = [ diff --git a/tests/TestCase.php b/tests/TestCase.php index a5c0f83c..00e70300 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,6 +28,8 @@ public function setUp(): void vfsStream::setup(); + Event::fake(); + $this->generatedFileBasePath = vfsStream::url('root'); $this->app->setBasePath($this->generatedFileBasePath); From 8442381e4da530ff17ae2753a03af26663fd6cc3 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 14 Jan 2025 11:07:51 +0600 Subject: [PATCH 12/14] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/EntityGenerator.php | 2 +- tests/ControllerGeneratorTest.php | 8 ------- tests/FactoryGeneratorTest.php | 7 ------- tests/ModelGeneratorTest.php | 17 ++------------- tests/NovaResourceGeneratorTest.php | 8 ------- tests/NovaTestGeneratorTest.php | 7 ------- tests/SeederGeneratorTest.php | 8 ------- .../ControllerGeneratorMockTrait.php | 2 -- tests/Support/Factory/FactoryMockTrait.php | 3 +-- tests/Support/GeneratorMockTrait.php | 3 +++ tests/Support/Model/ModelMockTrait.php | 4 ++-- tests/Support/Model/RelationModelMock.php | 10 --------- tests/Support/Models/WelcomeBonus.php | 4 ++++ .../NovaResourceGeneratorMockTrait.php | 2 -- .../NovaTestGeneratorMockTrait.php | 2 -- .../comment_relation_model.php | 21 ++++++++++++++++--- .../user_relation_model.php | 15 ------------- 17 files changed, 31 insertions(+), 92 deletions(-) delete mode 100644 tests/Support/Model/RelationModelMock.php delete mode 100644 tests/fixtures/ModelGeneratorTest/user_relation_model.php diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index e12283f9..212784c8 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -26,7 +26,7 @@ abstract class EntityGenerator protected $paths = []; protected $model; protected $fields; - protected $relations; + protected $relations = []; protected $crudOptions; /** diff --git a/tests/ControllerGeneratorTest.php b/tests/ControllerGeneratorTest.php index ac9f2484..96ce0f54 100644 --- a/tests/ControllerGeneratorTest.php +++ b/tests/ControllerGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use Illuminate\Contracts\Filesystem\FileNotFoundException; -use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\View; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; @@ -16,13 +15,6 @@ class ControllerGeneratorTest extends TestCase { use ControllerGeneratorMockTrait; - public function setUp(): void - { - parent::setUp(); - - Event::fake(); - } - public function testControllerAlreadyExists() { $this->mockClass(ControllerGenerator::class, [ diff --git a/tests/FactoryGeneratorTest.php b/tests/FactoryGeneratorTest.php index 0d103dd9..dae52b85 100644 --- a/tests/FactoryGeneratorTest.php +++ b/tests/FactoryGeneratorTest.php @@ -14,13 +14,6 @@ class FactoryGeneratorTest extends TestCase { use FactoryMockTrait; - public function setUp(): void - { - parent::setUp(); - - Event::fake(); - } - public function testModelNotExists() { $this->assertExceptionThrew( diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index 5d34ac25..73e96af1 100644 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -8,11 +8,10 @@ use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Generators\ModelGenerator; use RonasIT\Support\Tests\Support\Model\ModelMockTrait; -use RonasIT\Support\Traits\MockTrait; class ModelGeneratorTest extends TestCase { - use ModelMockTrait, MockTrait; + use ModelMockTrait; public function testModelAlreadyExists() { @@ -69,7 +68,7 @@ public function testCreateModel() $this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php'); $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/Comment.php'); - $this->assertGeneratedFileEquals('user_relation_model.php', 'app/Models/User.php'); + $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/User.php'); $this->assertEventPushed( className: SuccessCreateMessage::class, @@ -86,12 +85,6 @@ public function testCreateModelStubNotExist() app(ModelGenerator::class) ->setModel('Post') ->setFields([]) - ->setRelations([ - 'hasOne' => [], - 'hasMany' => [], - 'belongsTo' => [], - 'belongsToMany' => [], - ]) ->generate(); $this->assertFileDoesNotExist('app/Models/Post.php'); @@ -113,12 +106,6 @@ public function testCreateModelWithoutRelationsRelationStubNotExist() app(ModelGenerator::class) ->setModel('Post') ->setFields([]) - ->setRelations([ - 'hasOne' => [], - 'hasMany' => [], - 'belongsTo' => [], - 'belongsToMany' => [], - ]) ->generate(); $this->assertGeneratedFileEquals('new_model_without_fields_and_relations.php', 'app/Models/Post.php'); diff --git a/tests/NovaResourceGeneratorTest.php b/tests/NovaResourceGeneratorTest.php index 4e7710e1..5a5b897f 100644 --- a/tests/NovaResourceGeneratorTest.php +++ b/tests/NovaResourceGeneratorTest.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\Exceptions\ClassAlreadyExistsException; @@ -14,13 +13,6 @@ class NovaResourceGeneratorTest extends TestCase { use NovaResourceGeneratorMockTrait; - public function setUp(): void - { - parent::setUp(); - - Event::fake(); - } - public function testCreateWithMissingNovaPackage() { $this->mockNovaServiceProviderExists(false); diff --git a/tests/NovaTestGeneratorTest.php b/tests/NovaTestGeneratorTest.php index be1a052b..8b3fe11e 100644 --- a/tests/NovaTestGeneratorTest.php +++ b/tests/NovaTestGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use RonasIT\Support\Tests\Support\Models\WelcomeBonus; -use Illuminate\Support\Facades\Event; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Exceptions\ClassAlreadyExistsException; @@ -58,8 +57,6 @@ className: ClassAlreadyExistsException::class, public function testNovaTestStubNotExist() { - Event::fake(); - $this->mockNativeGeneratorFunctions( $this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]), $this->nativeClassExistsMethodCall([WelcomeBonus::class, true]), @@ -96,8 +93,6 @@ className: WarningEvent::class, public function testDumpStubNotExist() { - Event::fake(); - $this->mockNovaServiceProviderExists(); $this->mockFilesystem(); @@ -156,8 +151,6 @@ public function testSuccess() public function testGenerateNovaPackageNotInstall() { - Event::fake(); - $this->mockNovaServiceProviderExists(false); app(NovaTestGenerator::class) diff --git a/tests/SeederGeneratorTest.php b/tests/SeederGeneratorTest.php index 167b4821..a50ba6b1 100644 --- a/tests/SeederGeneratorTest.php +++ b/tests/SeederGeneratorTest.php @@ -2,7 +2,6 @@ namespace RonasIT\Support\Tests; -use Illuminate\Support\Facades\Event; use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Generators\SeederGenerator; use RonasIT\Support\Tests\Support\SeederGeneratorMockTrait; @@ -11,13 +10,6 @@ class SeederGeneratorTest extends TestCase { use SeederGeneratorMockTrait; - public function setUp(): void - { - parent::setUp(); - - Event::fake(); - } - public function testCreateSeeder() { $this->mockFilesystem(); diff --git a/tests/Support/ControllerGeneratorTest/ControllerGeneratorMockTrait.php b/tests/Support/ControllerGeneratorTest/ControllerGeneratorMockTrait.php index 67f9374b..fb39a019 100644 --- a/tests/Support/ControllerGeneratorTest/ControllerGeneratorMockTrait.php +++ b/tests/Support/ControllerGeneratorTest/ControllerGeneratorMockTrait.php @@ -4,12 +4,10 @@ use RonasIT\Support\Tests\Support\FileSystemMock; use RonasIT\Support\Tests\Support\GeneratorMockTrait; -use RonasIT\Support\Traits\MockTrait; trait ControllerGeneratorMockTrait { use GeneratorMockTrait; - use MockTrait; public function mockFilesystemWithoutRoutesFile(): void { diff --git a/tests/Support/Factory/FactoryMockTrait.php b/tests/Support/Factory/FactoryMockTrait.php index 60a4b920..3a87fca7 100644 --- a/tests/Support/Factory/FactoryMockTrait.php +++ b/tests/Support/Factory/FactoryMockTrait.php @@ -5,11 +5,10 @@ use org\bovigo\vfs\vfsStream; use RonasIT\Support\Generators\FactoryGenerator; use RonasIT\Support\Tests\Support\GeneratorMockTrait; -use RonasIT\Support\Traits\MockTrait; trait FactoryMockTrait { - use GeneratorMockTrait, MockTrait; + use GeneratorMockTrait; public function mockFactoryGenerator(array ...$functionCalls): void { 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/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index f0b2fcca..71f8362d 100644 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -14,8 +14,8 @@ public function mockFilesystem(): void $structure = [ 'app' => [ 'Models' => [ - 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php'), - 'User.php' => file_get_contents(getcwd() . '/tests/Support/Model/RelationModelMock.php'), + 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), ], ], ]; diff --git a/tests/Support/Model/RelationModelMock.php b/tests/Support/Model/RelationModelMock.php deleted file mode 100644 index d8ecef13..00000000 --- a/tests/Support/Model/RelationModelMock.php +++ /dev/null @@ -1,10 +0,0 @@ -belongsTo(Post::class); } -} +} \ No newline at end of file diff --git a/tests/fixtures/ModelGeneratorTest/user_relation_model.php b/tests/fixtures/ModelGeneratorTest/user_relation_model.php deleted file mode 100644 index c77a9c88..00000000 --- a/tests/fixtures/ModelGeneratorTest/user_relation_model.php +++ /dev/null @@ -1,15 +0,0 @@ -belongsTo(Post::class); - } -} From 3bde64ff3c7b2358dd0df5d3ec93c4568393edba Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 15 Jan 2025 12:23:52 +0600 Subject: [PATCH 13/14] refactor: code refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Generators/ModelGenerator.php | 7 +++- tests/FactoryGeneratorTest.php | 2 -- tests/MigrationGeneratorTest.php | 11 ------- tests/SeederGeneratorTest.php | 9 ------ tests/Support/Factory/FactoryMockTrait.php | 29 ++++------------- .../Support/Migration/MigrationMockTrait.php | 32 ------------------- tests/Support/Model/ModelMockTrait.php | 16 ++++------ tests/Support/SeederGeneratorMockTrait.php | 19 ----------- 8 files changed, 20 insertions(+), 105 deletions(-) delete mode 100644 tests/Support/Migration/MigrationMockTrait.php delete mode 100644 tests/Support/SeederGeneratorMockTrait.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 64caaba1..15c11ce2 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -25,7 +25,7 @@ public function generate(): void ); } - if ($this->isStubExists('model') && (collect($this->relations)->every(fn ($relation) => empty($relation)) || $this->isStubExists('relation', 'model'))) { + if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) { $this->prepareRelatedModels(); $modelContent = $this->getNewModelContent(); @@ -35,6 +35,11 @@ public function generate(): void } } + protected function hasRelations(): bool + { + return !collect($this->relations)->every(fn ($relation) => empty($relation)); + } + protected function getNewModelContent(): string { return $this->getStub('model', [ diff --git a/tests/FactoryGeneratorTest.php b/tests/FactoryGeneratorTest.php index dae52b85..b46b5eb5 100644 --- a/tests/FactoryGeneratorTest.php +++ b/tests/FactoryGeneratorTest.php @@ -50,7 +50,6 @@ className: ClassAlreadyExistsException::class, public function testProcessUnknownFieldType() { - $this->mockConfigurations(); $this->mockFilesystem(); $this->assertExceptionThrew( @@ -75,7 +74,6 @@ className: ViewException::class, public function testCreateSuccess() { - $this->mockConfigurations(); $this->mockFilesystem(); app(FactoryGenerator::class) diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index ac272a48..867a57f9 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -5,16 +5,11 @@ use Illuminate\Support\Carbon; use RonasIT\Support\Exceptions\UnknownFieldTypeException; use RonasIT\Support\Generators\MigrationGenerator; -use RonasIT\Support\Tests\Support\Migration\MigrationMockTrait; class MigrationGeneratorTest extends TestCase { - use MigrationMockTrait; - public function testSetUnknownFieldType() { - $this->setupConfigurations(); - $this->assertExceptionThrew( className: UnknownFieldTypeException::class, message: 'Unknown field type unknown-type in MigrationGenerator.', @@ -39,9 +34,6 @@ public function testCreateMigration() { Carbon::setTestNow('2022-02-02'); - $this->mockFilesystem(); - $this->setupConfigurations(); - app(MigrationGenerator::class) ->setModel('Post') ->setRelations([ @@ -67,9 +59,6 @@ public function testCreateMigrationMYSQL() Carbon::setTestNow('2022-02-02'); - $this->mockFilesystem(); - $this->setupConfigurations(); - app(MigrationGenerator::class) ->setModel('Post') ->setRelations([ diff --git a/tests/SeederGeneratorTest.php b/tests/SeederGeneratorTest.php index a50ba6b1..96b56bc5 100644 --- a/tests/SeederGeneratorTest.php +++ b/tests/SeederGeneratorTest.php @@ -4,16 +4,11 @@ use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Generators\SeederGenerator; -use RonasIT\Support\Tests\Support\SeederGeneratorMockTrait; class SeederGeneratorTest extends TestCase { - use SeederGeneratorMockTrait; - public function testCreateSeeder() { - $this->mockFilesystem(); - app(SeederGenerator::class) ->setRelations([ 'hasOne' => [], @@ -30,8 +25,6 @@ public function testCreateSeeder() public function testCreateSeederEmptyDatabaseSeederStubNotExist() { - $this->mockFilesystem(); - config(['entity-generator.stubs.database_empty_seeder' => 'entity-generator::database_seed_empty']); app(SeederGenerator::class) @@ -55,8 +48,6 @@ className: WarningEvent::class, public function testCreateSeederEntityDatabaseSeederStubNotExist() { - $this->mockFilesystem(); - config(['entity-generator.stubs.seeder' => 'incorrect_stub']); app(SeederGenerator::class) diff --git a/tests/Support/Factory/FactoryMockTrait.php b/tests/Support/Factory/FactoryMockTrait.php index 3a87fca7..353eb71e 100644 --- a/tests/Support/Factory/FactoryMockTrait.php +++ b/tests/Support/Factory/FactoryMockTrait.php @@ -2,8 +2,8 @@ namespace RonasIT\Support\Tests\Support\Factory; -use org\bovigo\vfs\vfsStream; use RonasIT\Support\Generators\FactoryGenerator; +use RonasIT\Support\Tests\Support\FileSystemMock; use RonasIT\Support\Tests\Support\GeneratorMockTrait; trait FactoryMockTrait @@ -15,30 +15,15 @@ public function mockFactoryGenerator(array ...$functionCalls): void $this->mockClass(FactoryGenerator::class, $functionCalls); } - public function mockConfigurations(): void - { - config([ - 'entity-generator.paths' => [ - 'models' => 'app/Models', - 'factories' => 'database/factories', - ], - ]); - } - public function mockFilesystem(): void { - $structure = [ - 'app' => [ - 'Models' => [ - 'Post.php' => $this->mockPhpFileContent(), - 'User.php' => $this->mockPhpFileContent(), - ], - ], - 'database' => [ - 'factories' => [], - ], + $fileSystemMock = new FileSystemMock; + + $fileSystemMock->models = [ + 'Post.php' => $this->mockPhpFileContent(), + 'User.php' => $this->mockPhpFileContent(), ]; - vfsStream::create($structure); + $fileSystemMock->setStructure(); } } diff --git a/tests/Support/Migration/MigrationMockTrait.php b/tests/Support/Migration/MigrationMockTrait.php deleted file mode 100644 index 05b88f7c..00000000 --- a/tests/Support/Migration/MigrationMockTrait.php +++ /dev/null @@ -1,32 +0,0 @@ - 'entity-generator::migration', - 'entity-generator.paths' => [ - 'migrations' => 'database/migrations', - ] - ]); - } - - public function mockFilesystem(): void - { - $structure = [ - 'database' => [ - 'migrations' => [], - ], - ]; - - vfsStream::create($structure); - } -} diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index 71f8362d..dad2d4ea 100644 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -2,7 +2,7 @@ namespace RonasIT\Support\Tests\Support\Model; -use org\bovigo\vfs\vfsStream; +use RonasIT\Support\Tests\Support\FileSystemMock; use RonasIT\Support\Tests\Support\GeneratorMockTrait; trait ModelMockTrait @@ -11,15 +11,13 @@ trait ModelMockTrait public function mockFilesystem(): void { - $structure = [ - 'app' => [ - 'Models' => [ - 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - ], - ], + $fileSystemMock = new FileSystemMock; + + $fileSystemMock->models = [ + 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), ]; - vfsStream::create($structure); + $fileSystemMock->setStructure(); } } diff --git a/tests/Support/SeederGeneratorMockTrait.php b/tests/Support/SeederGeneratorMockTrait.php deleted file mode 100644 index 455597a1..00000000 --- a/tests/Support/SeederGeneratorMockTrait.php +++ /dev/null @@ -1,19 +0,0 @@ - [] - ]; - - vfsStream::create($structure); - } -} \ No newline at end of file From 8e61ee5ce0beff92c2632a005ce4904a74958977 Mon Sep 17 00:00:00 2001 From: DenTray Date: Wed, 15 Jan 2025 21:40:22 +0600 Subject: [PATCH 14/14] Update tests/Support/Factory/FactoryMockTrait.php --- tests/Support/Factory/FactoryMockTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/Factory/FactoryMockTrait.php b/tests/Support/Factory/FactoryMockTrait.php index 353eb71e..0e9bb17f 100644 --- a/tests/Support/Factory/FactoryMockTrait.php +++ b/tests/Support/Factory/FactoryMockTrait.php @@ -17,7 +17,7 @@ public function mockFactoryGenerator(array ...$functionCalls): void public function mockFilesystem(): void { - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->models = [ 'Post.php' => $this->mockPhpFileContent(),