I'm trying to create a test AR:
$I->haveRecord(EmailContact::class, [
'address' => '[email protected]',
]);
The thing is that EmailContact AR has behavior for address attribute which converts it before save (encrypts) and after find (decrypts). So I expect that haveRecord() method will run behaviors as simple save() does.
/**
* Returns a list of behaviors that this component should behave as
* @return array
*/
public function behaviors()
{
return [
[
'class' => \common\behaviors\AesBehavior::class,
'key' => \yii::$app->params['aes.key'],
'attributes' => ['address'],
]
];
}
AR behaviors are not executed
This line is the culprit
$record = new $model;
https://github.com/Codeception/Codeception/blob/be8a6a5c642f267650f56a00931640e46f880e07/src/Codeception/Module/Yii2.php#L484-L494
https://github.com/Codeception/Codeception/blob/be8a6a5c642f267650f56a00931640e46f880e07/src/Codeception/Module/Yii2.php#L410-L412
solution is
$record = Yii::createObject($model);
I'd not expect so. We're not saving anything so I don't think any behaviors should run.
$record->save(false); also can't run validation
Why do we need validation when getting a mock-record?
I'm just wondering why it's a mock-record? This method allows to save a real DB-record (using transaction) which will be removed after test run
I mean we know what we're doing here so extra validation doesn't make much sense.
But it's not about validation, it's about behaviors that perform preprocessing of attributes' values
Right. Fix merged.
Why do we need validation when getting a mock-record?
If a model has default rules it can cause problems.
Most helpful comment
Right. Fix merged.