Skip to content

Commit 3bde64f

Browse files
committed
refactor: code
refs: #49
1 parent 8442381 commit 3bde64f

File tree

8 files changed

+20
-105
lines changed

8 files changed

+20
-105
lines changed

src/Generators/ModelGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function generate(): void
2525
);
2626
}
2727

28-
if ($this->isStubExists('model') && (collect($this->relations)->every(fn ($relation) => empty($relation)) || $this->isStubExists('relation', 'model'))) {
28+
if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) {
2929
$this->prepareRelatedModels();
3030
$modelContent = $this->getNewModelContent();
3131

@@ -35,6 +35,11 @@ public function generate(): void
3535
}
3636
}
3737

38+
protected function hasRelations(): bool
39+
{
40+
return !collect($this->relations)->every(fn ($relation) => empty($relation));
41+
}
42+
3843
protected function getNewModelContent(): string
3944
{
4045
return $this->getStub('model', [

tests/FactoryGeneratorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ className: ClassAlreadyExistsException::class,
5050

5151
public function testProcessUnknownFieldType()
5252
{
53-
$this->mockConfigurations();
5453
$this->mockFilesystem();
5554

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

7675
public function testCreateSuccess()
7776
{
78-
$this->mockConfigurations();
7977
$this->mockFilesystem();
8078

8179
app(FactoryGenerator::class)

tests/MigrationGeneratorTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
use Illuminate\Support\Carbon;
66
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
77
use RonasIT\Support\Generators\MigrationGenerator;
8-
use RonasIT\Support\Tests\Support\Migration\MigrationMockTrait;
98

109
class MigrationGeneratorTest extends TestCase
1110
{
12-
use MigrationMockTrait;
13-
1411
public function testSetUnknownFieldType()
1512
{
16-
$this->setupConfigurations();
17-
1813
$this->assertExceptionThrew(
1914
className: UnknownFieldTypeException::class,
2015
message: 'Unknown field type unknown-type in MigrationGenerator.',
@@ -39,9 +34,6 @@ public function testCreateMigration()
3934
{
4035
Carbon::setTestNow('2022-02-02');
4136

42-
$this->mockFilesystem();
43-
$this->setupConfigurations();
44-
4537
app(MigrationGenerator::class)
4638
->setModel('Post')
4739
->setRelations([
@@ -67,9 +59,6 @@ public function testCreateMigrationMYSQL()
6759

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

70-
$this->mockFilesystem();
71-
$this->setupConfigurations();
72-
7362
app(MigrationGenerator::class)
7463
->setModel('Post')
7564
->setRelations([

tests/SeederGeneratorTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44

55
use RonasIT\Support\Events\WarningEvent;
66
use RonasIT\Support\Generators\SeederGenerator;
7-
use RonasIT\Support\Tests\Support\SeederGeneratorMockTrait;
87

98
class SeederGeneratorTest extends TestCase
109
{
11-
use SeederGeneratorMockTrait;
12-
1310
public function testCreateSeeder()
1411
{
15-
$this->mockFilesystem();
16-
1712
app(SeederGenerator::class)
1813
->setRelations([
1914
'hasOne' => [],
@@ -30,8 +25,6 @@ public function testCreateSeeder()
3025

3126
public function testCreateSeederEmptyDatabaseSeederStubNotExist()
3227
{
33-
$this->mockFilesystem();
34-
3528
config(['entity-generator.stubs.database_empty_seeder' => 'entity-generator::database_seed_empty']);
3629

3730
app(SeederGenerator::class)
@@ -55,8 +48,6 @@ className: WarningEvent::class,
5548

5649
public function testCreateSeederEntityDatabaseSeederStubNotExist()
5750
{
58-
$this->mockFilesystem();
59-
6051
config(['entity-generator.stubs.seeder' => 'incorrect_stub']);
6152

6253
app(SeederGenerator::class)

tests/Support/Factory/FactoryMockTrait.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace RonasIT\Support\Tests\Support\Factory;
44

5-
use org\bovigo\vfs\vfsStream;
65
use RonasIT\Support\Generators\FactoryGenerator;
6+
use RonasIT\Support\Tests\Support\FileSystemMock;
77
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
88

99
trait FactoryMockTrait
@@ -15,30 +15,15 @@ public function mockFactoryGenerator(array ...$functionCalls): void
1515
$this->mockClass(FactoryGenerator::class, $functionCalls);
1616
}
1717

18-
public function mockConfigurations(): void
19-
{
20-
config([
21-
'entity-generator.paths' => [
22-
'models' => 'app/Models',
23-
'factories' => 'database/factories',
24-
],
25-
]);
26-
}
27-
2818
public function mockFilesystem(): void
2919
{
30-
$structure = [
31-
'app' => [
32-
'Models' => [
33-
'Post.php' => $this->mockPhpFileContent(),
34-
'User.php' => $this->mockPhpFileContent(),
35-
],
36-
],
37-
'database' => [
38-
'factories' => [],
39-
],
20+
$fileSystemMock = new FileSystemMock;
21+
22+
$fileSystemMock->models = [
23+
'Post.php' => $this->mockPhpFileContent(),
24+
'User.php' => $this->mockPhpFileContent(),
4025
];
4126

42-
vfsStream::create($structure);
27+
$fileSystemMock->setStructure();
4328
}
4429
}

tests/Support/Migration/MigrationMockTrait.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/Support/Model/ModelMockTrait.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RonasIT\Support\Tests\Support\Model;
44

5-
use org\bovigo\vfs\vfsStream;
5+
use RonasIT\Support\Tests\Support\FileSystemMock;
66
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
77

88
trait ModelMockTrait
@@ -11,15 +11,13 @@ trait ModelMockTrait
1111

1212
public function mockFilesystem(): void
1313
{
14-
$structure = [
15-
'app' => [
16-
'Models' => [
17-
'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
18-
'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
19-
],
20-
],
14+
$fileSystemMock = new FileSystemMock;
15+
16+
$fileSystemMock->models = [
17+
'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
18+
'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
2119
];
2220

23-
vfsStream::create($structure);
21+
$fileSystemMock->setStructure();
2422
}
2523
}

tests/Support/SeederGeneratorMockTrait.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)