Download the PHP package codenco-dev/eloquent-model-tester without Composer
On this page you can find all versions of the php package codenco-dev/eloquent-model-tester. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codenco-dev/eloquent-model-tester
More information about codenco-dev/eloquent-model-tester
Files in codenco-dev/eloquent-model-tester
Package eloquent-model-tester
Short Description Test easier your Eloquent Models in your Laravel Project
License MIT
Homepage https://github.com/thomasdominic/eloquent-model-tester
Informations about the package eloquent-model-tester
Helper for Testing structures, relations of your models in Laravel
This package allows you to test your models about table structures, relations and more
Installation
You can install the package via composer:
Usage
To use this package, you have to generate factories for your models. (
See Factories Documentation)
You can generate one test file by model or for several. For your model MyModel
you can use this command for example:
To be able to test database, don't forget RefreshDatabase
use statements.
For more simplicity, you can put the RefreshDatabase
use statement in tests/TestCase.php
file
Test of structure and of fillable / guarded
With this structure
users
id - integer
name - string
email - string
password - string
remember_token - string
other_field - string
created_at - timestamp
updated_at - timestamp
you can test if you have all the fields you need and if they are fillable or guarded.
The function assertHasColumns()
only checks if the values provided are in the schema of the table. If you want to
ensure that no other columns are present, you can use the stricter assertHasOnlyColumns()
assertion to check that
only the column names provided are present in the table.
N.B. When using this you will need to provide the created_at
and updated_at
fields if they are present in the
database table.
The functions assertHasColumnsInFillable()
and assertHasColumnsInGuarded()
only check if the $fillable
and $guarded
arrays contain the values passed. If you want to ensure that no other entries are in the $fillable
and $guarded
arrays, then you can use the stricter assertHasOnlyColumnsInFillable()
and assetHasOnlyColumnsInGuarded()
functions as follows:
To further confirm that only a set of columns can be filled, you can use the assertCanOnlyFill()
assertion to confirm
this. This assertion confirms that the fields provided are the only columns that can be filled, by confirming that these
are the only values in the $fillable
array and they don't appear in the $guarded
array.
To confirm that a field does not appear in both the $fillable
and $guarded
arrays by mistake there is
the assertNoGuardedAndFillableFields()
assertion that checks that no entry appears in both:
To check for soft delete deleted_at
column on your model, you can use the assertHasSoftDeleteTimestampColumns()
assertion:
user:
id - integer
name - string
deleted_at - timestamp
HasOne et BelongsTo
You can test relations of your models. For example, with this structure
user
id - integer
name - string
phones
id - integer
number - string
user_id - integer
you can use assertHasHasOneRelation()
and assertHasBelongsToRelations
methods like this:
HasMany et BelongsTo
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
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
If you have several relations, you can chain methods like this:
HasManyThrough relations
You can test has many through relations of your models. For example, with this structure
customers
id - integer
name - string
locations
id - integer
customer_id - integer
orders
id - integer
location_id - integer
you can use assertHasHasManyThroughRelations
method like this
If you don't use Laravel naming convention, you may also override the relation and foreign keys by passing additional
arguments to the assertHasHasManyThroughRelations
method
Attention: as there is no official inverse of this relationship, it is not possible to use this assertion in the
reverse, i.e., in the orders
model checking for a customers
relationship.
Many to Many relations
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
You can override the relation argument too :
Morph Relations
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
MorphOne Relations
If you have a Morph One Relation,
posts
id - integer
title - string
body - text
users
id - integer
title - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
you can use assertHasBelongsToMorphRelations
and assertHasMorphOneRelations
methods like this
Pivot and table without Model
You can test if a table contains columns with the tableTestable
method
Model Scopes
You can test if a model has a query scope defined using the assertHasScope()
assertion:
Of course if you're using Dynamic Scopes then assertHasScope()
takes the arguments to pass to the query scope:
Available Assertions
Assertion | Description | Notes |
---|---|---|
assertHasTimestampsColumns() |
checks if the created_at and updated_at columns are present in the table. |
|
assertHasSoftDeleteTimestampColumns() |
checks if the deleted_at column is present in the table. |
|
assertHasColumns() |
checks for the presence of the provided column names appear in the table. | |
assertHasOnlyColumns() |
checks that only the column names provided are the only columns of the table. | |
assertCanOnlyFill() |
checks that only the fields provided can be filled and no others. | |
assertCanFillables() |
checks if the fields provided can be filled. | Deprecated - Alias of assertHasColumnsInFillable() |
assertHasColumnsInFillable() |
checks if the fields provided can be filled. | |
assertHasOnlyColumnsInFillable() |
checks if only the fields provided are those that appear in the $fillable array. | |
assertHasColumnsInGuarded() |
checks if the fields provide are guarded. | |
assertHasOnlyColumnsInGuarded() |
checks if the only the fields provided are those that appear in the $guarded array. | |
assertHasNoGuardedAndFillableFields() |
checks that a column does not appear in both the $fillable and $guarded arrays. | |
assertHasHasOneRelation() |
checks that the model has the HasOne relation. |
|
assertHasMorphOneRelation() |
checks that the model has the MorphOne relation. |
|
assertHasHasManyRelation() |
checks that the model hsa the HasMany relation. |
|
assertHasHasManyThroughRelation() |
checks that the model has the HasManyThrough relation. |
|
assertHasBelongsToRelation() |
checks that the model has the BelongsTo relation. |
|
assertHasManyToManyRelation() |
checks that the model has the ManyToMany relation. |
|
assertHasHasManyMorphRelation() |
checks that the model has the HasManyMorph relation. |
|
assertHasBelongsToMorphRelation() |
checks that the model has the BelongsToMorph relation. |
|
assertHasScope() |
checks that the model has the scope. |
Testing
Changelog
Please see CHANGELOG for more information about what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] or contact me on Twitter DomThomasEs instead of using the issue tracker.
Credits
- Dominique THOMAS
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.