-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
8c7116b
8dc9b15
08c0965
d2e27c0
ba8a9f0
fe2e1f3
45de444
6551491
523ad40
14abfa0
3ae2cdc
d185d9c
f316530
f871c5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
public function {{$name}}() | ||
{ | ||
return $this->{{$type}}({{$entity}}::class); | ||
} | ||
} |
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'); | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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() | ||||
{ | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
} | ||||
} |
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); | ||
} | ||
} |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we change |
||
'is_published' => 'boolean', | ||
]; | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
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