thomasdominic/eloquent-model-testor

此包已被弃用且不再维护。未建议替代包。

在 Laravel 项目中轻松测试 Eloquent 模型

v0.9.2 2020-03-06 18:16 UTC

This package is auto-updated.

Last update: 2020-06-28 08:13:37 UTC


README

Latest Version on Packagist StyleCI Build Status Quality Score Total Downloads

此包允许您测试模型关于表结构、关系等方面的内容

安装

您可以通过 composer 安装此包

composer require thomasdominic/eloquent-model-testor --dev

使用

要使用此包,您必须为您的模型生成工厂。(参见 工厂文档)您可以通过模型或多个模型生成一个测试文件。例如,对于您的模型 MyModel,您可以使用以下命令

php artisan make:model MyModel -mf
php artisan make:test Models/MyModelTest

结构和可填充测试

使用此结构

users
    id - integer
    name - string
    other_field - string 

您可以测试是否包含所有需要的字段,以及它们是否可填充。

class UserTest extends TestCase
{
    use HasModelTestor;
    
    public function test_have_user_model()
    {
        $this->modelTestable(User::class)
            ->assertHasColumns(['id','name','other_field'])
            ->assertCanFillables(['name','other_field']);
    }

}

HasMany 和 BelongsTo

您可以使用此结构测试模型的关系。例如,使用此结构

categories
    id - integer
    name - string

customers
    id - integer
    name - string
    category_id - integer
    type_id - integer

您可以使用 assertHasHasManyRelationsassertHasBelongsToRelations 方法,如下所示

class CategoryTest extends TestCase
{
    
    use HasModelTestor;
    
    public function test_have_category_model()
    {
        $this->modelTestable(Category::class)
            ->assertHasHasManyRelation(Customer::class);
    }

}

class CustomerTest extends TestCase
{
    use HasModelTestor;

    public function test_have_customer_model()
    {
        $this->modelTestable(Customer::class)
            ->assertHasBelongsToRelation(Category::class);
    }
}

如果您不使用 Laravel 命名约定,您也可以通过传递额外的参数来覆盖关系和本地键(对于 belongsTo 关系)

    $this->modelTestable(Customer::class)
            ->assertHasBelongsToRelation(Category::class,'category','category_id');

    $this->modelTestable(Category::class)
            ->assertHasHasManyRelation(Customer::class,'customers');

如果您有多个关系,您可以像这样链式调用方法

    $this->modelTestable(Customer::class)
            ->assertHasBelongsToRelation(Category::class)
            ->assertHasBelongsToRelation(OtherModel::class);
    

多对多关系

您可以使用 ManyToManyRelationsTestable 特性测试您的多对多关系。

users
    id - integer
    name - string

roles
    id - integer
    name - string

role_user
    user_id - integer
    role_id - integer
class UserTest extends TestCase
{
     use HasModelTestor;
     
    public function test_have_user_model()
    {
        $this->modelTestable(User::class)
            ->assertHasManyToManyRelation(Role::class);
    }


}

class RoleTest extends TestCase
{
    use HasModelTestor;

    public function test_have_role_model()
    {
        $this->modelTestable(User::class)
            ->assertHasManyToManyRelation(User::class);
    }

}

您也可以覆盖关系参数

    $this->modelTestable(User::class)
            ->assertHasManyToManyRelation(User::class,'users');

形态关系

如果您有形态关系

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

您可以使用 assertHasBelongsToMorphRelationsassertHasHasManyMorphRelations 方法,如下所示

class PostTest extends TestCase
{
    
    use HasModelTestor;
                
    public function test_have_post_model()
        {
            $this->modelTestable(Post::class)
                ->assertHasHasManyMorphRelation(Comment::class,'comments');
        }
}

class VideoTest extends TestCase
{
    use HasModelTestor;
    
    public function test_have_video_model()
        {
            $this->modelTestable(Video::class)
                ->assertHasHasManyMorphRelation(Comment::class,'comments');
        }
}

class CommentTest extends TestCase
{
    
    use HasModelTestor;
    
    public function test_have_morph_model_model()
    {
        $this->modelTestable(Comment::class)
           ->assertHasBelongsToMorphRelation(Post::class,'commentable')
           ->assertHasBelongsToMorphRelation(Video::class,'commentable');
    }
}

枢纽表和无模型表

您可以使用 tableTestable 方法测试表是否包含具有 tableTestable 方法的列

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

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何安全相关的问题,请通过电子邮件 dthomas@codenco.io 反馈,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。

Laravel包模板

此包是使用Laravel包模板生成的。