Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model generator tests #70

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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;

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

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

Choose a reason for hiding this comment

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

please check why no empty line between relations

{
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