Skip to content
PHP Ecosystem
Explore

FixtureTrait

API reference › yii › test › FixtureTrait

traityii\test\FixtureTrait
Since2.0
Sourceframework/test/FixtureTrait.php
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.

MethodDescriptionDefined 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() protectedCreates the specified fixture instances.FixtureTrait
public function fixtures(): array

Declares 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

public function getFixture(string $name): yii\test\Fixture|null

Returns the named fixture.

Parameters

ParameterTypeDescription
$namestringThe 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.

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

public function globalFixtures(): array

Declares 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.

Returns

array: The fixtures shared and required by different test cases.

See also

public function initFixtures(): mixed

Initialize the fixtures.

Returns

mixed

Since

2.0.12

public function loadFixtures(yii\test\Fixture[]|null $fixtures = null): mixed

Loads the specified fixtures.

This method will call Fixture::load() for every fixture object.

Parameters

ParameterTypeDescription
$fixturesFixture[]|nullThe fixtures to be loaded. If this parameter is not specified, the return value of getFixtures() will be used.

Returns

mixed

public function unloadFixtures(yii\test\Fixture[]|null $fixtures = null): mixed

Unloads the specified fixtures.

This method will call Fixture::unload() for every fixture object.

Parameters

ParameterTypeDescription
$fixturesFixture[]|nullThe fixtures to be loaded. If this parameter is not specified, the return value of getFixtures() will be used.

Returns

mixed

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

ParameterTypeDescription
$fixturesarrayThe 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