2010-05-07 22:36:17 +00:00
|
|
|
<?php
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* A factory class to manage the life cycle of test fixtures
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.libs
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-09-28 03:07:23 +00:00
|
|
|
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
|
2010-05-08 20:18:45 +00:00
|
|
|
|
2010-05-07 22:36:17 +00:00
|
|
|
class CakeFixtureManager {
|
2010-05-08 22:20:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Was this class already initialized?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected $_initialized = false;
|
2010-05-08 22:20:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default datasource to use
|
|
|
|
*
|
|
|
|
* @var DataSource
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected $_db = null;
|
2010-05-08 22:20:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the fixture classes that where instantiated
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected $_loaded = array();
|
2010-05-08 22:20:55 +00:00
|
|
|
|
|
|
|
/**
|
2010-06-01 04:19:18 +00:00
|
|
|
* Holds the fixture classes that where ins tantiated indexed by class name
|
2010-05-08 22:20:55 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected $_fixtureMap = array();
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Inspects the test to look for unloaded fixtures and loads them
|
|
|
|
*
|
|
|
|
* @param CakeTestCase $test the test case to inspect
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
public function fixturize(CakeTestCase $test) {
|
|
|
|
if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
|
|
|
|
$test->db = $this->_db;
|
2010-05-07 22:36:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
$this->_initDb();
|
|
|
|
$test->db = $this->_db;
|
2010-05-07 22:36:17 +00:00
|
|
|
if (!is_array($test->fixtures)) {
|
|
|
|
$test->fixtures = array_map('trim', explode(',', $test->fixtures));
|
|
|
|
}
|
|
|
|
if (isset($test->fixtures)) {
|
2010-05-08 20:22:11 +00:00
|
|
|
$this->_loadFixtures($test->fixtures);
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
|
|
|
|
$this->_processed[get_class($test)] = true;
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Initializes this class with a DataSource object to use as default for all fixtures
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected function _initDb() {
|
|
|
|
if ($this->_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2010-05-07 22:36:17 +00:00
|
|
|
$testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
|
|
|
|
|
|
|
|
$_prefix = null;
|
|
|
|
|
|
|
|
if ($testDbAvailable) {
|
|
|
|
// Try for test DB
|
|
|
|
@$db = ConnectionManager::getDataSource('test');
|
|
|
|
$testDbAvailable = $db->isConnected();
|
2010-09-25 23:45:26 +00:00
|
|
|
} else {
|
|
|
|
throw new MissingConnectionException(__('You need to create a $test datasource connection to start using fixtures'));
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$testDbAvailable) {
|
2010-09-25 23:45:26 +00:00
|
|
|
throw new MissingConnectionException(__('Unable to connect to the $test datasource'));
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
2010-09-20 02:41:31 +00:00
|
|
|
$this->_db = $db;
|
|
|
|
ClassRegistry::config(array('ds' => 'test'));
|
2010-05-08 20:22:11 +00:00
|
|
|
$this->_initialized = true;
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Looks for fixture files and instantiates the classes accordingly
|
|
|
|
*
|
|
|
|
* @param array $fixtures the fixture names to load using the notation {type}.{name}
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected function _loadFixtures($fixtures) {
|
2010-05-07 22:36:17 +00:00
|
|
|
foreach ($fixtures as $index => $fixture) {
|
|
|
|
$fixtureFile = null;
|
|
|
|
$fixtureIndex = $fixture;
|
2010-05-08 20:22:11 +00:00
|
|
|
if (isset($this->_loaded[$fixture])) {
|
2010-05-07 22:36:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strpos($fixture, 'core.') === 0) {
|
|
|
|
$fixture = substr($fixture, strlen('core.'));
|
|
|
|
foreach (App::core('cake') as $key => $path) {
|
|
|
|
$fixturePaths[] = $path . 'tests' . DS . 'fixtures';
|
|
|
|
}
|
|
|
|
} elseif (strpos($fixture, 'app.') === 0) {
|
|
|
|
$fixture = substr($fixture, strlen('app.'));
|
|
|
|
$fixturePaths = array(
|
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
} elseif (strpos($fixture, 'plugin.') === 0) {
|
|
|
|
$parts = explode('.', $fixture, 3);
|
|
|
|
$pluginName = $parts[1];
|
|
|
|
$fixture = $parts[2];
|
|
|
|
$fixturePaths = array(
|
|
|
|
App::pluginPath($pluginName) . 'tests' . DS . 'fixtures',
|
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$fixturePaths = array(
|
|
|
|
TESTS . 'fixtures',
|
|
|
|
VENDORS . 'tests' . DS . 'fixtures',
|
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fixturePaths as $path) {
|
|
|
|
if (is_readable($path . DS . $fixture . '_fixture.php')) {
|
|
|
|
$fixtureFile = $path . DS . $fixture . '_fixture.php';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($fixtureFile)) {
|
|
|
|
require_once($fixtureFile);
|
|
|
|
$fixtureClass = Inflector::camelize($fixture) . 'Fixture';
|
2010-05-08 20:22:11 +00:00
|
|
|
$this->_loaded[$fixtureIndex] = new $fixtureClass($this->_db);
|
|
|
|
$this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Runs the drop and create commands on the fixtures if necessary
|
|
|
|
*
|
|
|
|
* @param CakeTestFixture $fixture the fixture object to create
|
|
|
|
* @param DataSource $db the datasource instance to use
|
|
|
|
* @param boolean $drop whether drop the fixture if it is already created or not
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected function _setupTable($fixture, $db = null, $drop = true) {
|
2010-05-07 22:36:17 +00:00
|
|
|
if (!empty($fixture->created)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!$db) {
|
2010-05-08 20:22:11 +00:00
|
|
|
$db = $this->_db;
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$cacheSources = $db->cacheSources;
|
|
|
|
$db->cacheSources = false;
|
|
|
|
$db->cacheSources = $cacheSources;
|
|
|
|
$sources = $db->listSources();
|
|
|
|
$table = $db->config['prefix'] . $fixture->table;
|
|
|
|
|
|
|
|
if ($drop && in_array($table, $sources)) {
|
|
|
|
$fixture->drop($db);
|
|
|
|
$fixture->create($db);
|
|
|
|
$fixture->created = true;
|
|
|
|
} elseif (!in_array($table, $sources)) {
|
|
|
|
$fixture->create($db);
|
|
|
|
$fixture->created = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Crates the fixtures tables and inserts data on them
|
|
|
|
*
|
|
|
|
* @param CakeTestCase $test the test to inspect for fixture loading
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
public function load(CakeTestCase $test) {
|
2010-05-07 22:36:17 +00:00
|
|
|
if (empty($test->fixtures)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$fixtures = $test->fixtures;
|
|
|
|
if (empty($fixtures) || $test->autoFixtures == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fixtures as $f) {
|
2010-05-08 20:22:11 +00:00
|
|
|
if (!empty($this->_loaded[$f])) {
|
|
|
|
$fixture = $this->_loaded[$f];
|
|
|
|
$this->_setupTable($fixture, $test->db, $test->dropTables);
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixture->insert($test->db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Trucantes the fixtures tables
|
|
|
|
*
|
|
|
|
* @param CakeTestCase $test the test to inspect for fixture unloading
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
public function unload(CakeTestCase $test) {
|
2010-09-25 23:45:26 +00:00
|
|
|
$fixtures = !empty($test->fixtures) ? $test->fixtures : array();
|
2010-05-07 22:36:17 +00:00
|
|
|
foreach ($fixtures as $f) {
|
2010-05-08 20:22:11 +00:00
|
|
|
if (isset($this->_loaded[$f])) {
|
|
|
|
$fixture = $this->_loaded[$f];
|
2010-05-07 22:36:17 +00:00
|
|
|
if (!empty($fixture->created)) {
|
|
|
|
$fixture->truncate($test->db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Trucantes the fixtures tables
|
|
|
|
*
|
|
|
|
* @param CakeTestCase $test the test to inspect for fixture unloading
|
|
|
|
* @return void
|
|
|
|
* @throws UnexpectedValueException if $name is not a previously loaded class
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
public function loadSingle($name, $db = null) {
|
2010-05-07 22:36:17 +00:00
|
|
|
$name .= 'Fixture';
|
2010-05-08 20:22:11 +00:00
|
|
|
if (isset($this->_fixtureMap[$name])) {
|
2010-05-07 22:36:17 +00:00
|
|
|
if (!$db) {
|
2010-05-08 20:22:11 +00:00
|
|
|
$db = $this->_db;
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
$fixture = $this->_fixtureMap[$name];
|
2010-06-01 04:19:18 +00:00
|
|
|
$this->_setupTable($fixture, $db);
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixture->truncate($db);
|
|
|
|
$fixture->insert($db);
|
|
|
|
} else {
|
|
|
|
throw new UnexpectedValueException(sprintf(__('Referenced fixture class %s not found'), $name));
|
|
|
|
}
|
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Drop all fixture tables loaded by this class
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
public function shutDown() {
|
|
|
|
foreach ($this->_loaded as $fixture) {
|
|
|
|
if (!empty($fixture->created)) {
|
|
|
|
$fixture->drop($this->_db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|