FixtureTrait
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.
Tags
Table of Contents
Properties
- $_fixtures : array<string|int, mixed>
Methods
- fixtures() : array<string|int, mixed>
- Declares the fixtures that are needed by the current test case.
- getFixture() : Fixture|null
- Returns the named fixture.
- getFixtures() : array<string|int, Fixture>
- Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
- globalFixtures() : array<string|int, mixed>
- Declares the fixtures shared required by different test cases.
- initFixtures() : mixed
- Initialize the fixtures.
- loadFixtures() : mixed
- Loads the specified fixtures.
- unloadFixtures() : mixed
- Unloads the specified fixtures.
- createFixtures() : array<string|int, Fixture>
- Creates the specified fixture instances.
Properties
$_fixtures
private
array<string|int, mixed>
$_fixtures
the list of fixture objects available for the current test. The array keys are the corresponding fixture class names. The fixtures are listed in their dependency order. That is, fixture A is listed before B if B depends on A.
Methods
fixtures()
Declares the fixtures that are needed by the current test case.
public
fixtures() : array<string|int, mixed>
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()]].
Return values
array<string|int, mixed> —the fixtures needed by the current test case
getFixture()
Returns the named fixture.
public
getFixture(string $name) : Fixture|null
Parameters
- $name : string
-
the fixture name. This can be either the fixture alias name, or the class name if the alias is not used.
Return values
Fixture|null —the fixture object, or null if the named fixture does not exist.
getFixtures()
Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
public
getFixtures() : array<string|int, Fixture>
Return values
array<string|int, Fixture> —the loaded fixtures for the current test case
globalFixtures()
Declares the fixtures shared required by different test cases.
public
globalFixtures() : array<string|int, mixed>
The return value should be similar to that of [[fixtures()]]. You should usually override this method in a base class.
Tags
Return values
array<string|int, mixed> —the fixtures shared and required by different test cases.
initFixtures()
Initialize the fixtures.
public
initFixtures() : mixed
Tags
loadFixtures()
Loads the specified fixtures.
public
loadFixtures([array<string|int, Fixture>|null $fixtures = null ]) : mixed
This method will call [[Fixture::load()]] for every fixture object.
Parameters
- $fixtures : array<string|int, Fixture>|null = null
-
the fixtures to be loaded. If this parameter is not specified, the return value of [[getFixtures()]] will be used.
unloadFixtures()
Unloads the specified fixtures.
public
unloadFixtures([array<string|int, Fixture>|null $fixtures = null ]) : mixed
This method will call [[Fixture::unload()]] for every fixture object.
Parameters
- $fixtures : array<string|int, Fixture>|null = null
-
the fixtures to be loaded. If this parameter is not specified, the return value of [[getFixtures()]] will be used.
createFixtures()
Creates the specified fixture instances.
protected
createFixtures(array<string|int, mixed> $fixtures) : array<string|int, Fixture>
All dependent fixtures will also be created. Duplicate fixtures and circular dependencies will only be created once.
Parameters
- $fixtures : array<string|int, mixed>
-
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.
Tags
Return values
array<string|int, Fixture> —the created fixture instances