erikgall/eloquent-phpunit

使用PHPUnit测试Eloquent模型、数据库模式与表、关系/外键。

v1.0.7 2016-11-08 21:58 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:12 UTC


README

StyleCI

测试您的Laravel Eloquent模型和数据库模式

本包受到Ruby on Rails世界和测试框架RSpec的启发。Ruby on Rails社区(大多数情况下)以检查模型属性、关系、数据库表和列设置(默认值、可为空等)的方式来编写模型测试。

目录

  1. 可测试的内容
  2. 安装
  3. 需求
  4. 文档
    1. 属性
    2. 表测试方法
    3. 模型测试方法
  5. 示例模型测试类
  6. 贡献
  7. 版本发布历史
  8. 使用Eloquent-PHPUnit的项目
  9. 作者
  10. 许可证

Test a database table

Test an eloquent model's attributes/relationships

可测试的内容

  • 属性数组
  • 可填充属性数组
  • 隐藏属性数组
  • 日期属性数组
  • 关系方法

您还可以测试您的数据库表,例如

  • 表存在
  • 表列存在
  • 表列类型(字符串、文本、日期、日期时间、布尔值等)。
  • 列默认值
  • 空/非空
  • 自增主键。
  • 表索引
  • 唯一索引
  • 外键关系

安装

  1. 使用终端中的composer使用/安装此包是最简单的方法
composer require erikgall/eloquent-phpunit
  1. 或者,您可以将以下行添加到您的composer.json文件中的require-dev依赖项中
{
	"require-dev": {
		"erikgall/eloquent-phpunit": "~1.0"
	}
}

需求

此包需要PHP 5.6PHP 7+。它已与Laravel 5.2Laravel 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'); 
	}
}

贡献

  1. 分叉它。
  2. 创建您的分支: git checkout -b my-new-feature
  3. 提交您的更改: git commit -am 'Add some feature'
  4. 将分支推送到: git push origin my-new-feature
  5. 提交pull请求。

历史

  • v1.0.0 发布:2016/8/5
  • v1.0.3 发布:2016/8/9
  • v1.0.6 发布:2016/9/21

使用Eloquent-PHPUnit的项目

作者

许可证

Eloquent-PHPUnit 是一个开源软件,采用 MIT 许可协议