|
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\console\controllers; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\InvalidConfigException; |
|
12
|
|
|
use yii\base\InvalidArgumentException; |
|
13
|
|
|
use yii\console\Controller; |
|
14
|
|
|
use yii\console\Exception; |
|
15
|
|
|
use yii\console\ExitCode; |
|
16
|
|
|
use yii\helpers\Console; |
|
17
|
|
|
use yii\helpers\FileHelper; |
|
18
|
|
|
use yii\test\FixtureTrait; |
|
19
|
|
|
use yii\test\InitDbFixture; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Manages fixture data loading and unloading. |
|
23
|
|
|
* |
|
24
|
|
|
* ``` |
|
25
|
|
|
* #load fixtures from UsersFixture class with default namespace "tests\unit\fixtures" |
|
26
|
|
|
* yii fixture/load User |
|
27
|
|
|
* |
|
28
|
|
|
* #also a short version of this command (generate action is default) |
|
29
|
|
|
* yii fixture User |
|
30
|
|
|
* |
|
31
|
|
|
* #load all fixtures |
|
32
|
|
|
* yii fixture "*" |
|
33
|
|
|
* |
|
34
|
|
|
* #load all fixtures except User |
|
35
|
|
|
* yii fixture "*, -User" |
|
36
|
|
|
* |
|
37
|
|
|
* #load fixtures with different namespace. |
|
38
|
|
|
* yii fixture/load User --namespace=alias\my\custom\namespace\goes\here |
|
39
|
|
|
* ``` |
|
40
|
|
|
* |
|
41
|
|
|
* The `unload` sub-command can be used similarly to unload fixtures. |
|
42
|
|
|
* |
|
43
|
|
|
* @author Mark Jebri <[email protected]> |
|
44
|
|
|
* @since 2.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
class FixtureController extends Controller |
|
47
|
|
|
{ |
|
48
|
|
|
use FixtureTrait; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string controller default action ID. |
|
52
|
|
|
*/ |
|
53
|
|
|
public $defaultAction = 'load'; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var string default namespace to search fixtures in |
|
56
|
|
|
*/ |
|
57
|
|
|
public $namespace = 'tests\unit\fixtures'; |
|
58
|
|
|
/** |
|
59
|
|
|
* @var array global fixtures that should be applied when loading and unloading. By default it is set to `InitDbFixture` |
|
60
|
|
|
* that disables and enables integrity check, so your data can be safely loaded. |
|
61
|
|
|
*/ |
|
62
|
|
|
public $globalFixtures = [ |
|
63
|
|
|
InitDbFixture::class, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function options($actionID) |
|
71
|
|
|
{ |
|
72
|
|
|
return array_merge(parent::options($actionID), [ |
|
73
|
|
|
'namespace', 'globalFixtures', |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
* @since 2.0.8 |
|
80
|
|
|
*/ |
|
81
|
|
|
public function optionAliases() |
|
82
|
|
|
{ |
|
83
|
|
|
return array_merge(parent::optionAliases(), [ |
|
84
|
|
|
'g' => 'globalFixtures', |
|
85
|
|
|
'n' => 'namespace', |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Loads the specified fixture data. |
|
91
|
|
|
* |
|
92
|
|
|
* For example, |
|
93
|
|
|
* |
|
94
|
|
|
* ``` |
|
95
|
|
|
* # load the fixture data specified by User and UserProfile. |
|
96
|
|
|
* # any existing fixture data will be removed first |
|
97
|
|
|
* yii fixture/load "User, UserProfile" |
|
98
|
|
|
* |
|
99
|
|
|
* # load all available fixtures found under 'tests\unit\fixtures' |
|
100
|
|
|
* yii fixture/load "*" |
|
101
|
|
|
* |
|
102
|
|
|
* # load all fixtures except User and UserProfile |
|
103
|
|
|
* yii fixture/load "*, -User, -UserProfile" |
|
104
|
|
|
* ``` |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $fixturesInput |
|
107
|
|
|
* @return int return code |
|
108
|
|
|
* @throws Exception if the specified fixture does not exist. |
|
109
|
|
|
*/ |
|
110
|
7 |
|
public function actionLoad(array $fixturesInput = []) |
|
111
|
|
|
{ |
|
112
|
7 |
|
if ($fixturesInput === []) { |
|
113
|
|
|
$this->stdout($this->getHelpSummary() . "\n"); |
|
114
|
|
|
|
|
115
|
|
|
$helpCommand = Console::ansiFormat('yii help fixture', [Console::FG_CYAN]); |
|
116
|
|
|
$this->stdout("Use $helpCommand to get usage info.\n"); |
|
117
|
|
|
|
|
118
|
|
|
return ExitCode::OK; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
7 |
|
$filtered = $this->filterFixtures($fixturesInput); |
|
122
|
7 |
|
$except = $filtered['except']; |
|
123
|
|
|
|
|
124
|
7 |
|
if (!$this->needToApplyAll($fixturesInput[0])) { |
|
125
|
5 |
|
$fixtures = $filtered['apply']; |
|
126
|
|
|
|
|
127
|
5 |
|
$foundFixtures = $this->findFixtures($fixtures); |
|
128
|
5 |
|
$notFoundFixtures = array_diff($fixtures, $foundFixtures); |
|
129
|
|
|
|
|
130
|
5 |
|
if ($notFoundFixtures) { |
|
|
|
|
|
|
131
|
5 |
|
$this->notifyNotFound($notFoundFixtures); |
|
132
|
|
|
} |
|
133
|
|
|
} else { |
|
134
|
2 |
|
$foundFixtures = $this->findFixtures(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
7 |
|
$fixturesToLoad = array_diff($foundFixtures, $except); |
|
138
|
|
|
|
|
139
|
7 |
|
if (!$foundFixtures) { |
|
|
|
|
|
|
140
|
1 |
|
throw new Exception( |
|
141
|
1 |
|
'No files were found for: "' . implode(', ', $fixturesInput) . "\".\n" . |
|
142
|
1 |
|
"Check that files exist under fixtures path: \n\"" . $this->getFixturePath() . '".' |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
6 |
|
if (!$fixturesToLoad) { |
|
|
|
|
|
|
147
|
|
|
$this->notifyNothingToLoad($foundFixtures, $except); |
|
148
|
|
|
return ExitCode::OK; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
6 |
|
if (!$this->confirmLoad($fixturesToLoad, $except)) { |
|
|
|
|
|
|
152
|
|
|
return ExitCode::OK; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
6 |
|
$fixtures = $this->getFixturesConfig(array_merge($this->globalFixtures, $fixturesToLoad)); |
|
156
|
|
|
|
|
157
|
6 |
|
if (!$fixtures) { |
|
|
|
|
|
|
158
|
|
|
throw new Exception('No fixtures were found in namespace: "' . $this->namespace . '"' . ''); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
6 |
|
$fixturesObjects = $this->createFixtures($fixtures); |
|
162
|
|
|
|
|
163
|
6 |
|
$this->unloadFixtures($fixturesObjects); |
|
164
|
6 |
|
$this->loadFixtures($fixturesObjects); |
|
165
|
6 |
|
$this->notifyLoaded($fixtures); |
|
166
|
|
|
|
|
167
|
6 |
|
return ExitCode::OK; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Unloads the specified fixtures. |
|
172
|
|
|
* |
|
173
|
|
|
* For example, |
|
174
|
|
|
* |
|
175
|
|
|
* ``` |
|
176
|
|
|
* # unload the fixture data specified by User and UserProfile. |
|
177
|
|
|
* yii fixture/unload "User, UserProfile" |
|
178
|
|
|
* |
|
179
|
|
|
* # unload all fixtures found under 'tests\unit\fixtures' |
|
180
|
|
|
* yii fixture/unload "*" |
|
181
|
|
|
* |
|
182
|
|
|
* # unload all fixtures except User and UserProfile |
|
183
|
|
|
* yii fixture/unload "*, -User, -UserProfile" |
|
184
|
|
|
* ``` |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $fixturesInput |
|
187
|
|
|
* @return int return code |
|
188
|
|
|
* @throws Exception if the specified fixture does not exist. |
|
189
|
|
|
*/ |
|
190
|
6 |
|
public function actionUnload(array $fixturesInput = []) |
|
191
|
|
|
{ |
|
192
|
6 |
|
$filtered = $this->filterFixtures($fixturesInput); |
|
193
|
6 |
|
$except = $filtered['except']; |
|
194
|
|
|
|
|
195
|
6 |
|
if (!$this->needToApplyAll($fixturesInput[0])) { |
|
196
|
4 |
|
$fixtures = $filtered['apply']; |
|
197
|
|
|
|
|
198
|
4 |
|
$foundFixtures = $this->findFixtures($fixtures); |
|
199
|
4 |
|
$notFoundFixtures = array_diff($fixtures, $foundFixtures); |
|
200
|
|
|
|
|
201
|
4 |
|
if ($notFoundFixtures) { |
|
|
|
|
|
|
202
|
4 |
|
$this->notifyNotFound($notFoundFixtures); |
|
203
|
|
|
} |
|
204
|
|
|
} else { |
|
205
|
2 |
|
$foundFixtures = $this->findFixtures(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
6 |
|
$fixturesToUnload = array_diff($foundFixtures, $except); |
|
209
|
|
|
|
|
210
|
6 |
|
if (!$foundFixtures) { |
|
|
|
|
|
|
211
|
1 |
|
throw new Exception( |
|
212
|
1 |
|
'No files were found for: "' . implode(', ', $fixturesInput) . "\".\n" . |
|
213
|
1 |
|
"Check that files exist under fixtures path: \n\"" . $this->getFixturePath() . '".' |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
5 |
|
if (!$fixturesToUnload) { |
|
|
|
|
|
|
218
|
|
|
$this->notifyNothingToUnload($foundFixtures, $except); |
|
219
|
|
|
return ExitCode::OK; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
5 |
|
if (!$this->confirmUnload($fixturesToUnload, $except)) { |
|
|
|
|
|
|
223
|
|
|
return ExitCode::OK; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
5 |
|
$fixtures = $this->getFixturesConfig(array_merge($this->globalFixtures, $fixturesToUnload)); |
|
227
|
|
|
|
|
228
|
5 |
|
if (!$fixtures) { |
|
|
|
|
|
|
229
|
|
|
throw new Exception('No fixtures were found in namespace: ' . $this->namespace . '".'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
5 |
|
$this->unloadFixtures($this->createFixtures($fixtures)); |
|
233
|
5 |
|
$this->notifyUnloaded($fixtures); |
|
234
|
5 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Notifies user that fixtures were successfully loaded. |
|
238
|
|
|
* @param array $fixtures |
|
239
|
|
|
*/ |
|
240
|
6 |
|
private function notifyLoaded($fixtures) |
|
241
|
|
|
{ |
|
242
|
6 |
|
$this->stdout("Fixtures were successfully loaded from namespace:\n", Console::FG_YELLOW); |
|
243
|
6 |
|
$this->stdout("\t\"" . Yii::getAlias($this->namespace) . "\"\n\n", Console::FG_GREEN); |
|
244
|
6 |
|
$this->outputList($fixtures); |
|
245
|
6 |
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Notifies user that there are no fixtures to load according input conditions. |
|
249
|
|
|
* @param array $foundFixtures array of found fixtures |
|
250
|
|
|
* @param array $except array of names of fixtures that should not be loaded |
|
251
|
|
|
*/ |
|
252
|
|
|
public function notifyNothingToLoad($foundFixtures, $except) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->stdout("Fixtures to load could not be found according given conditions:\n\n", Console::FG_RED); |
|
255
|
|
|
$this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW); |
|
256
|
|
|
$this->stdout("\t" . $this->namespace . "\n", Console::FG_GREEN); |
|
257
|
|
|
|
|
258
|
|
|
if (count($foundFixtures)) { |
|
259
|
|
|
$this->stdout("\nFixtures founded under the namespace:\n\n", Console::FG_YELLOW); |
|
260
|
|
|
$this->outputList($foundFixtures); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if (count($except)) { |
|
264
|
|
|
$this->stdout("\nFixtures that will NOT be loaded: \n\n", Console::FG_YELLOW); |
|
265
|
|
|
$this->outputList($except); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Notifies user that there are no fixtures to unload according input conditions. |
|
271
|
|
|
* @param array $foundFixtures array of found fixtures |
|
272
|
|
|
* @param array $except array of names of fixtures that should not be loaded |
|
273
|
|
|
*/ |
|
274
|
|
|
public function notifyNothingToUnload($foundFixtures, $except) |
|
275
|
|
|
{ |
|
276
|
|
|
$this->stdout("Fixtures to unload could not be found according to given conditions:\n\n", Console::FG_RED); |
|
277
|
|
|
$this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW); |
|
278
|
|
|
$this->stdout("\t" . $this->namespace . "\n", Console::FG_GREEN); |
|
279
|
|
|
|
|
280
|
|
|
if (count($foundFixtures)) { |
|
281
|
|
|
$this->stdout("\nFixtures found under the namespace:\n\n", Console::FG_YELLOW); |
|
282
|
|
|
$this->outputList($foundFixtures); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
if (count($except)) { |
|
286
|
|
|
$this->stdout("\nFixtures that will NOT be unloaded: \n\n", Console::FG_YELLOW); |
|
287
|
|
|
$this->outputList($except); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Notifies user that fixtures were successfully unloaded. |
|
293
|
|
|
* @param array $fixtures |
|
294
|
|
|
*/ |
|
295
|
5 |
|
private function notifyUnloaded($fixtures) |
|
296
|
|
|
{ |
|
297
|
5 |
|
$this->stdout("\nFixtures were successfully unloaded from namespace: ", Console::FG_YELLOW); |
|
298
|
5 |
|
$this->stdout(Yii::getAlias($this->namespace) . "\"\n\n", Console::FG_GREEN); |
|
299
|
5 |
|
$this->outputList($fixtures); |
|
300
|
5 |
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Notifies user that fixtures were not found under fixtures path. |
|
304
|
|
|
* @param array $fixtures |
|
305
|
|
|
*/ |
|
306
|
2 |
|
private function notifyNotFound($fixtures) |
|
307
|
|
|
{ |
|
308
|
2 |
|
$this->stdout("Some fixtures were not found under path:\n", Console::BG_RED); |
|
309
|
2 |
|
$this->stdout("\t" . $this->getFixturePath() . "\n\n", Console::FG_GREEN); |
|
310
|
2 |
|
$this->stdout("Check that they have correct namespace \"{$this->namespace}\" \n", Console::BG_RED); |
|
311
|
2 |
|
$this->outputList($fixtures); |
|
312
|
2 |
|
$this->stdout("\n"); |
|
313
|
2 |
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Prompts user with confirmation if fixtures should be loaded. |
|
317
|
|
|
* @param array $fixtures |
|
318
|
|
|
* @param array $except |
|
319
|
|
|
* @return bool |
|
320
|
|
|
*/ |
|
321
|
6 |
|
private function confirmLoad($fixtures, $except) |
|
322
|
|
|
{ |
|
323
|
6 |
|
$this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW); |
|
324
|
6 |
|
$this->stdout("\t" . $this->namespace . "\n\n", Console::FG_GREEN); |
|
325
|
|
|
|
|
326
|
6 |
|
if (count($this->globalFixtures)) { |
|
327
|
2 |
|
$this->stdout("Global fixtures will be used:\n\n", Console::FG_YELLOW); |
|
328
|
2 |
|
$this->outputList($this->globalFixtures); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
6 |
|
if (count($fixtures)) { |
|
332
|
6 |
|
$this->stdout("\nFixtures below will be loaded:\n\n", Console::FG_YELLOW); |
|
333
|
6 |
|
$this->outputList($fixtures); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
6 |
|
if (count($except)) { |
|
337
|
3 |
|
$this->stdout("\nFixtures that will NOT be loaded: \n\n", Console::FG_YELLOW); |
|
338
|
3 |
|
$this->outputList($except); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
6 |
|
$this->stdout("\nBe aware that:\n", Console::BOLD); |
|
342
|
6 |
|
$this->stdout("Applying leads to purging of certain data in the database!\n", Console::FG_RED); |
|
343
|
|
|
|
|
344
|
6 |
|
return $this->confirm("\nLoad above fixtures?"); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Prompts user with confirmation for fixtures that should be unloaded. |
|
349
|
|
|
* @param array $fixtures |
|
350
|
|
|
* @param array $except |
|
351
|
|
|
* @return bool |
|
352
|
|
|
*/ |
|
353
|
5 |
|
private function confirmUnload($fixtures, $except) |
|
354
|
|
|
{ |
|
355
|
5 |
|
$this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW); |
|
356
|
5 |
|
$this->stdout("\t" . $this->namespace . "\n\n", Console::FG_GREEN); |
|
357
|
|
|
|
|
358
|
5 |
|
if (count($this->globalFixtures)) { |
|
359
|
1 |
|
$this->stdout("Global fixtures will be used:\n\n", Console::FG_YELLOW); |
|
360
|
1 |
|
$this->outputList($this->globalFixtures); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
5 |
|
if (count($fixtures)) { |
|
364
|
5 |
|
$this->stdout("\nFixtures below will be unloaded:\n\n", Console::FG_YELLOW); |
|
365
|
5 |
|
$this->outputList($fixtures); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
5 |
|
if (count($except)) { |
|
369
|
3 |
|
$this->stdout("\nFixtures that will NOT be unloaded:\n\n", Console::FG_YELLOW); |
|
370
|
3 |
|
$this->outputList($except); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
5 |
|
return $this->confirm("\nUnload fixtures?"); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* Outputs data to the console as a list. |
|
378
|
|
|
* @param array $data |
|
379
|
|
|
*/ |
|
380
|
13 |
|
private function outputList($data) |
|
381
|
|
|
{ |
|
382
|
13 |
|
foreach ($data as $index => $item) { |
|
383
|
13 |
|
$this->stdout("\t" . ($index + 1) . ". {$item}\n", Console::FG_GREEN); |
|
384
|
|
|
} |
|
385
|
13 |
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Checks if needed to apply all fixtures. |
|
389
|
|
|
* @param string $fixture |
|
390
|
|
|
* @return bool |
|
391
|
|
|
*/ |
|
392
|
13 |
|
public function needToApplyAll($fixture) |
|
393
|
|
|
{ |
|
394
|
13 |
|
return $fixture === '*'; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Finds fixtures to be loaded, for example "User", if no fixtures were specified then all of them |
|
399
|
|
|
* will be searching by suffix "Fixture.php". |
|
400
|
|
|
* @param array $fixtures fixtures to be loaded |
|
401
|
|
|
* @return array Array of found fixtures. These may differ from input parameter as not all fixtures may exists. |
|
402
|
|
|
*/ |
|
403
|
13 |
|
private function findFixtures(array $fixtures = []) |
|
404
|
|
|
{ |
|
405
|
13 |
|
$fixturesPath = $this->getFixturePath(); |
|
406
|
|
|
|
|
407
|
13 |
|
$filesToSearch = ['*Fixture.php']; |
|
408
|
13 |
|
$findAll = ($fixtures === []); |
|
409
|
|
|
|
|
410
|
13 |
|
if (!$findAll) { |
|
411
|
9 |
|
$filesToSearch = []; |
|
412
|
|
|
|
|
413
|
9 |
|
foreach ($fixtures as $fileName) { |
|
414
|
9 |
|
$filesToSearch[] = $fileName . 'Fixture.php'; |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
13 |
|
$files = FileHelper::findFiles($fixturesPath, ['only' => $filesToSearch]); |
|
|
|
|
|
|
419
|
13 |
|
$foundFixtures = []; |
|
420
|
|
|
|
|
421
|
13 |
|
foreach ($files as $fixture) { |
|
422
|
11 |
|
$foundFixtures[] = $this->getFixtureRelativeName($fixture); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
13 |
|
return $foundFixtures; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Calculates fixture's name |
|
430
|
|
|
* Basically, strips [[getFixturePath()]] and `Fixture.php' suffix from fixture's full path. |
|
431
|
|
|
* @see getFixturePath() |
|
432
|
|
|
* @param string $fullFixturePath Full fixture path |
|
433
|
|
|
* @return string Relative fixture name |
|
434
|
|
|
*/ |
|
435
|
11 |
|
private function getFixtureRelativeName($fullFixturePath) |
|
436
|
|
|
{ |
|
437
|
11 |
|
$fixturesPath = FileHelper::normalizePath($this->getFixturePath()); |
|
|
|
|
|
|
438
|
11 |
|
$fullFixturePath = FileHelper::normalizePath($fullFixturePath); |
|
439
|
|
|
|
|
440
|
11 |
|
$relativeName = substr($fullFixturePath, strlen($fixturesPath) + 1); |
|
441
|
11 |
|
$relativeDir = dirname($relativeName) === '.' ? '' : dirname($relativeName) . DIRECTORY_SEPARATOR; |
|
442
|
|
|
|
|
443
|
11 |
|
return $relativeDir . basename($fullFixturePath, 'Fixture.php'); |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Returns valid fixtures config that can be used to load them. |
|
448
|
|
|
* @param array $fixtures fixtures to configure |
|
449
|
|
|
* @return array |
|
450
|
|
|
*/ |
|
451
|
11 |
|
private function getFixturesConfig($fixtures) |
|
452
|
|
|
{ |
|
453
|
11 |
|
$config = []; |
|
454
|
|
|
|
|
455
|
11 |
|
foreach ($fixtures as $fixture) { |
|
456
|
11 |
|
$isNamespaced = (strpos($fixture, '\\') !== false); |
|
457
|
|
|
// replace linux' path slashes to namespace backslashes, in case if $fixture is non-namespaced relative path |
|
458
|
11 |
|
$fixture = str_replace('/', '\\', $fixture); |
|
459
|
11 |
|
$fullClassName = $isNamespaced ? $fixture : $this->namespace . '\\' . $fixture; |
|
460
|
|
|
|
|
461
|
11 |
|
if (class_exists($fullClassName)) { |
|
462
|
1 |
|
$config[] = $fullClassName; |
|
463
|
11 |
|
} elseif (class_exists($fullClassName . 'Fixture')) { |
|
464
|
11 |
|
$config[] = $fullClassName . 'Fixture'; |
|
465
|
|
|
} |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
11 |
|
return $config; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Filters fixtures by splitting them in two categories: one that should be applied and not. |
|
473
|
|
|
* |
|
474
|
|
|
* If fixture is prefixed with "-", for example "-User", that means that fixture should not be loaded, |
|
475
|
|
|
* if it is not prefixed it is considered as one to be loaded. Returns array: |
|
476
|
|
|
* |
|
477
|
|
|
* ```php |
|
478
|
|
|
* [ |
|
479
|
|
|
* 'apply' => [ |
|
480
|
|
|
* 'User', |
|
481
|
|
|
* ... |
|
482
|
|
|
* ], |
|
483
|
|
|
* 'except' => [ |
|
484
|
|
|
* 'Custom', |
|
485
|
|
|
* ... |
|
486
|
|
|
* ], |
|
487
|
|
|
* ] |
|
488
|
|
|
* ``` |
|
489
|
|
|
* @param array $fixtures |
|
490
|
|
|
* @return array fixtures array with 'apply' and 'except' elements. |
|
491
|
|
|
*/ |
|
492
|
13 |
|
private function filterFixtures($fixtures) |
|
493
|
|
|
{ |
|
494
|
|
|
$filtered = [ |
|
495
|
13 |
|
'apply' => [], |
|
496
|
|
|
'except' => [], |
|
497
|
|
|
]; |
|
498
|
|
|
|
|
499
|
13 |
|
foreach ($fixtures as $fixture) { |
|
500
|
13 |
|
if (mb_strpos($fixture, '-') !== false) { |
|
501
|
6 |
|
$filtered['except'][] = str_replace('-', '', $fixture); |
|
502
|
|
|
} else { |
|
503
|
13 |
|
$filtered['apply'][] = $fixture; |
|
504
|
|
|
} |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
13 |
|
return $filtered; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
/** |
|
511
|
|
|
* Returns fixture path that determined on fixtures namespace. |
|
512
|
|
|
* @throws InvalidConfigException if fixture namespace is invalid |
|
513
|
|
|
* @return string fixture path |
|
514
|
|
|
*/ |
|
515
|
13 |
|
private function getFixturePath() |
|
516
|
|
|
{ |
|
517
|
|
|
try { |
|
518
|
13 |
|
return Yii::getAlias('@' . str_replace('\\', '/', $this->namespace)); |
|
519
|
|
|
} catch (InvalidArgumentException $e) { |
|
520
|
|
|
throw new InvalidConfigException('Invalid fixture namespace: "' . $this->namespace . '". Please, check your FixtureController::namespace parameter'); |
|
521
|
|
|
} |
|
522
|
|
|
} |
|
523
|
|
|
} |
|
524
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.