/
FixtureManager.php
306 lines (285 loc) · 8.18 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
<?php
/**
* A factory class to manage the life cycle of test fixtures
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Fixture;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Error;
use Cake\TestSuite\Fixture\TestFixture;
use Cake\TestSuite\TestCase;
use Cake\Utility\Inflector;
/**
* A factory class to manage the life cycle of test fixtures
*
*/
class FixtureManager {
/**
* Was this class already initialized?
*
* @var boolean
*/
protected $_initialized = false;
/**
* Holds the fixture classes that where instantiated
*
* @var array
*/
protected $_loaded = array();
/**
* Holds the fixture classes that where instantiated indexed by class name
*
* @var array
*/
protected $_fixtureMap = array();
/**
* 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;
}
$test->db = ConnectionManager::get('test', false);
if (!is_array($test->fixtures)) {
$test->fixtures = array_map('trim', explode(',', $test->fixtures));
}
$this->_loadFixtures($test);
$this->_processed[get_class($test)] = true;
}
/**
* Add aliaes 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 = [
'test' => 'default',
];
foreach ($connections as $connection) {
if (isset($map[$connection])) {
continue;
}
if (strpos($connection, 'test_') === 0) {
$map[$connection] = substr($connection, 5);
} else {
$map['test_' . $connection] = $connection;
}
}
foreach ($map as $alias => $connection) {
ConnectionManager::alias($alias, $connection);
}
}
/**
* 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, 'core.') === 0) {
list($core, $base) = explode('.', $fixture, 2);
$baseNamespace = 'Cake';
} elseif (strpos($fixture, 'app.') === 0) {
list($app, $base) = explode('.', $fixture, 2);
$baseNamespace = Configure::read('App.namespace');
} elseif (strpos($fixture, 'plugin.') === 0) {
list($p, $plugin, $base) = explode('.', $fixture);
$baseNamespace = Plugin::getNamespace($plugin);
} else {
$base = $fixture;
}
$base = Inflector::camelize($base);
$className = implode('\\', array($baseNamespace, 'Test\Fixture', $base . 'Fixture'));
if (class_exists($className)) {
$this->_loaded[$fixture] = new $className();
$this->_fixtureMap[$base] = $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\TestSuite\Fixture\TestFixture $fixture the fixture object to create
* @param Connection $db the datasource instance to use
* @param boolean $drop whether drop the fixture if it is already created or not
* @return void
*/
protected function _setupTable(TestFixture $fixture, Connection $db = null, $drop = true) {
if (!$db) {
if (!empty($fixture->connection)) {
$db = ConnectionManager::get($fixture->connection, false);
}
if (!$db) {
$db = ConnectionManager::get('test', false);
}
}
if (!empty($fixture->created) && in_array($db->configName(), $fixture->created)) {
return;
}
$schemaCollection = $db->schemaCollection();
$sources = (array)$schemaCollection->listTables();
$table = $fixture->table;
$exists = in_array($table, $sources);
if ($drop && $exists) {
$fixture->drop($db);
$fixture->create($db);
} elseif (!$exists) {
$fixture->create($db);
} else {
$fixture->created[] = $db->configName();
}
}
/**
* 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\Error\Exception When fixture records cannot be inserted.
*/
public function load(TestCase $test) {
if (empty($test->fixtures)) {
return;
}
$fixtures = $test->fixtures;
if (empty($fixtures) || !$test->autoFixtures) {
return;
}
$dbs = [];
foreach ($fixtures as $f) {
if (!empty($this->_loaded[$f])) {
$fixture = $this->_loaded[$f];
$dbs[$fixture->connection][$f] = $fixture;
}
}
try {
foreach ($dbs as $db => $fixtures) {
$db = ConnectionManager::get($fixture->connection, false);
$db->transactional(function($db) use ($fixtures, $test) {
foreach ($fixtures as $fixture) {
$this->_setupTable($fixture, $db, $test->dropTables);
$fixture->truncate($db);
$fixture->insert($db);
}
});
}
} catch (\PDOException $e) {
$msg = sprintf('Unable to insert fixtures for "%s" test case. %s', get_class($test), $e->getMessage());
throw new Error\Exception($msg);
}
}
/**
* Truncates the fixtures tables
*
* @param Cake\TestSuite\TestCase $test the test to inspect for fixture unloading
* @return void
*/
public function unload(TestCase $test) {
$fixtures = !empty($test->fixtures) ? $test->fixtures : array();
foreach (array_reverse($fixtures) as $f) {
if (isset($this->_loaded[$f])) {
$fixture = $this->_loaded[$f];
if (!empty($fixture->created)) {
foreach ($fixture->created as $ds) {
$db = ConnectionManager::get($ds);
$fixture->truncate($db);
}
}
}
}
}
/**
* Creates a single fixture table and loads data into it.
*
* @param string $name of the fixture
* @param DataSource $db DataSource instance or leave null to get DataSource from the fixture
* @param boolean $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])) {
$fixture = $this->_fixtureMap[$name];
if (!$db) {
$db = ConnectionManager::get($fixture->connection);
}
$this->_setupTable($fixture, $db, $dropTables);
$fixture->truncate($db);
$fixture->insert($db);
} else {
throw new \UnexpectedValueException(sprintf('Referenced fixture class %s not found', $name));
}
}
/**
* Drop all fixture tables loaded by this class
*
* This will also close the session, as failing to do so will cause
* fatal errors with database sessions.
*
* @return void
*/
public function shutDown() {
if (session_id()) {
session_write_close();
}
foreach ($this->_loaded as $fixture) {
if (!empty($fixture->created)) {
foreach ($fixture->created as $ds) {
$db = ConnectionManager::get($ds);
$fixture->drop($db);
}
}
}
}
}