Skip to content
Permalink
Browse files

DRY out fixture manager.

Make common code shared across the various load/truncate/drop
operations.
  • Loading branch information...
markstory committed Sep 7, 2014
1 parent ecd6ca6 commit c8a2625c42a6040313f3eb9acc1d2107a1366e2e
Showing with 47 additions and 39 deletions.
  1. +47 −39 src/TestSuite/Fixture/FixtureManager.php
@@ -225,29 +225,44 @@ public function load($test) {
$dbs = $this->_fixtureConnections($fixtures);
try {
foreach ($dbs as $connection => $fixtures) {
$db = ConnectionManager::get($connection, false);
$db->transactional(function($db) use ($fixtures, $test) {
$tables = $db->schemaCollection()->listTables();
$db->disableForeignKeys();
foreach ($fixtures as $fixture) {
if (!in_array($db->configName(), (array)$fixture->created)) {
$this->_setupTable($fixture, $db, $tables, $test->dropTables);
}
if (!$test->dropTables) {
$fixture->truncate($db);
}
$fixture->insert($db);
$createTables = function($db, $fixtures) use ($test) {
$tables = $db->schemaCollection()->listTables();
foreach ($fixtures as $fixture) {
if (!in_array($db->configName(), (array)$fixture->created)) {
$this->_setupTable($fixture, $db, $tables, $test->dropTables);
}
$db->enableForeignKeys();
});
}
if (!$test->dropTables) {
$fixture->truncate($db);
}
$fixture->insert($db);
}
};
$this->_runOperation($fixtures, $createTables);
} catch (\PDOException $e) {
$msg = sprintf('Unable to insert fixtures for "%s" test case. %s', get_class($test), $e->getMessage());
throw new Exception($msg);
}
}
/**
* 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, false);
$db->transactional(function($db) use ($fixtures, $operation) {
$db->disableForeignKeys();
$operation($db, $fixtures);
$db->enableForeignKeys();
});
}
}
/**
* Get the unique list of connections that a set of fixtures contains.
*
@@ -273,19 +288,15 @@ protected function _fixtureConnections($fixtures) {
*/
public function unload($test) {
$fixtures = !empty($test->fixtures) ? $test->fixtures : [];
$dbs = $this->_fixtureConnections($fixtures);
foreach ($dbs as $connection => $fixtures) {
$db = ConnectionManager::get($connection, false);
$db->transactional(function($db) use ($fixtures, $connection) {
$db->disableForeignKeys();
foreach ($fixtures as $fixture) {
if (!empty($fixture->created) && in_array($connection, $fixture->created)) {
$fixture->truncate($db);
}
$truncate = function($db, $fixtures) {
$connection = $db->configName();
foreach ($fixtures as $fixture) {
if (!empty($fixture->created) && in_array($connection, $fixture->created)) {
$fixture->truncate($db);
}
$db->enableForeignKeys();
});
}
}
};
$this->_runOperation($fixtures, $truncate);
}
/**
@@ -322,18 +333,15 @@ public function loadSingle($name, $db = null, $dropTables = true) {
* @return void
*/
public function shutDown() {
$dbs = $this->_fixtureConnections(array_keys($this->_loaded));
foreach ($dbs as $connection => $fixtures) {
$db = ConnectionManager::get($connection, false);
$db->transactional(function($db) use ($fixtures, $connection) {
$db->disableForeignKeys();
foreach ($fixtures as $fixture) {
if (!empty($fixture->created) && in_array($connection, $fixture->created)) {
$fixture->drop($db);
}
$shutdown = function($db, $fixtures) {
$connection = $db->configName();
foreach ($fixtures as $fixture) {
if (!empty($fixture->created) && in_array($connection, $fixture->created)) {
$fixture->drop($db);
}
});
}
}
};
$this->_runOperation(array_keys($this->_loaded), $shutdown);
}
}

0 comments on commit c8a2625

Please sign in to comment.
You can’t perform that action at this time.