From 3bde64ff3c7b2358dd0df5d3ec93c4568393edba Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 15 Jan 2025 12:23:52 +0600 Subject: [PATCH] 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 64caaba..15c11ce 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 dae52b8..b46b5eb 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 ac272a4..867a57f 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 a50ba6b..96b56bc 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 3a87fca..353eb71 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 05b88f7..0000000 --- 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 71f8362..dad2d4e 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 455597a..0000000 --- a/tests/Support/SeederGeneratorMockTrait.php +++ /dev/null @@ -1,19 +0,0 @@ - [] - ]; - - vfsStream::create($structure); - } -} \ No newline at end of file