paasky / laravel-model-test
Tests Eloquent Model instances and relations
Requires
- php: >=7.2
- laravel/framework: >=6
- phpunit/phpunit: >=8
- roave/better-reflection: >=6
- wyrihaximus/list-classes-in-directory: >=1.5
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Trait to test that all your Laravel Models are instances of correct classes and all relations work
Installation
composer require paasky/laravel-model-test --dev
Usage
In any PHPUnit test
use Paasky\TestsModels;
class MyTest extends TestCase
{
use RefreshDatabase, TestsModels;
public function testModels()
{
$this->assertModels();
}
}
Configuration
Configurations are public attributes of the TestsModels-Trait
modelPaths
Full path(s) to look in for Model classes, includes sub-folders.
Default: [app_path('Models')]
$this->modelPaths = [app_path('Models'), app_path('SuperCoolModels')];- Tip: If your models are directly in
app, you can skip auto-discovery and pass an array of classes toassertModels():
$this->assertModels([User::class, SomethingElse::class, ...]);
allowedInstances
Classes that found classes can be an instance of. Useful if your project has a custom Model-class.
Default: [Model::class]
$this->allowedInstances = [ProjectModel::class];
allowNonModels
Skip or fail classes that are not instances of Illuminate\Database\Eloquent\Model
Default: false (Fail)
$this->allowNonModels = true;
requiredInstancePerModel
Override allowedInstances for specific classes by setting the required instance.
Default: ['App\User' => Authenticatable::class, 'App\Models\User' => Authenticatable::class]
$this->requiredInstancePerModel[SimpleUser::class] = Model::class;
ignoreMethodsPerNamespace
Ignore these methods from validation, useful when packages don't typehint the return type and are failing
Use '*' to ignore all methods in the namespace
Default: ['Illuminate\\' => ['*']
$this->ignoreMethodsPerNamespace['SomeDude\\Package\\'] = ['dumbMethod'];
enableBackRelationValidation
Should back relations (eg User has Post, so Post must have User) be validated
Default: true
$this->enableBackRelationValidation = false;
enableBackRelationTypeValidation
Should back relation return types (eg User HasMany Posts, so Post must BelongTo User) be validated
Default: true
$this->enableBackRelationTypeValidation = false;
skipBackRelationMethodsValidationPerModel
Methods to skip for back relation validation
Use '*' to ignore all methods of the class
Default: ['App\User' => ['tokens'], 'App\Models\User' => ['tokens']]
$this->skipBackRelationMethodsValidationPerModel[PivotModel::class] = ['*'];