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 repository generator tests #73

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8c7116b
test: add model generation test.
Dec 20, 2023
ae0b552
test: add repository generator test skeleton.
Dec 21, 2023
11a1c0c
Merge branch 'master' into 49-add-repository-generator-tests
Dec 21, 2023
db89113
chore: correct tests.
Dec 21, 2023
6071eda
test: add tests for repository generator.
Dec 21, 2023
f7f1b13
chore: remove fixture export.
Dec 21, 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
4ca3368
Merge remote-tracking branch 'origin/49-add-model-generator-tests' in…
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
694ee92
refactor: tests
pirs1337 Dec 17, 2024
fec7db2
refactor: remove useless classes
pirs1337 Dec 18, 2024
47b57bd
Merge remote-tracking branch 'origin/master' into 49-add-repository-g…
pirs1337 Dec 18, 2024
e29f790
Merge remote-tracking branch 'origin/49-add-model-generator-tests-2' …
pirs1337 Dec 18, 2024
3f66a2f
feat: change tests
pirs1337 Dec 18, 2024
3ae2cdc
refactor: code
pirs1337 Dec 19, 2024
d185d9c
refactor: code
pirs1337 Dec 19, 2024
f316530
refactor: code
pirs1337 Dec 19, 2024
3fca4a6
Merge remote-tracking branch 'origin/49-add-model-generator-tests' in…
pirs1337 Dec 19, 2024
8a4cd74
refactor: code
pirs1337 Dec 19, 2024
ef0b6e1
Merge branch 'master' into 49-add-repository-generator-tests
DenTray Dec 23, 2024
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,
"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);
}
}
1 change: 0 additions & 1 deletion tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ className: ViewException::class,
])
->setModel('Post')
->generate();

}

public function testCreateSuccess()
Expand Down
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');
}
}
38 changes: 38 additions & 0 deletions tests/RepositoryGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace RonasIT\Support\Tests;

use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Generators\RepositoryGenerator;
use RonasIT\Support\Tests\Support\Repository\RepositoryMockTrait;

class RepositoryGeneratorTest extends TestCase
{
use RepositoryMockTrait;

public function testModelDoesntExists()
{
$this->mockGeneratorForMissingModel();

$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: "Cannot create Post Model cause Post Model does not exists. "
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'.",
);

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

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

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

$this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.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
{
$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()
{

}
}
34 changes: 34 additions & 0 deletions tests/Support/Repository/RepositoryMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace RonasIT\Support\Tests\Support\Repository;

use org\bovigo\vfs\vfsStream;
use RonasIT\Support\Generators\RepositoryGenerator;
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
use RonasIT\Support\Traits\MockTrait;

trait RepositoryMockTrait
{
use GeneratorMockTrait, MockTrait;

public function mockGeneratorForMissingModel(): void
{
$this->mockClass(RepositoryGenerator::class, [
$this->classExistsMethodCall(['models', 'Post'], false),
]);
}

public function mockFilesystem(): void
{
$structure = [
'app' => [
'Models' => [
'Post.php' => '<?php',
],
'Repositories' => [],
],
];

vfsStream::create($structure);
}
}
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 = [
'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
Loading