Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8c7116b
test: add model generation test.
Dec 20, 2023
8dc9b15
Merge branch 'master' into 49-add-model-generator-tests
t0xas Nov 20, 2024
08c0965
Merge branch '49-add-migration-generator-tests' into 49-add-model-gen…
t0xas Nov 20, 2024
d2e27c0
chore: add migration generator tests
t0xas Nov 20, 2024
ba8a9f0
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 Dec 9, 2024
fe2e1f3
feat: fix tests
pirs1337 Dec 9, 2024
45de444
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 Dec 9, 2024
6551491
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 Dec 17, 2024
523ad40
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 Dec 17, 2024
14abfa0
feat: change test
pirs1337 Dec 17, 2024
3ae2cdc
refactor: code
pirs1337 Dec 19, 2024
d185d9c
refactor: code
pirs1337 Dec 19, 2024
f316530
refactor: code
pirs1337 Dec 19, 2024
f871c5d
Merge branch 'master' into 49-add-model-generator-tests
DenTray Dec 23, 2024
4b5bd34
fix: model blade
pirs1337 Jan 10, 2025
864e2c1
Merge remote-tracking branch 'origin/49-add-model-generator-tests' in…
pirs1337 Jan 10, 2025
391eb09
fix: tests
pirs1337 Jan 10, 2025
3ce0f96
Merge remote-tracking branch 'origin/master' into 49-add-model-genera…
pirs1337 Jan 10, 2025
e35ed3e
feat: add tests
pirs1337 Jan 10, 2025
db624fc
refactor: code
pirs1337 Jan 10, 2025
8442381
refactor: code
pirs1337 Jan 14, 2025
3bde64f
refactor: code
pirs1337 Jan 15, 2025
8e61ee5
Update tests/Support/Factory/FactoryMockTrait.php
DenTray Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
);
}

Expand All @@ -42,7 +42,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'),
]);
}

Expand All @@ -60,8 +60,8 @@ public function prepareRelatedModels(): void
if (!$this->classExists('models', $relation)) {
$this->throwFailureException(
ClassNotExistsException::class,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add arg names for multiline style

"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'."
);
}

Expand All @@ -70,7 +70,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);
Expand All @@ -80,9 +80,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);
}
Expand All @@ -97,7 +97,7 @@ public function prepareRelations(): array
$result[] = [
'name' => $this->getRelationName($relation, $type),
'type' => $type,
'entity' => $relation
'entity' => $relation,
];
}
}
Expand All @@ -106,7 +106,7 @@ public function prepareRelations(): array
return $result;
}

protected function getCasts($fields): array
protected function getCasts(array $fields): array
{
$casts = [
'boolean-required' => 'boolean',
Expand All @@ -117,7 +117,7 @@ protected function getCasts($fields): array
$result = [];

foreach ($fields as $fieldType => $names) {
if (empty($casts[$fieldType])) {
if (!array_key_exists($fieldType, $casts)) {
continue;
}

Expand All @@ -129,7 +129,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);

Expand Down
2 changes: 1 addition & 1 deletion stubs/relation.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public function {{$name}}()
{
return $this->{{$type}}({{$entity}}::class);
}
}
70 changes: 70 additions & 0 deletions tests/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace RonasIT\Support\Tests;

use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just use MockTrait into Generator MockTrait?


public function testModelAlreadyExists()
{
$this->mockGeneratorForExistingModel();

$this->assertExceptionThrew(
className: ClassAlreadyExistsException::class,
message: 'Cannot create Post Model cause Post Model already exists. Remove Post Model.',
);

app(ModelGenerator::class)
->setModel('Post')
->generate();
}

public function testRelationModelMissing()
{
$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')
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => [],
'belongsTo' => [],
'belongsToMany' => [],
])
->generate();
}

public function testCreateModel()
{
$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->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');
}
}
53 changes: 53 additions & 0 deletions tests/Support/Model/ModelMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

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 mockGeneratorForMissingRelationModel(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems this method is not using

{
$this->mockClass(ModelGenerator::class, [
[
'function' => 'classExists',
'arguments' => ['models', 'Post'],
'result' => false,
],
[
'function' => 'classExists',
'arguments' => ['models', 'Comment'],
'result' => false,
],
]);
}

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);
}
}
11 changes: 11 additions & 0 deletions tests/Support/Model/RelationModelMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


namespace RonasIT\Support\Tests\Support\Model;

class RelationModelMock
{
public function some_relation()
{

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}
}
16 changes: 16 additions & 0 deletions tests/fixtures/ModelGeneratorTest/comment_relation_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace RonasIT\Support\Tests\Support\Model;

class RelationModelMock
{
public function some_relation()
{

}

public function post()
{
return $this->belongsTo(Post::class);
}
}
31 changes: 31 additions & 0 deletions tests/fixtures/ModelGeneratorTest/new_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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;

protected $fillable = [
'media_id',
'is_published',
];

protected $hidden = ['pivot'];

public function comment()
{
return $this->hasOne(Comment::class);
}
public function users()
{
return $this->hasMany(User::class);
}
protected $casts = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we change casts place after the hidden property

'is_published' => 'boolean',
];
}
16 changes: 16 additions & 0 deletions tests/fixtures/ModelGeneratorTest/user_relation_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace RonasIT\Support\Tests\Support\Model;

class RelationModelMock
{
public function some_relation()
{

}

public function post()
{
return $this->belongsTo(Post::class);
}
}
Loading