Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 7 additions & 6 deletions src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
Expand Down Expand Up @@ -287,12 +288,12 @@ protected function getCrudOptions()

protected function getRelations()
{
return [
'hasOne' => $this->option('has-one'),
'hasMany' => $this->option('has-many'),
'belongsTo' => $this->option('belongs-to'),
'belongsToMany' => $this->option('belongs-to-many')
];
return new RelationsDTO(
hasOne: $this->option('has-one'),
hasMany: $this->option('has-many'),
belongsTo: $this->option('belongs-to'),
belongsToMany: $this->option('belongs-to-many'),
);
}

protected function getFields()
Expand Down
24 changes: 24 additions & 0 deletions src/DTO/RelationsDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RonasIT\Support\DTO;

class RelationsDTO
{
public function __construct(
public array $hasOne = [],
public array $hasMany = [],
public array $belongsTo = [],
public array $belongsToMany = [],
) {
}

public function toArray(): array
{
return [
'hasOne' => $this->hasOne,
'hasMany' => $this->hasMany,
'belongsTo' => $this->belongsTo,
'belongsToMany' => $this->belongsToMany,
];
}
}
5 changes: 4 additions & 1 deletion src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
Expand Down Expand Up @@ -76,8 +77,10 @@ public function setFields($fields)
* @param array $relations
* @return $this
*/
public function setRelations($relations)
public function setRelations(RelationsDTO $relations)
{
$relations = $relations->toArray();

$this->relations = $relations;

foreach ($relations['belongsTo'] as $field) {
Expand Down
3 changes: 2 additions & 1 deletion src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;

class RequestsGenerator extends EntityGenerator
Expand All @@ -14,7 +15,7 @@ class RequestsGenerator extends EntityGenerator
const DELETE_METHOD = 'Delete';
const GET_METHOD = 'Get';

public function setRelations($relations)
public function setRelations(RelationsDTO $relations)
{
parent::setRelations($relations);

Expand Down
5 changes: 4 additions & 1 deletion src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;

class ServiceGenerator extends EntityGenerator
{
public function setRelations($relations)
public function setRelations(RelationsDTO $relations)
{
$relations = $relations->toArray();

foreach ($relations['belongsTo'] as $field) {
$name = Str::snake($field) . '_id';

Expand Down
25 changes: 10 additions & 15 deletions tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Event;
use Illuminate\View\ViewException;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
Expand Down Expand Up @@ -63,11 +64,7 @@ className: ViewException::class,
->setFields([
'another_type' => ['some_field'],
])
->setRelations([
'hasOne' => [],
'hasMany' => [],
'belongsTo' => [],
])
->setRelations(new RelationsDTO())
->setModel('Post')
->generate();
}
Expand All @@ -82,11 +79,10 @@ public function testCreateSuccess()
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setRelations(new RelationsDTO(
hasOne: ['user'],
belongsTo: ['user'],
))
->setModel('Post')
->generate();

Expand All @@ -110,11 +106,10 @@ public function testCreateFactoryWithoutFactoryStub(): void
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setRelations(new RelationsDTO(
hasOne: ['user'],
belongsTo: ['user'],
))
->setModel('Post')
->generate();

Expand Down
29 changes: 5 additions & 24 deletions tests/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RonasIT\Support\Tests;

use Illuminate\Support\Carbon;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
use RonasIT\Support\Generators\MigrationGenerator;
Expand All @@ -18,12 +19,7 @@ className: UnknownFieldTypeException::class,

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'unknown-type' => ['title'],
Expand All @@ -37,12 +33,7 @@ public function testCreateMigration()

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'string' => ['title', 'body'],
Expand All @@ -62,12 +53,7 @@ public function testCreateMigrationMYSQL()

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'string' => ['title', 'body'],
Expand All @@ -88,12 +74,7 @@ public function testCreateMigrationWithoutMigrationStub(): void

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'string' => ['title', 'body'],
Expand Down
30 changes: 12 additions & 18 deletions tests/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
Expand Down Expand Up @@ -39,12 +40,9 @@ className: ClassNotExistsException::class,

app(ModelGenerator::class)
->setModel('Post')
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => [],
'belongsTo' => [],
'belongsToMany' => [],
])
->setRelations(new RelationsDTO(
hasOne: ['Comment']
))
->generate();
}

Expand All @@ -58,12 +56,10 @@ public function testCreateModel()
'integer-required' => ['media_id'],
'boolean-required' => ['is_published'],
])
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => ['User'],
'belongsTo' => [],
'belongsToMany' => [],
])
->setRelations(new RelationsDTO(
hasOne: ['Comment'],
hasMany: ['User'],
))
->generate();

$this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php');
Expand Down Expand Up @@ -125,12 +121,10 @@ public function testCreateModelWithRelationsRelationStubNotExist()
app(ModelGenerator::class)
->setModel('Post')
->setFields([])
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => ['User'],
'belongsTo' => [],
'belongsToMany' => [],
])
->setRelations(new RelationsDTO(
hasOne: ['Comment'],
hasMany: ['User'],
))
->generate();

$this->assertFileDoesNotExist('new_model.php', 'app/Models/Post.php');
Expand Down
11 changes: 5 additions & 6 deletions tests/RequestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Generators\RequestsGenerator;
Expand All @@ -12,12 +13,10 @@ public function testCreateRequests()
{
app(RequestsGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => ['User'],
'hasMany' => ['Comments'],
'hasOne' => [],
'belongsToMany' => []
])
->setRelations(new RelationsDTO(
hasMany: ['Comments'],
belongsTo: ['User'],
))
->setFields([
'boolean-required' => ['is_published'],
'integer' => ['user_id'],
Expand Down
31 changes: 13 additions & 18 deletions tests/SeederGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Generators\SeederGenerator;

Expand All @@ -10,12 +11,10 @@ class SeederGeneratorTest extends TestCase
public function testCreateSeeder()
{
app(SeederGenerator::class)
->setRelations([
'hasOne' => [],
'belongsTo' => ['User'],
'hasMany' => ['Comment'],
'belongsToMany' => []
])
->setRelations(new RelationsDTO(
hasMany: ['Comment'],
belongsTo: ['User'],
))
->setModel('Post')
->generate();

Expand All @@ -28,12 +27,10 @@ public function testCreateSeederEmptyDatabaseSeederStubNotExist()
config(['entity-generator.stubs.database_empty_seeder' => 'entity-generator::database_seed_empty']);

app(SeederGenerator::class)
->setRelations([
'hasOne' => [],
'belongsTo' => ['User'],
'hasMany' => ['Comment'],
'belongsToMany' => []
])
->setRelations(new RelationsDTO(
hasMany: ['Comment'],
belongsTo: ['User'],
))
->setModel('Post')
->generate();

Expand All @@ -51,12 +48,10 @@ public function testCreateSeederEntityDatabaseSeederStubNotExist()
config(['entity-generator.stubs.seeder' => 'incorrect_stub']);

app(SeederGenerator::class)
->setRelations([
'hasOne' => [],
'belongsTo' => ['User'],
'hasMany' => ['Comment'],
'belongsToMany' => []
])
->setRelations(new RelationsDTO(
hasMany: ['Comment'],
belongsTo: ['User'],
))
->setModel('Post')
->generate();

Expand Down
Loading