Codeception / module-yii2

Codeception module for Yii2 framework
MIT License
16 stars 36 forks source link

InitDbFixture object cannot be overridden during FixturesStore initialization #88

Closed PoohOka closed 1 year ago

PoohOka commented 1 year ago

Issue

There is an issue configuring component properties when the user does not use Yii2 default values during Codeception tests. The problem caused by using anonymous array value to register InitDbFixture [yii\test\InitDbFixture] component rather than using key-pair component registering.

The following attachment occurred from changing Yii default database component from db to something else.

Screenshot 2023-06-15 at 16 58 15

Solution

During FixturesStore [Codeception\Lib\Connector\Yii2\FixturesStore] initialization, its globalFixtures() has InitDbFixture component registering as anonymous fixture value resulting to Yii unable to merge the fixtures array in FixtureTrait [yii\test\FixtureTrait::getFixtures()].

By changing its registration from the following

return [
    InitDbFixture::class
];

to

return [
    'initDbFixture' => [
        'class' => InitDbFixture::class,
    ],
];

This allows the user to override component InitDbFixture default values via _fixtures() in test case.

Result

Example of application using otherDbComponent to be main database component than Yii default db.

// in a Cest file
public function _fixtures()
{
    return [
        // This will be merged and overridden during initialization
        'initDbFixture' => [
            'class' => \yii\test\InitDbFixture::class,
            'db' => 'otherDbComponent',
        ],
        'user' => [
            'class' => UserFixture::class,
            'db' => 'otherDbComponent',
            'dataFile' => codecept_data_dir() . 'login_data.php'
        ]
    ];
}
samdark commented 1 year ago

Thanks!