Skip to content

Instantly share code, notes, and snippets.

@akiyan
Created November 7, 2013 09:41
Show Gist options
  • Save akiyan/7351902 to your computer and use it in GitHub Desktop.
Save akiyan/7351902 to your computer and use it in GitHub Desktop.
function init_db_by_fixture() {
require_once CAKE . 'TestSuite' . DS . 'Fixture' . DS . 'CakeTestFixture.php';
$model_files = glob(APP . 'Model/*.php');
$fixture_db = ConnectionManager::getDataSource('test');
foreach ($model_files as $path) {
$model = Inflector::camelize(str_replace('.php', '', basename($path)));
$modelObj = ClassRegistry::init($model);
if (isset($modelObj->useDbConfig) && $modelObj->useDbConfig == 'default' && is_string($modelObj->useTable)) {
$fixtureName = $model;
$fixture_path = TESTS . 'Fixture' . DS . $fixtureName . 'Fixture.php';
if (is_readable($fixture_path)) {
$this->out($model);
$fixtureFile = $fixture_path;
require_once($fixtureFile);
$fixtureClass = Inflector::camelize($fixtureName) . 'Fixture';
$fixture =& new $fixtureClass($fixture_db);
$fixture->drop($fixture_db);
$fixture->create($fixture_db);
$fixture->insert($fixture_db);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment