/
FixtureManager.php
502 lines (457 loc) · 16.3 KB
/
FixtureManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Fixture;
loadPHPUnitAliases();
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Database\Schema\TableSchema;
use Cake\Database\Schema\TableSchemaAwareInterface;
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\TableSchemaInterface;
use Cake\Utility\Inflector;
use PDOException;
use UnexpectedValueException;
/**
* A factory class to manage the life cycle of test fixtures
*/
class FixtureManager
{
/**
* Was this instance already initialized?
*
* @var bool
*/
protected $_initialized = false;
/**
* Holds the fixture classes that where instantiated
*
* @var \Cake\Datasource\FixtureInterface[]
*/
protected $_loaded = [];
/**
* Holds the fixture classes that where instantiated indexed by class name
*
* @var \Cake\Datasource\FixtureInterface[]
*/
protected $_fixtureMap = [];
/**
* A map of connection names and the fixture currently in it.
*
* @var array
*/
protected $_insertionMap = [];
/**
* List of TestCase class name that have been processed
*
* @var array
*/
protected $_processed = [];
/**
* Is the test runner being run with `--debug` enabled.
* When true, fixture SQL will also be logged.
*
* @var bool
*/
protected $_debug = false;
/**
* Modify the debug mode.
*
* @param bool $debug Whether or not fixture debug mode is enabled.
* @return void
*/
public function setDebug($debug)
{
$this->_debug = $debug;
}
/**
* Inspects the test to look for unloaded fixtures and loads them
*
* @param \Cake\TestSuite\TestCase $test The test case to inspect.
* @return void
*/
public function fixturize($test)
{
$this->_initDb();
if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
return;
}
if (!is_array($test->fixtures)) {
$test->fixtures = array_map('trim', explode(',', $test->fixtures));
}
$this->_loadFixtures($test);
$this->_processed[get_class($test)] = true;
}
/**
* Get the loaded fixtures.
*
* @return array
*/
public function loaded()
{
return $this->_loaded;
}
/**
* Add aliases for all non test prefixed connections.
*
* This allows models to use the test connections without
* a pile of configuration work.
*
* @return void
*/
protected function _aliasConnections()
{
$connections = ConnectionManager::configured();
ConnectionManager::alias('test', 'default');
$map = [];
foreach ($connections as $connection) {
if ($connection === 'test' || $connection === 'default') {
continue;
}
if (isset($map[$connection])) {
continue;
}
if (strpos($connection, 'test_') === 0) {
$map[$connection] = substr($connection, 5);
} else {
$map['test_' . $connection] = $connection;
}
}
foreach ($map as $testConnection => $normal) {
ConnectionManager::alias($testConnection, $normal);
}
}
/**
* Initializes this class with a DataSource object to use as default for all fixtures
*
* @return void
*/
protected function _initDb()
{
if ($this->_initialized) {
return;
}
$this->_aliasConnections();
$this->_initialized = true;
}
/**
* Looks for fixture files and instantiates the classes accordingly
*
* @param \Cake\TestSuite\TestCase $test The test suite to load fixtures for.
* @return void
* @throws \UnexpectedValueException when a referenced fixture does not exist.
*/
protected function _loadFixtures($test)
{
if (empty($test->fixtures)) {
return;
}
foreach ($test->fixtures as $fixture) {
if (isset($this->_loaded[$fixture])) {
continue;
}
if (strpos($fixture, '.')) {
list($type, $pathName) = explode('.', $fixture, 2);
$path = explode('/', $pathName);
$name = array_pop($path);
$additionalPath = implode('\\', $path);
if ($type === 'core') {
$baseNamespace = 'Cake';
} elseif ($type === 'app') {
$baseNamespace = Configure::read('App.namespace');
} elseif ($type === 'plugin') {
list($plugin, $name) = explode('.', $pathName);
// Flip vendored plugin separators
$path = implode('\\', explode('/', $plugin));
$baseNamespace = Inflector::camelize(str_replace('\\', '\ ', $path));
$additionalPath = null;
} else {
$baseNamespace = '';
$name = $fixture;
}
// Tweak subdirectory names, so camelize() can make the correct name
if (strpos($name, '/') > 0) {
$name = implode('\\ ', explode('/', $name));
}
$name = Inflector::camelize($name);
$nameSegments = [
$baseNamespace,
'Test\Fixture',
$additionalPath,
$name . 'Fixture'
];
$className = implode('\\', array_filter($nameSegments));
} else {
$className = $fixture;
$name = preg_replace('/Fixture\z/', '', substr(strrchr($fixture, '\\'), 1));
}
if (class_exists($className)) {
$this->_loaded[$fixture] = new $className();
$this->_fixtureMap[$name] = $this->_loaded[$fixture];
} else {
$msg = sprintf(
'Referenced fixture class "%s" not found. Fixture "%s" was referenced in test case "%s".',
$className,
$fixture,
get_class($test)
);
throw new UnexpectedValueException($msg);
}
}
}
/**
* Runs the drop and create commands on the fixtures if necessary.
*
* @param \Cake\Datasource\FixtureInterface $fixture the fixture object to create
* @param \Cake\Database\Connection $db The Connection object instance to use
* @param array $sources The existing tables in the datasource.
* @param bool $drop whether drop the fixture if it is already created or not
* @return void
*/
protected function _setupTable($fixture, $db, array $sources, $drop = true)
{
$configName = $db->configName();
$isFixtureSetup = $this->isFixtureSetup($configName, $fixture);
if ($isFixtureSetup) {
return;
}
$table = $fixture->sourceName();
$exists = in_array($table, $sources);
$hasSchema = $fixture instanceof TableSchemaInterface && $fixture->schema() instanceof TableSchema
|| $fixture instanceof TableSchemaAwareInterface && $fixture->getTableSchema() instanceof TableSchema;
if (($drop && $exists) || ($exists && !$isFixtureSetup && $hasSchema)) {
$fixture->drop($db);
$fixture->create($db);
} elseif (!$exists) {
$fixture->create($db);
} else {
$fixture->truncate($db);
}
$this->_insertionMap[$configName][] = $fixture;
}
/**
* Creates the fixtures tables and inserts data on them.
*
* @param \Cake\TestSuite\TestCase $test The test to inspect for fixture loading.
* @return void
* @throws \Cake\Core\Exception\Exception When fixture records cannot be inserted.
*/
public function load($test)
{
if (empty($test->fixtures)) {
return;
}
$fixtures = $test->fixtures;
if (empty($fixtures) || !$test->autoFixtures) {
return;
}
try {
$createTables = function ($db, $fixtures) use ($test) {
$tables = $db->schemaCollection()->listTables();
$configName = $db->configName();
if (!isset($this->_insertionMap[$configName])) {
$this->_insertionMap[$configName] = [];
}
foreach ($fixtures as $name => $fixture) {
if (in_array($fixture->table, $tables)) {
try {
$fixture->dropConstraints($db);
} catch (PDOException $e) {
$msg = sprintf(
'Unable to drop constraints for fixture "%s" in "%s" test case: ' . "\n" . '%s',
get_class($fixture),
get_class($test),
$e->getMessage()
);
throw new Exception($msg, null, $e);
}
}
}
foreach ($fixtures as $fixture) {
if (!in_array($fixture, $this->_insertionMap[$configName])) {
$this->_setupTable($fixture, $db, $tables, $test->dropTables);
} else {
$fixture->truncate($db);
}
}
foreach ($fixtures as $name => $fixture) {
try {
$fixture->createConstraints($db);
} catch (PDOException $e) {
$msg = sprintf(
'Unable to create constraints for fixture "%s" in "%s" test case: ' . "\n" . '%s',
get_class($fixture),
get_class($test),
$e->getMessage()
);
throw new Exception($msg, null, $e);
}
}
};
$this->_runOperation($fixtures, $createTables);
// Use a separate transaction because of postgres.
$insert = function ($db, $fixtures) use ($test) {
foreach ($fixtures as $fixture) {
try {
$fixture->insert($db);
} catch (PDOException $e) {
$msg = sprintf(
'Unable to insert fixture "%s" in "%s" test case: ' . "\n" . '%s',
get_class($fixture),
get_class($test),
$e->getMessage()
);
throw new Exception($msg, null, $e);
}
}
};
$this->_runOperation($fixtures, $insert);
} catch (PDOException $e) {
$msg = sprintf(
'Unable to insert fixtures for "%s" test case. %s',
get_class($test),
$e->getMessage()
);
throw new Exception($msg, null, $e);
}
}
/**
* Run a function on each connection and collection of fixtures.
*
* @param array $fixtures A list of fixtures to operate on.
* @param callable $operation The operation to run on each connection + fixture set.
* @return void
*/
protected function _runOperation($fixtures, $operation)
{
$dbs = $this->_fixtureConnections($fixtures);
foreach ($dbs as $connection => $fixtures) {
$db = ConnectionManager::get($connection);
$logQueries = $db->logQueries();
if ($logQueries && !$this->_debug) {
$db->logQueries(false);
}
$db->transactional(function ($db) use ($fixtures, $operation) {
$db->disableConstraints(function ($db) use ($fixtures, $operation) {
$operation($db, $fixtures);
});
});
if ($logQueries) {
$db->logQueries(true);
}
}
}
/**
* Get the unique list of connections that a set of fixtures contains.
*
* @param array $fixtures The array of fixtures a list of connections is needed from.
* @return array An array of connection names.
*/
protected function _fixtureConnections($fixtures)
{
$dbs = [];
foreach ($fixtures as $f) {
if (!empty($this->_loaded[$f])) {
$fixture = $this->_loaded[$f];
$dbs[$fixture->connection()][$f] = $fixture;
}
}
return $dbs;
}
/**
* Truncates the fixtures tables
*
* @param \Cake\TestSuite\TestCase $test The test to inspect for fixture unloading.
* @return void
*/
public function unload($test)
{
if (empty($test->fixtures)) {
return;
}
$truncate = function ($db, $fixtures) {
$configName = $db->configName();
foreach ($fixtures as $name => $fixture) {
if ($this->isFixtureSetup($configName, $fixture)) {
$fixture->dropConstraints($db);
}
}
foreach ($fixtures as $fixture) {
if ($this->isFixtureSetup($configName, $fixture)) {
$fixture->truncate($db);
}
}
};
$this->_runOperation($test->fixtures, $truncate);
}
/**
* Creates a single fixture table and loads data into it.
*
* @param string $name of the fixture
* @param \Cake\Datasource\ConnectionInterface|null $db Connection instance or leave null to get a Connection from the fixture
* @param bool $dropTables Whether or not tables should be dropped and re-created.
* @return void
* @throws \UnexpectedValueException if $name is not a previously loaded class
*/
public function loadSingle($name, $db = null, $dropTables = true)
{
if (!isset($this->_fixtureMap[$name])) {
throw new UnexpectedValueException(sprintf('Referenced fixture class %s not found', $name));
}
$fixture = $this->_fixtureMap[$name];
if (!$db) {
$db = ConnectionManager::get($fixture->connection());
}
if (!$this->isFixtureSetup($db->configName(), $fixture)) {
$sources = $db->schemaCollection()->listTables();
$this->_setupTable($fixture, $db, $sources, $dropTables);
}
if (!$dropTables) {
$fixture->dropConstraints($db);
$fixture->truncate($db);
}
$fixture->createConstraints($db);
$fixture->insert($db);
}
/**
* Drop all fixture tables loaded by this class
*
* @return void
*/
public function shutDown()
{
$shutdown = function ($db, $fixtures) {
$connection = $db->configName();
foreach ($fixtures as $fixture) {
if ($this->isFixtureSetup($connection, $fixture)) {
$fixture->drop($db);
$index = array_search($fixture, $this->_insertionMap[$connection]);
unset($this->_insertionMap[$connection][$index]);
}
}
};
$this->_runOperation(array_keys($this->_loaded), $shutdown);
}
/**
* Check whether or not a fixture has been inserted in a given connection name.
*
* @param string $connection The connection name.
* @param \Cake\Datasource\FixtureInterface $fixture The fixture to check.
* @return bool
*/
public function isFixtureSetup($connection, $fixture)
{
return isset($this->_insertionMap[$connection]) && in_array($fixture, $this->_insertionMap[$connection]);
}
}