Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
refs: #49
  • Loading branch information
pirs1337 committed Jan 15, 2025
1 parent 8442381 commit 3bde64f
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 105 deletions.
7 changes: 6 additions & 1 deletion src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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', [
Expand Down
2 changes: 0 additions & 2 deletions tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ className: ClassAlreadyExistsException::class,

public function testProcessUnknownFieldType()
{
$this->mockConfigurations();
$this->mockFilesystem();

$this->assertExceptionThrew(
Expand All @@ -75,7 +74,6 @@ className: ViewException::class,

public function testCreateSuccess()
{
$this->mockConfigurations();
$this->mockFilesystem();

app(FactoryGenerator::class)
Expand Down
11 changes: 0 additions & 11 deletions tests/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -39,9 +34,6 @@ public function testCreateMigration()
{
Carbon::setTestNow('2022-02-02');

$this->mockFilesystem();
$this->setupConfigurations();

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
Expand All @@ -67,9 +59,6 @@ public function testCreateMigrationMYSQL()

Carbon::setTestNow('2022-02-02');

$this->mockFilesystem();
$this->setupConfigurations();

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
Expand Down
9 changes: 0 additions & 9 deletions tests/SeederGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand All @@ -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)
Expand All @@ -55,8 +48,6 @@ className: WarningEvent::class,

public function testCreateSeederEntityDatabaseSeederStubNotExist()
{
$this->mockFilesystem();

config(['entity-generator.stubs.seeder' => 'incorrect_stub']);

app(SeederGenerator::class)
Expand Down
29 changes: 7 additions & 22 deletions tests/Support/Factory/FactoryMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
32 changes: 0 additions & 32 deletions tests/Support/Migration/MigrationMockTrait.php

This file was deleted.

16 changes: 7 additions & 9 deletions tests/Support/Model/ModelMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
19 changes: 0 additions & 19 deletions tests/Support/SeederGeneratorMockTrait.php

This file was deleted.

0 comments on commit 3bde64f

Please sign in to comment.