Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用可指责行为时的Yii2测试_Php_Yii2 - Fatal编程技术网

Php 使用可指责行为时的Yii2测试

Php 使用可指责行为时的Yii2测试,php,yii2,Php,Yii2,我有一个模型使用了可指责的行为: class Vehicle extends ActiveRecord { // ... public function behaviors() { return [ 'blameable' => [ 'class' => BlameableBehavior::className(), 'createdByAttribute

我有一个模型使用了
可指责的
行为:

class Vehicle extends ActiveRecord 
{
    // ...
    public function behaviors()
    {
        return [
            'blameable' => [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'UserID',
                'updatedByAttribute' => null,
            ]
        ];
    }
    // ...
}
问题是,当我尝试保存
车辆的实例以使用特定用户ID进行测试时,
可指责的
将使用null覆盖该实例(因为没有用户被设置为当前登录的用户),并且模型的保存将失败

这个剪贴说明了我到目前为止是如何解决这个问题的:

$owner = $this->createUser(); // creates user with fake data
Yii::$app->user->setIdentity($owner);
$vehicle = $this->createVehicle(); // creates vehicle and relies that the $owner->UserID will be set when vehicle is being saved

但是我不喜欢它,因为不清楚为什么要提前设置用户身份。有没有办法在测试中禁用可责备的行为?

只需在
createVehicle()
方法中分离可责备的行为,如下所示:

public function createVehile()
{
    $vehicle = new Vehicle();
    $vehicle->detachBehavior('blameable');
    // ...
}

对于那些使用
'value'=>Yii::$app->user->identity->username
的人来说,分离行为无法解决问题,因为调用新模型仍然会抛出错误

相反,您可以使用_before方法为所有测试设置标识

单元测试


使用app\models\User;//您的用户类
类SupplierTest扩展\Codeception\Test\Unit
{
受保护函数_before()
{
//对于使用Yii@.
$owner=新用户();
$owner->username='tester';
$owner->auth_key='123456';
$owner->password_hash='123456';
$owner->email=something@some.com';
$owner->save();
Yii::$app->user->setIdentity($owner);
}
公共函数testCreateRecord(){
$rec=新供应商();
$rec->supplier=“supplier ABC”;
$rec->smr_number=“123456”;
$rec->validate();
Debug::Debug($rec->getErrors());
$this->assertTrue($rec->save());
}
各种模型行为

    public function behaviors()
    {
        return [
            [
                'class' => \yii\behaviors\BlameableBehavior::className(),
                'value' => Yii::$app->user->identity->username,
            ],
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'value' => new \yii\db\Expression('NOW()'),
            ],
            [
                'class' => 'sammaye\audittrail\LoggableBehavior',
                'userAttribute' => 'updated_by', //blameable attribute of the current model.
                'ignored' => ['updated_by', 'updated_at'], // This ignores fields from a selection of all fields, not needed with allowed
            ],
        ];
    }

在我的例子中,使用与@ddinchev相同的
可责备行为
我需要使用
detachBehaviors();
分离所有行为才能工作。也许“可责备”这个名称并不适用于所有情况。