Codeception: Yii2 behaviors aren't executed for haveRecord() method

Created on 18 Jan 2018  路  9Comments  路  Source: Codeception/Codeception

What are you trying to achieve?

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'],
        ]
    ];
}

What do you get instead?

AR behaviors are not executed

Details

  • Codeception version:
  • PHP Version: v2.3.6
  • Operating System: Windows 10
  • Installation type: Composer

Most helpful comment

Right. Fix merged.

All 9 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

m4rcinkowski picture m4rcinkowski  路  4Comments

rogoit picture rogoit  路  3Comments

sasha-x picture sasha-x  路  3Comments

marcelodeandrade picture marcelodeandrade  路  3Comments

sebastianneubert picture sebastianneubert  路  3Comments