3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
use yii\base\InvalidConfigException;
14
* FixtureTrait provides functionalities for loading, unloading and accessing fixtures for a test case.
16
* By using FixtureTrait, a test class will be able to specify which fixtures to load by overriding
17
* the [[fixtures()]] method. It can then load and unload the fixtures using [[loadFixtures()]] and [[unloadFixtures()]].
18
* Once a fixture is loaded, it can be accessed like an object property, thanks to the PHP `__get()` magic method.
19
* Also, if the fixture is an instance of [[ActiveFixture]], you will be able to access AR models
20
* through the syntax `$this->fixtureName('model name')`.
22
* For more details and usage information on FixtureTrait, see the [guide article on fixtures](guide:test-fixtures).
24
* @author Qiang Xue <qiang.xue@gmail.com>
30
* @var array the list of fixture objects available for the current test.
31
* The array keys are the corresponding fixture class names.
32
* The fixtures are listed in their dependency order. That is, fixture A is listed before B
39
* Declares the fixtures that are needed by the current test case.
41
* The return value of this method must be an array of fixture configurations. For example,
45
* // anonymous fixture
48
* 'users' => UserFixture::class,
49
* // "cache" fixture with configuration
51
* 'class' => CacheFixture::class,
57
* Note that the actual fixtures used for a test case will include both [[globalFixtures()]]
60
* @return array the fixtures needed by the current test case
62
public function fixtures()
68
* Declares the fixtures shared required by different test cases.
69
* The return value should be similar to that of [[fixtures()]].
70
* You should usually override this method in a base class.
71
* @return array the fixtures shared and required by different test cases.
74
public function globalFixtures()
80
* Loads the specified fixtures.
81
* This method will call [[Fixture::load()]] for every fixture object.
82
* @param Fixture[]|null $fixtures the fixtures to be loaded. If this parameter is not specified,
83
* the return value of [[getFixtures()]] will be used.
85
public function loadFixtures($fixtures = null)
87
if ($fixtures === null) {
88
$fixtures = $this->getFixtures();
91
/* @var $fixture Fixture */
92
foreach ($fixtures as $fixture) {
93
$fixture->beforeLoad();
95
foreach ($fixtures as $fixture) {
98
foreach (array_reverse($fixtures) as $fixture) {
99
$fixture->afterLoad();
104
* Unloads the specified fixtures.
105
* This method will call [[Fixture::unload()]] for every fixture object.
106
* @param Fixture[]|null $fixtures the fixtures to be loaded. If this parameter is not specified,
107
* the return value of [[getFixtures()]] will be used.
109
public function unloadFixtures($fixtures = null)
111
if ($fixtures === null) {
112
$fixtures = $this->getFixtures();
115
/* @var $fixture Fixture */
116
foreach ($fixtures as $fixture) {
117
$fixture->beforeUnload();
119
$fixtures = array_reverse($fixtures);
120
foreach ($fixtures as $fixture) {
123
foreach ($fixtures as $fixture) {
124
$fixture->afterUnload();
129
* Initialize the fixtures.
132
public function initFixtures()
134
$this->unloadFixtures();
135
$this->loadFixtures();
139
* Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
140
* @return Fixture[] the loaded fixtures for the current test case
142
public function getFixtures()
144
if ($this->_fixtures === null) {
145
$this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
148
return $this->_fixtures;
152
* Returns the named fixture.
153
* @param string $name the fixture name. This can be either the fixture alias name, or the class name if the alias is not used.
154
* @return Fixture|null the fixture object, or null if the named fixture does not exist.
156
public function getFixture($name)
158
if ($this->_fixtures === null) {
159
$this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
161
$name = ltrim($name, '\\');
163
return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
167
* Creates the specified fixture instances.
168
* All dependent fixtures will also be created. Duplicate fixtures and circular dependencies will only be created once.
169
* @param array $fixtures the fixtures to be created. You may provide fixture names or fixture configurations.
170
* If this parameter is not provided, the fixtures specified in [[globalFixtures()]] and [[fixtures()]] will be created.
171
* @return Fixture[] the created fixture instances
172
* @throws InvalidConfigException if fixtures are not properly configured
174
protected function createFixtures(array $fixtures)
176
// normalize fixture configurations
177
$config = []; // configuration provided in test case
178
$aliases = []; // class name => alias or class name
179
foreach ($fixtures as $name => $fixture) {
180
if (!is_array($fixture)) {
181
$class = ltrim($fixture, '\\');
182
$fixtures[$name] = ['class' => $class];
183
$aliases[$class] = is_int($name) ? $class : $name;
184
} elseif (isset($fixture['class'])) {
185
$class = ltrim($fixture['class'], '\\');
186
$config[$class] = $fixture;
187
$aliases[$class] = $name;
189
throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
193
// create fixture instances
195
$stack = array_reverse($fixtures);
196
while (($fixture = array_pop($stack)) !== null) {
197
if ($fixture instanceof Fixture) {
198
$class = get_class($fixture);
199
$name = isset($aliases[$class]) ? $aliases[$class] : $class;
200
unset($instances[$name]); // unset so that the fixture is added to the last in the next line
201
$instances[$name] = $fixture;
203
$class = ltrim($fixture['class'], '\\');
204
$name = isset($aliases[$class]) ? $aliases[$class] : $class;
205
if (!isset($instances[$name])) {
206
$instances[$name] = false;
207
$stack[] = $fixture = Yii::createObject($fixture);
208
foreach ($fixture->depends as $dep) {
209
// need to use the configuration provided in test case
210
$stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
213
// if the fixture is already loaded (ie. a circular dependency or if two fixtures depend on the same fixture) just skip it.