1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\test; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* FixtureTrait provides functionalities for loading, unloading and accessing fixtures for a test case. |
15
|
|
|
* |
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')`. |
21
|
|
|
* |
22
|
|
|
* For more details and usage information on FixtureTrait, see the [guide article on fixtures](guide:test-fixtures). |
23
|
|
|
* |
24
|
|
|
* @author Qiang Xue <[email protected]> |
25
|
|
|
* @since 2.0 |
26
|
|
|
*/ |
27
|
|
|
trait FixtureTrait |
28
|
|
|
{ |
29
|
|
|
/** |
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 |
33
|
|
|
* if B depends on A. |
34
|
|
|
*/ |
35
|
|
|
private $_fixtures; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Declares the fixtures that are needed by the current test case. |
40
|
|
|
* |
41
|
|
|
* The return value of this method must be an array of fixture configurations. For example, |
42
|
|
|
* |
43
|
|
|
* ```php |
44
|
|
|
* [ |
45
|
|
|
* // anonymous fixture |
46
|
|
|
* PostFixture::class, |
47
|
|
|
* // "users" fixture |
48
|
|
|
* 'users' => UserFixture::class, |
49
|
|
|
* // "cache" fixture with configuration |
50
|
|
|
* 'cache' => [ |
51
|
|
|
* '__class' => CacheFixture::class, |
52
|
|
|
* 'host' => 'xxx', |
53
|
|
|
* ], |
54
|
|
|
* ] |
55
|
|
|
* ``` |
56
|
|
|
* |
57
|
|
|
* Note that the actual fixtures used for a test case will include both [[globalFixtures()]] |
58
|
|
|
* and [[fixtures()]]. |
59
|
|
|
* |
60
|
|
|
* @return array the fixtures needed by the current test case |
61
|
|
|
*/ |
62
|
|
|
public function fixtures() |
63
|
|
|
{ |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
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. |
72
|
|
|
* @see fixtures() |
73
|
|
|
*/ |
74
|
22 |
|
public function globalFixtures() |
75
|
|
|
{ |
76
|
22 |
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Loads the specified fixtures. |
81
|
|
|
* This method will call [[Fixture::load()]] for every fixture object. |
82
|
|
|
* @param Fixture[] $fixtures the fixtures to be loaded. If this parameter is not specified, |
83
|
|
|
* the return value of [[getFixtures()]] will be used. |
84
|
|
|
*/ |
85
|
28 |
|
public function loadFixtures($fixtures = null) |
86
|
|
|
{ |
87
|
28 |
|
if ($fixtures === null) { |
88
|
22 |
|
$fixtures = $this->getFixtures(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* @var $fixture Fixture */ |
92
|
28 |
|
foreach ($fixtures as $fixture) { |
93
|
28 |
|
$fixture->beforeLoad(); |
94
|
|
|
} |
95
|
28 |
|
foreach ($fixtures as $fixture) { |
96
|
28 |
|
$fixture->load(); |
97
|
|
|
} |
98
|
28 |
|
foreach (array_reverse($fixtures) as $fixture) { |
99
|
28 |
|
$fixture->afterLoad(); |
100
|
|
|
} |
101
|
28 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Unloads the specified fixtures. |
105
|
|
|
* This method will call [[Fixture::unload()]] for every fixture object. |
106
|
|
|
* @param Fixture[] $fixtures the fixtures to be loaded. If this parameter is not specified, |
107
|
|
|
* the return value of [[getFixtures()]] will be used. |
108
|
|
|
*/ |
109
|
32 |
|
public function unloadFixtures($fixtures = null) |
110
|
|
|
{ |
111
|
32 |
|
if ($fixtures === null) { |
112
|
21 |
|
$fixtures = $this->getFixtures(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* @var $fixture Fixture */ |
116
|
32 |
|
foreach ($fixtures as $fixture) { |
117
|
32 |
|
$fixture->beforeUnload(); |
118
|
|
|
} |
119
|
32 |
|
$fixtures = array_reverse($fixtures); |
120
|
32 |
|
foreach ($fixtures as $fixture) { |
121
|
32 |
|
$fixture->unload(); |
122
|
|
|
} |
123
|
32 |
|
foreach ($fixtures as $fixture) { |
124
|
32 |
|
$fixture->afterUnload(); |
125
|
|
|
} |
126
|
32 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Initialize the fixtures. |
130
|
|
|
* @since 2.0.12 |
131
|
|
|
*/ |
132
|
20 |
|
public function initFixtures() |
133
|
|
|
{ |
134
|
20 |
|
$this->unloadFixtures(); |
135
|
20 |
|
$this->loadFixtures(); |
136
|
20 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]]. |
140
|
|
|
* @return Fixture[] the loaded fixtures for the current test case |
141
|
|
|
*/ |
142
|
22 |
|
public function getFixtures() |
143
|
|
|
{ |
144
|
22 |
|
if ($this->_fixtures === null) { |
145
|
22 |
|
$this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures())); |
146
|
|
|
} |
147
|
|
|
|
148
|
22 |
|
return $this->_fixtures; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
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 the fixture object, or null if the named fixture does not exist. |
155
|
|
|
*/ |
156
|
21 |
|
public function getFixture($name) |
157
|
|
|
{ |
158
|
21 |
|
if ($this->_fixtures === null) { |
159
|
|
|
$this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures())); |
160
|
|
|
} |
161
|
21 |
|
$name = ltrim($name, '\\'); |
162
|
|
|
|
163
|
21 |
|
return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Creates the specified fixture instances. |
168
|
|
|
* All dependent fixtures will also be created. |
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 or if a circular dependency among |
173
|
|
|
* the fixtures is detected. |
174
|
|
|
*/ |
175
|
33 |
|
protected function createFixtures(array $fixtures) |
176
|
|
|
{ |
177
|
|
|
// normalize fixture configurations |
178
|
33 |
|
$config = []; // configuration provided in test case |
179
|
33 |
|
$aliases = []; // class name => alias or class name |
180
|
33 |
|
foreach ($fixtures as $name => $fixture) { |
181
|
33 |
|
if (!is_array($fixture)) { |
182
|
25 |
|
$class = ltrim($fixture, '\\'); |
183
|
25 |
|
$fixtures[$name] = ['__class' => $class]; |
184
|
25 |
|
$aliases[$class] = is_int($name) ? $class : $name; |
185
|
8 |
|
} elseif (isset($fixture['__class'])) { |
186
|
8 |
|
$class = ltrim($fixture['__class'], '\\'); |
187
|
8 |
|
$config[$class] = $fixture; |
188
|
8 |
|
$aliases[$class] = $name; |
189
|
|
|
} else { |
190
|
33 |
|
throw new InvalidConfigException("You must specify '__class' for the fixture '$name'."); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// create fixture instances |
195
|
33 |
|
$instances = []; |
196
|
33 |
|
$stack = array_reverse($fixtures); |
197
|
33 |
|
while (($fixture = array_pop($stack)) !== null) { |
198
|
33 |
|
if ($fixture instanceof Fixture) { |
199
|
33 |
|
$class = get_class($fixture); |
200
|
33 |
|
$name = $aliases[$class] ?? $class; |
201
|
33 |
|
unset($instances[$name]); // unset so that the fixture is added to the last in the next line |
202
|
33 |
|
$instances[$name] = $fixture; |
203
|
|
|
} else { |
204
|
33 |
|
$class = ltrim($fixture['__class'], '\\'); |
205
|
33 |
|
$name = $aliases[$class] ?? $class; |
206
|
33 |
|
if (!isset($instances[$name])) { |
207
|
33 |
|
$instances[$name] = false; |
208
|
33 |
|
$stack[] = $fixture = Yii::createObject($fixture); |
209
|
33 |
|
foreach ($fixture->depends as $dep) { |
210
|
|
|
// need to use the configuration provided in test case |
211
|
33 |
|
$stack[] = $config[$dep] ?? ['__class' => $dep]; |
212
|
|
|
} |
213
|
2 |
|
} elseif ($instances[$name] === false) { |
214
|
|
|
throw new InvalidConfigException("A circular dependency is detected for fixture '$class'."); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
33 |
|
return $instances; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|