erikgall / eloquent-phpunit
使用PHPUnit测试Eloquent模型、数据库模式与表、关系/外键。
Requires
- doctrine/dbal: ^2.5
- laravel/framework: ^5.2|^5.3
README
测试您的Laravel Eloquent模型和数据库模式
本包受到Ruby on Rails世界和测试框架RSpec的启发。Ruby on Rails社区(大多数情况下)以检查模型属性、关系、数据库表和列设置(默认值、可为空等)的方式来编写模型测试。
目录
可测试的内容
- 属性数组
- 可填充属性数组
- 隐藏属性数组
- 日期属性数组
- 关系方法
您还可以测试您的数据库表,例如
- 表存在
- 表列存在
- 表列类型(字符串、文本、日期、日期时间、布尔值等)。
- 列默认值
- 空/非空
- 自增主键。
- 表索引
- 唯一索引
- 外键关系
安装
- 使用终端中的composer使用/安装此包是最简单的方法
composer require erikgall/eloquent-phpunit
- 或者,您可以将以下行添加到您的
composer.json
文件中的require-dev
依赖项中
{ "require-dev": { "erikgall/eloquent-phpunit": "~1.0" } }
需求
此包需要PHP 5.6
或PHP 7+
。它已与Laravel 5.2
和Laravel 5.3
进行测试和使用。应该没有问题与Laravel 5.0/1
一起使用,但尚未进行测试或确认100%。
文档
测试类属性
*这些设置仅在seedDatabase属性未设置为false时使用(seedDatabase属性的默认值为true)。
** 主题属性是\Illuminate\Database\Eloquent\Model的实例。
数据库测试方法
\EGALL\EloquentPHPUnit\Database\Table
通过调用table属性来获取EGALL\EloquentPHPUnit\Database\Table
类实例。
用法
$this->table
表方法
column($columnName)
为传递给表的列名初始化一个新的EGALL\EloquentPHPUnit\Database\Column
类实例。
用法
$this->table->column('column_name')
返回: EGALL\EloquentPHPUnit\Database\Column
exists()
断言表存在于数据库中。
用法
$this->table->exists();
返回: EGALL\EloquentPHPUnit\Database\Table
hasTimestamps()
断言表有时间戳列。
用法
$this->table->hasTimestamps();
返回: EGALL\EloquentPHPUnit\Database\Table
resetTable($tableName)
使用此方法可以在一个测试类中测试多个表。
用法
下面的使用代码使用用户/用户-角色示例。关系如下:一个用户可以有多个角色,一个角色可以有多个用户(多对多)。在eloquent中,我们会将这种关系描述为用户通过user_role表belongsToMany
角色,反之亦然。
protected $model = 'App\Role'; public function testDatabase() { // We are testing the roles table below $this->table->column('id')->increments(); $this->table->column('name')->string()->notNull()->unique(); $this->table->column('label')->string()->notNull(); $this->table->hasTimestamps(); // To switch to the user_role table we must reset the table. // You can method chain off of the resetTable() method as well. $this->resetTable('user_role'); // Begin testing user_role table like normal. $this->table->column('role_id')->integer()->primary()->foreign('roles'); $this->table->column('user_id')->integer()->primary()->foreign('roles'); $this->table->hasTimestamps(); }
返回: EGALL\EloquentPHPUnit\EloquentTestCase
/ $this
模型测试方法
// TODO
示例模型测试
Class UserModelTest extends \EGALL\EloquentPHPUnit\EloquentTestCase { protected $model = 'App\User'; // If you want to run the DatabaseSeeder class protected $seedDatabase = true; // If you only want to run a specific seeder protected $seeders = ['UsersTableSeeder', 'SchoolsTableSeeder']; // Change the default seeder that calls the rest of your seeders. // The default is the default Laravel Seeder named: DatabaseSeeder. // Ex. (You have a TestDatabaseSeeder and the default DatabaseSeeder). protected $defaultSeeder = 'TestDatabaseSeeder' /** * Test the database table. */ public function testDatabaseTable() { $this->table->column('id')->integer()->increments(); $this->table->column('name')->string()->nullable(); $this->table->column('email')->string()->notNullable()->unique(); $this->table->column('password')->string()->notNullable(); $this->table->column('dob')->date()->nullable(); $this->table->column('avatar_id')->integer()->foreign('images', 'id', $onUpdate = 'cascade', $onDelete = 'cascade'); $this->table->column('is_verified')->boolean()->defaults(false); $this->table->column('is_admin')->boolean()->defaults(false); $this->table->column('verification_sent_at')->dateTime()->nullable(); $this->table->column('invite_sent_at')->dateTime()->nullable(); $this->table->column('api_token')->string()->index(); $this->table->column('remember_token')->string()->nullable(); $this->table->hasTimestamps(); } /** * Test the model's properties. */ public function testModelProperties() { $this->hasFillable('name', 'email', 'password', 'dob', 'avatar_id') ->hasHidden('password', 'remember_token') ->hasCasts('is_verified', 'boolean') // or ->hasCasts(['is_verified' => 'boolean', 'is_admin' => 'boolean']) ->hasDates('verification_sent_at', 'invite_sent_at') ->belongsTo(Image::class) // if method name = 'image()' or ->belongsTo(Image::class, $customMethod = 'avatar') ->hasMany(Profile::class) ->morphsTo($method = 'slug', $morphTo = 'sluggable') // or: example below assumes the db fields are: 'sluggable_id' and 'sluggable_type' ->morphsTo('sluggable'); } }
贡献
- 分叉它。
- 创建您的分支:
git checkout -b my-new-feature
- 提交您的更改:
git commit -am 'Add some feature'
- 将分支推送到:
git push origin my-new-feature
- 提交pull请求。
历史
- v1.0.0 发布:2016/8/5
- v1.0.3 发布:2016/8/9
- v1.0.6 发布:2016/9/21
使用Eloquent-PHPUnit的项目
- Canvas ★803:由 @austintoddj 构建的一个简单、强大的基于 Laravel 5 的博客发布平台。
作者
许可证
Eloquent-PHPUnit 是一个开源软件,采用 MIT 许可协议。