Google News
logo
Yii framework - Interview Questions
What about Fixtures in Yii Framework?
Fixtures are an important part of testing. Their main purpose is to set up the environment in a fixed/known state so that your tests are repeatable and run in an expected way. Yii provides a fixture framework that allows you to define your fixtures precisely and use them easily both when running your tests with Codeception and independently.
 
A key concept in the Yii fixture framework is the so-called fixture object. A fixture object represents a particular aspect of a test environment and is an instance of yii\test\Fixture or its child class. For example, you may use UserFixture to make sure the user DB table contains a fixed set of data. You load one or multiple fixture objects before running a test and unload them when finishing.
 
A fixture may depend on other fixtures, specified via its yii\test\Fixture::$depends property. When a fixture is being loaded, the fixtures it depends on will be automatically loaded BEFORE the fixture; and when the fixture is being unloaded, the dependent fixtures will be unloaded AFTER the fixture.
 
Defining a Fixture : To define a fixture, create a new class by extending yii\test\Fixture or yii\test\ActiveFixture. The former is best suited for general purpose fixtures, while the latter has enhanced features specifically designed to work with database and ActiveRecord.
 
The following code defines a fixture about the User ActiveRecord and the corresponding user table.
<?php
namespace app\tests\fixtures;

use yii\test\ActiveFixture;

class UserFixture extends ActiveFixture
{
    public $modelClass = 'app\models\User';
}
Advertisement