This package allows you to test your models about table structures, relations and more
You can install the package via composer:
composer require thomasdominic/models-testor
To use this package, you have to generate factories for your models. (See Factories Documentation)
You can generate one test file by model or by several. For your model MyModel
you can use this command for example:
php artisan make:test Models/MyModelTest
With this structure
users
id - integer
name - string
other_field - string
you can test if you have all the fields you need and if they are fillable.
class UserTest extends TestCase
{
use ModelsTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasColumns(['id','name','other_field'])
->assertCanFillables(['name','other_field']);
}
}
You can test relations of your models. For example, with this structure
categories
id - integer
name - string
customers
id - integer
name - string
category_id - integer
type_id - integer
you can use assertHasHasManyRelations
and assertHasBelongsToRelations
methods like this
class CategoryTest extends TestCase
{
use ModelsTestor;
public function test_have_category_model()
{
$this->modelTestable(Category::class)
->assertHasHasManyRelation(Customer::class);
}
}
class CustomerTest extends TestCase
{
use ModelsTestor;
public function test_have_customer_model()
{
$this->modelTestable(Customer::class)
->assertHasBelongsToRelation(Category::class);
}
}
If you don't use Laravel naming convention, you may also override the relation and local keys (for belongsTo relation) by passing
additional arguments to the assertHasHasManyRelations
and assertHasBelongsToRelations
methods
$this->modelTestable(Customer::class)
->assertHasBelongsToRelation(Category::class,'category','category_id');
$this->modelTestable(Category::class)
->assertHasHasManyRelation(Customer::class,'customers');
If you have several relations, you can chain methods like this:
$this->modelTestable(Customer::class)
->assertHasBelongsToRelation(Category::class)
->assertHasBelongsToRelation(OtherModel::class);
You can test your ManyToMany relations with the ManyToManyRelationsTestable
trait.
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
class UserTest extends TestCase
{
use ModelsTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasManyToManyRelation(Role::class);
}
}
class RoleTest extends TestCase
{
use ModelsTestor;
public function test_have_role_model()
{
$this->modelTestable(User::class)
->assertHasManyToManyRelation(User::class);
}
}
You can override the relation argument too :
$this->modelTestable(User::class)
->assertHasManyToManyRelation(User::class,'users');
If you have a Morph Relation,
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
you can use assertHasBelongsToMorphRelations
and assertHasHasManyMorphRelations
methods like this
class PostTest extends TestCase
{
use ModelsTestor;
public function test_have_post_model()
{
$this->modelTestable(Post::class)
->assertHasHasManyMorphRelation(Comment::class,'comments');
}
}
class VideoTest extends TestCase
{
use ModelsTestor;
public function test_have_video_model()
{
$this->modelTestable(Video::class)
->assertHasHasManyMorphRelation(Comment::class,'comments');
}
}
class CommentTest extends TestCase
{
use ModelsTestor;
public function test_have_morph_model_model()
{
$this->modelTestable(Comment::class)
->assertHasBelongsToMorphRelation(Post::class,'commentable')
->assertHasBelongsToMorphRelation(Video::class,'commentable');
}
}
You can test if a table contains columns with the tableTestable
method
class MyPivotTest extends TestCase
{
public function test_have_table_without_model()
{
$this->tableTestable('pivot_table')
->assertHasColumns(['first_model_id','second_model_id','other_property']);
}
}
composer test
Please see CHANGELOG for more information about what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email dthomas@codenco.io instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.