Skip to content

Commit 3b523a6

Browse files
authored
Merge pull request #70 from RonasIT/49-add-model-generator-tests
Add model generator tests
2 parents 69b0fe6 + 8e61ee5 commit 3b523a6

24 files changed

+292
-166
lines changed

src/Generators/EntityGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class EntityGenerator
2626
protected $paths = [];
2727
protected $model;
2828
protected $fields;
29-
protected $relations;
29+
protected $relations = [];
3030
protected $crudOptions;
3131

3232
/**
@@ -213,14 +213,14 @@ protected function getModelClass(string $model): string
213213
return "{$modelNamespace}\\{$model}";
214214
}
215215

216-
protected function isStubExists(string $stubName): bool
216+
protected function isStubExists(string $stubName, ?string $generationType = null): bool
217217
{
218218
$config = "entity-generator.stubs.{$stubName}";
219219

220220
$stubPath = config($config);
221221

222222
if (!view()->exists($stubPath)) {
223-
$generationType = Str::replace('_', ' ', $stubName);
223+
$generationType ??= Str::replace('_', ' ', $stubName);
224224

225225
$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.";
226226

src/Generators/ModelGenerator.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010

1111
class ModelGenerator extends EntityGenerator
1212
{
13-
CONST PLURAL_NUMBER_REQUIRED = [
13+
protected const array PLURAL_NUMBER_REQUIRED = [
1414
'belongsToMany',
15-
'hasMany'
15+
'hasMany',
1616
];
1717

1818
public function generate(): void
1919
{
2020
if ($this->classExists('models', $this->model)) {
2121
$this->throwFailureException(
22-
ClassAlreadyExistsException::class,
23-
"Cannot create {$this->model} Model cause {$this->model} Model already exists.",
24-
"Remove {$this->model} Model."
22+
exceptionClass: ClassAlreadyExistsException::class,
23+
failureMessage: "Cannot create {$this->model} Model cause {$this->model} Model already exists.",
24+
recommendedMessage: "Remove {$this->model} Model.",
2525
);
2626
}
2727

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

@@ -35,14 +35,19 @@ 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', [
4146
'entity' => $this->model,
4247
'fields' => Arr::collapse($this->fields),
4348
'relations' => $this->prepareRelations(),
4449
'casts' => $this->getCasts($this->fields),
45-
'namespace' => $this->getOrCreateNamespace('models')
50+
'namespace' => $this->getOrCreateNamespace('models'),
4651
]);
4752
}
4853

@@ -59,9 +64,9 @@ public function prepareRelatedModels(): void
5964
foreach ($relationsByType as $relation) {
6065
if (!$this->classExists('models', $relation)) {
6166
$this->throwFailureException(
62-
ClassNotExistsException::class,
63-
"Cannot create {$relation} Model cause {$relation} Model does not exists.",
64-
"Create a {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'."
67+
exceptionClass: ClassNotExistsException::class,
68+
failureMessage: "Cannot create {$this->model} Model cause relation model {$relation} does not exist.",
69+
recommendedMessage: "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'.",
6570
);
6671
}
6772

@@ -70,7 +75,7 @@ public function prepareRelatedModels(): void
7075
$newRelation = $this->getStub('relation', [
7176
'name' => $this->getRelationName($this->model, $types[$type]),
7277
'type' => $types[$type],
73-
'entity' => $this->model
78+
'entity' => $this->model,
7479
]);
7580

7681
$fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content);
@@ -80,9 +85,9 @@ public function prepareRelatedModels(): void
8085
}
8186
}
8287

83-
public function getModelContent($model): string
88+
public function getModelContent(string $model): string
8489
{
85-
$modelPath = base_path($this->paths['models'] . "/{$model}.php");
90+
$modelPath = base_path("{$this->paths['models']}/{$model}.php");
8691

8792
return file_get_contents($modelPath);
8893
}
@@ -97,7 +102,7 @@ public function prepareRelations(): array
97102
$result[] = [
98103
'name' => $this->getRelationName($relation, $type),
99104
'type' => $type,
100-
'entity' => $relation
105+
'entity' => $relation,
101106
];
102107
}
103108
}
@@ -106,7 +111,7 @@ public function prepareRelations(): array
106111
return $result;
107112
}
108113

109-
protected function getCasts($fields): array
114+
protected function getCasts(array $fields): array
110115
{
111116
$casts = [
112117
'boolean-required' => 'boolean',
@@ -117,7 +122,7 @@ protected function getCasts($fields): array
117122
$result = [];
118123

119124
foreach ($fields as $fieldType => $names) {
120-
if (empty($casts[$fieldType])) {
125+
if (!array_key_exists($fieldType, $casts)) {
121126
continue;
122127
}
123128

@@ -129,7 +134,7 @@ protected function getCasts($fields): array
129134
return $result;
130135
}
131136

132-
private function getRelationName($relation, $type): string
137+
private function getRelationName(string $relation, string $type): string
133138
{
134139
$relationName = Str::snake($relation);
135140

stubs/model.blade.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class {{$entity}} extends Model
1414
];
1515

1616
protected $hidden = ['pivot'];
17-
@foreach($relations as $relation)
18-
19-
@include(config('entity-generator.stubs.relation'), $relation)
20-
@endforeach
2117
@if(!empty($casts))
2218

2319
protected $casts = [
@@ -26,4 +22,9 @@ class {{$entity}} extends Model
2622
@endforeach
2723
];
2824
@endif
25+
@foreach($relations as $relation)
26+
27+
@include(config('entity-generator.stubs.relation'), $relation)
28+
29+
@endforeach
2930
}

stubs/relation.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
public function {{$name}}()
22
{
33
return $this->{{$type}}({{$entity}}::class);
4-
}
4+
}

tests/ControllerGeneratorTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RonasIT\Support\Tests;
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6-
use Illuminate\Support\Facades\Event;
76
use Illuminate\Support\Facades\View;
87
use RonasIT\Support\Events\SuccessCreateMessage;
98
use RonasIT\Support\Events\WarningEvent;
@@ -16,13 +15,6 @@ class ControllerGeneratorTest extends TestCase
1615
{
1716
use ControllerGeneratorMockTrait;
1817

19-
public function setUp(): void
20-
{
21-
parent::setUp();
22-
23-
Event::fake();
24-
}
25-
2618
public function testControllerAlreadyExists()
2719
{
2820
$this->mockClass(ControllerGenerator::class, [

tests/FactoryGeneratorTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ class FactoryGeneratorTest extends TestCase
1414
{
1515
use FactoryMockTrait;
1616

17-
public function setUp(): void
18-
{
19-
parent::setUp();
20-
21-
Event::fake();
22-
}
23-
2417
public function testModelNotExists()
2518
{
2619
$this->assertExceptionThrew(
@@ -57,7 +50,6 @@ className: ClassAlreadyExistsException::class,
5750

5851
public function testProcessUnknownFieldType()
5952
{
60-
$this->mockConfigurations();
6153
$this->mockFilesystem();
6254

6355
$this->assertExceptionThrew(
@@ -82,7 +74,6 @@ className: ViewException::class,
8274

8375
public function testCreateSuccess()
8476
{
85-
$this->mockConfigurations();
8677
$this->mockFilesystem();
8778

8879
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([

0 commit comments

Comments
 (0)