developeruz/yii-matcher

适用于 Yii 2.x 的 shoulda matchers 测试用例

安装: 19

依赖: 0

建议者: 0

安全性: 0

星标: 5

关注者: 2

分支: 2

开放问题: 0

类型:yii2-extension

0.0.3 2015-07-20 08:10 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:53:37 UTC


README

为什么要进行测试? Yii 框架的开发者测试并保证了验证规则的正确性。但他们不保证您是否在模型中遗漏了规则,或者后来删除了一些规则。因此,为模型编写单元测试非常重要。该类可以简化您对模型验证规则的测试。

安装:##

$ php composer.phar require developeruz/yii-matcher "*"

使用示例:##

use developeruz\yii_matcher\ModelMatcher;

class ValidateTest extends TestCase {

    public function testPhoneIsSafeOnlyInRegistration()
    {
         $userModel = new ModelMatcher('app\models\User');
         $userModel->shouldBeSafe('phone', 'registration');
         $userModel->shouldBeNotSafe('phone');
    }
    
     public function testUserHasPostsRelation()
     {
         $userModel = new ModelMatcher('app\models\User');
         $userModel->hasMany('posts', 'app\models\Post', ['user_id' => 'id']);
     }
     
     public function testLoginLength()
     {
          $userModel = new ModelMatcher('app\models\User');
          $userModel->matchLength('login', 3, 20);
     }
}

可用方法

  • shouldBeSafe()shouldBeNotSafe() - 检查属性是否可以批量赋值
  • shouldBeRequired()shouldBeNotRequired() - 检查参数是否必须填写
  • matchLength() - 检查字符串长度。如果要只检查 minmax,请将第二个参数设为 null。
  • hasOne()hasMany() - 检查关联

所有方法都接受参数名和一个可选的参数 - 场景。

PS:欢迎提交包含额外 matcher 的 pull-request。或者您可以在 issue 中说明希望添加哪些验证器。