FixtureTrait
API reference › yii › test › FixtureTrait
Used by 1 type
FixtureTrait provides functionalities for loading, unloading and accessing fixtures for a test case.
By using FixtureTrait, a test class will be able to specify which fixtures to load by overriding
the fixtures() method. It can then load and unload the fixtures using loadFixtures() and unloadFixtures().
Once a fixture is loaded, it can be accessed like an object property, thanks to the PHP __get() magic method.
Also, if the fixture is an instance of ActiveFixture, you will be able to access AR models
through the syntax $this->fixtureName('model name').
For more details and usage information on FixtureTrait, see the guide article on fixtures.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
fixtures() | Declares the fixtures that are needed by the current test case. | FixtureTrait |
getFixture() | Returns the named fixture. | FixtureTrait |
getFixtures() | Returns the fixture objects as specified in globalFixtures() and fixtures(). | FixtureTrait |
globalFixtures() | Declares the fixtures shared required by different test cases. | FixtureTrait |
initFixtures() | Initialize the fixtures. | FixtureTrait |
loadFixtures() | Loads the specified fixtures. | FixtureTrait |
unloadFixtures() | Unloads the specified fixtures. | FixtureTrait |
createFixtures() protected | Creates the specified fixture instances. | FixtureTrait |
Method details
Section titled “Method details”fixtures()
Section titled “fixtures() ”public function fixtures(): arrayDeclares the fixtures that are needed by the current test case.
The return value of this method must be an array of fixture configurations. For example,
[ // anonymous fixture PostFixture::class, // "users" fixture 'users' => UserFixture::class, // "cache" fixture with configuration 'cache' => [ 'class' => CacheFixture::class, 'host' => 'xxx', ],]Note that the actual fixtures used for a test case will include both globalFixtures()
and fixtures().
Returns
array: The fixtures needed by the current test case
getFixture()
Section titled “getFixture() ”public function getFixture(string $name): yii\test\Fixture|nullReturns the named fixture.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The fixture name. This can be either the fixture alias name, or the class name if the alias is not used. |
Returns
Fixture|null: The fixture object, or null if the named fixture does not exist.
getFixtures()
Section titled “getFixtures() ”public function getFixtures(): yii\test\Fixture[]Returns the fixture objects as specified in globalFixtures() and fixtures().
Returns
Fixture[]: The loaded fixtures for the current test case
globalFixtures()
Section titled “globalFixtures() ”public function globalFixtures(): arrayDeclares the fixtures shared required by different test cases.
The return value should be similar to that of fixtures().
You should usually override this method in a base class.
initFixtures()
Section titled “initFixtures() ”public function initFixtures(): mixedInitialize the fixtures.
Returns
mixed
Since
2.0.12
loadFixtures()
Section titled “loadFixtures() ”public function loadFixtures(yii\test\Fixture[]|null $fixtures = null): mixedLoads the specified fixtures.
This method will call Fixture::load() for every fixture object.
Parameters
| Parameter | Type | Description |
|---|---|---|
$fixtures | Fixture[]| | The fixtures to be loaded. If this parameter is not specified, the return value of getFixtures() will be used. |
Returns
mixed
unloadFixtures()
Section titled “unloadFixtures() ”public function unloadFixtures(yii\test\Fixture[]|null $fixtures = null): mixedUnloads the specified fixtures.
This method will call Fixture::unload() for every fixture object.
Parameters
| Parameter | Type | Description |
|---|---|---|
$fixtures | Fixture[]| | The fixtures to be loaded. If this parameter is not specified, the return value of getFixtures() will be used. |
Returns
mixed
createFixtures()
Section titled “createFixtures() ”protected function createFixtures(array $fixtures): yii\test\Fixture[]Creates the specified fixture instances.
All dependent fixtures will also be created. Duplicate fixtures and circular dependencies will only be created once.
Parameters
| Parameter | Type | Description |
|---|---|---|
$fixtures | array | The fixtures to be created. You may provide fixture names or fixture configurations. If this parameter is not provided, the fixtures specified in globalFixtures() and fixtures() will be created. |
Returns
Fixture[]: The created fixture instances
Throws
yii\base\InvalidConfigException: if fixtures are not properly configured