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
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-05-08 22:20:55 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-05-08 22:20:55 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite.Fixture
|
2010-05-08 22:20:55 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-05-08 22:20:55 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('ConnectionManager', 'Model');
|
|
|
|
App::uses('ClassRegistry', 'Utility');
|
|
|
|
|
2011-12-08 15:35:02 +00:00
|
|
|
/**
|
|
|
|
* A factory class to manage the life cycle of test fixtures
|
|
|
|
*
|
|
|
|
* @package Cake.TestSuite.Fixture
|
|
|
|
*/
|
2010-05-07 22:36:17 +00:00
|
|
|
class CakeFixtureManager {
|
2010-05-08 22:20:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Was this class already initialized?
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-05-08 22:20:55 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Holds the fixture classes that where instantiated 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-12-08 06:19:36 +00:00
|
|
|
public function fixturize($test) {
|
2012-04-05 02:09:47 +00:00
|
|
|
if (!$this->_initialized) {
|
|
|
|
ClassRegistry::config(array('ds' => 'test', 'testing' => true));
|
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
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-12-08 06:19:36 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
2011-10-12 02:10:09 +00:00
|
|
|
$db->cacheSources = false;
|
2010-09-20 02:41:31 +00:00
|
|
|
$this->_db = $db;
|
2010-05-08 20:22:11 +00:00
|
|
|
$this->_initialized = true;
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 19:55:46 +00:00
|
|
|
/**
|
|
|
|
* Parse the fixture path included in test cases, to get the fixture class name, and the
|
|
|
|
* real fixture path including sub-directories
|
2016-09-14 01:46:28 +00:00
|
|
|
*
|
2014-03-11 19:55:46 +00:00
|
|
|
* @param string $fixturePath the fixture path to parse
|
|
|
|
* @return array containing fixture class name and optional additional path
|
2014-03-11 20:16:30 +00:00
|
|
|
*/
|
|
|
|
protected function _parseFixturePath($fixturePath) {
|
2014-03-11 19:55:46 +00:00
|
|
|
$pathTokenArray = explode('/', $fixturePath);
|
|
|
|
$fixture = array_pop($pathTokenArray);
|
|
|
|
$additionalPath = '';
|
|
|
|
foreach ($pathTokenArray as $pathToken) {
|
|
|
|
$additionalPath .= DS . $pathToken;
|
|
|
|
}
|
|
|
|
return array('fixture' => $fixture, 'additionalPath' => $additionalPath);
|
|
|
|
}
|
|
|
|
|
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
|
2012-06-19 03:29:28 +00:00
|
|
|
* @throws UnexpectedValueException when a referenced fixture does not exist.
|
2010-05-08 22:20:55 +00:00
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
protected function _loadFixtures($fixtures) {
|
2013-01-23 12:45:50 +00:00
|
|
|
foreach ($fixtures as $fixture) {
|
2010-05-07 22:36:17 +00:00
|
|
|
$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;
|
|
|
|
}
|
2010-12-11 05:47:55 +00:00
|
|
|
|
2010-05-07 22:36:17 +00:00
|
|
|
if (strpos($fixture, 'core.') === 0) {
|
|
|
|
$fixture = substr($fixture, strlen('core.'));
|
2011-04-17 10:35:21 +00:00
|
|
|
$fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
|
2010-05-07 22:36:17 +00:00
|
|
|
} elseif (strpos($fixture, 'app.') === 0) {
|
2014-03-11 19:12:52 +00:00
|
|
|
$fixturePrefixLess = substr($fixture, strlen('app.'));
|
2014-03-11 19:55:46 +00:00
|
|
|
$fixtureParsedPath = $this->_parseFixturePath($fixturePrefixLess);
|
|
|
|
$fixture = $fixtureParsedPath['fixture'];
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixturePaths = array(
|
2014-03-11 19:55:46 +00:00
|
|
|
TESTS . 'Fixture' . $fixtureParsedPath['additionalPath']
|
2010-05-07 22:36:17 +00:00
|
|
|
);
|
|
|
|
} elseif (strpos($fixture, 'plugin.') === 0) {
|
2014-03-11 19:55:46 +00:00
|
|
|
$explodedFixture = explode('.', $fixture, 3);
|
2014-03-11 19:12:52 +00:00
|
|
|
$pluginName = $explodedFixture[1];
|
2014-03-11 19:55:46 +00:00
|
|
|
$fixtureParsedPath = $this->_parseFixturePath($explodedFixture[2]);
|
|
|
|
$fixture = $fixtureParsedPath['fixture'];
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixturePaths = array(
|
2014-03-11 19:55:46 +00:00
|
|
|
CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture' . $fixtureParsedPath['additionalPath'],
|
|
|
|
TESTS . 'Fixture' . $fixtureParsedPath['additionalPath']
|
2010-05-07 22:36:17 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$fixturePaths = array(
|
2011-04-11 00:38:24 +00:00
|
|
|
TESTS . 'Fixture',
|
2012-03-05 02:51:44 +00:00
|
|
|
CAKE . 'Test' . DS . 'Fixture'
|
2010-05-07 22:36:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-06-18 13:28:00 +00:00
|
|
|
$loaded = false;
|
2010-05-07 22:36:17 +00:00
|
|
|
foreach ($fixturePaths as $path) {
|
2011-04-11 00:38:24 +00:00
|
|
|
$className = Inflector::camelize($fixture);
|
|
|
|
if (is_readable($path . DS . $className . 'Fixture.php')) {
|
|
|
|
$fixtureFile = $path . DS . $className . 'Fixture.php';
|
2012-03-05 02:51:44 +00:00
|
|
|
require_once $fixtureFile;
|
2011-04-11 00:38:24 +00:00
|
|
|
$fixtureClass = $className . 'Fixture';
|
2011-11-05 06:55:59 +00:00
|
|
|
$this->_loaded[$fixtureIndex] = new $fixtureClass();
|
2011-04-11 00:38:24 +00:00
|
|
|
$this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
|
2012-06-18 13:28:00 +00:00
|
|
|
$loaded = true;
|
2010-05-07 22:36:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-18 13:28:00 +00:00
|
|
|
|
|
|
|
if (!$loaded) {
|
|
|
|
$firstPath = str_replace(array(APP, CAKE_CORE_INCLUDE_PATH, ROOT), '', $fixturePaths[0] . DS . $className . 'Fixture.php');
|
|
|
|
throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s (%s) not found', $className, $firstPath));
|
|
|
|
}
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
2017-08-05 20:15:10 +00:00
|
|
|
* Runs the drop, create and truncate commands on the fixtures if necessary.
|
2010-05-08 22:20:55 +00:00
|
|
|
*
|
|
|
|
* @param CakeTestFixture $fixture the fixture object to create
|
|
|
|
* @param DataSource $db the datasource instance to use
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $drop whether drop the fixture if it is already created or not
|
2010-05-08 22:20:55 +00:00
|
|
|
* @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 (!$db) {
|
2011-10-20 04:24:10 +00:00
|
|
|
if (!empty($fixture->useDbConfig)) {
|
2012-05-04 18:13:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
|
2011-10-20 04:24:10 +00:00
|
|
|
} else {
|
|
|
|
$db = $this->_db;
|
|
|
|
}
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
2011-10-20 04:24:10 +00:00
|
|
|
if (!empty($fixture->created) && in_array($db->configKeyName, $fixture->created)) {
|
2017-08-05 20:15:10 +00:00
|
|
|
$fixture->truncate($db);
|
2011-05-24 17:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-05-07 22:36:17 +00:00
|
|
|
|
2013-01-11 13:14:05 +00:00
|
|
|
$sources = (array)$db->listSources();
|
2010-05-07 22:36:17 +00:00
|
|
|
$table = $db->config['prefix'] . $fixture->table;
|
2012-06-24 16:57:54 +00:00
|
|
|
$exists = in_array($table, $sources);
|
2010-05-07 22:36:17 +00:00
|
|
|
|
2012-06-24 16:57:54 +00:00
|
|
|
if ($drop && $exists) {
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixture->drop($db);
|
|
|
|
$fixture->create($db);
|
2012-06-24 16:57:54 +00:00
|
|
|
} elseif (!$exists) {
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixture->create($db);
|
2012-06-24 16:57:54 +00:00
|
|
|
} else {
|
|
|
|
$fixture->created[] = $db->configKeyName;
|
2017-08-05 20:15:10 +00:00
|
|
|
$fixture->truncate($db);
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
2012-02-17 05:45:37 +00:00
|
|
|
* Creates the fixtures tables and inserts data on them.
|
2010-05-08 22:20:55 +00:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2016-09-13 15:00:32 +00:00
|
|
|
$fixtures = $test->fixtures;
|
2012-09-14 17:26:30 +00:00
|
|
|
if (empty($fixtures) || !$test->autoFixtures) {
|
2010-05-07 22:36:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fixtures as $f) {
|
2010-05-08 20:22:11 +00:00
|
|
|
if (!empty($this->_loaded[$f])) {
|
|
|
|
$fixture = $this->_loaded[$f];
|
2011-10-20 04:24:10 +00:00
|
|
|
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
|
2013-01-05 19:50:09 +00:00
|
|
|
$db->begin();
|
2011-10-20 04:24:10 +00:00
|
|
|
$this->_setupTable($fixture, $db, $test->dropTables);
|
|
|
|
$fixture->insert($db);
|
2013-01-05 19:50:09 +00:00
|
|
|
$db->commit();
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Truncates the fixtures tables
|
2010-05-08 22:20:55 +00:00
|
|
|
*
|
|
|
|
* @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();
|
2011-07-25 19:14:23 +00:00
|
|
|
foreach (array_reverse($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)) {
|
2011-10-20 04:24:10 +00:00
|
|
|
foreach ($fixture->created as $ds) {
|
|
|
|
$db = ConnectionManager::getDataSource($ds);
|
|
|
|
$fixture->truncate($db);
|
|
|
|
}
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
2012-02-17 05:45:37 +00:00
|
|
|
* Creates a single fixture table and loads data into it.
|
2010-05-08 22:20:55 +00:00
|
|
|
*
|
2012-02-17 05:45:37 +00:00
|
|
|
* @param string $name of the fixture
|
|
|
|
* @param DataSource $db DataSource instance or leave null to get DataSource from the fixture
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $dropTables Whether or not tables should be dropped and re-created.
|
2010-05-08 22:20:55 +00:00
|
|
|
* @return void
|
|
|
|
* @throws UnexpectedValueException if $name is not a previously loaded class
|
|
|
|
*/
|
2013-03-12 13:34:20 +00:00
|
|
|
public function loadSingle($name, $db = null, $dropTables = true) {
|
2010-05-07 22:36:17 +00:00
|
|
|
$name .= 'Fixture';
|
2010-05-08 20:22:11 +00:00
|
|
|
if (isset($this->_fixtureMap[$name])) {
|
2011-10-20 04:24:10 +00:00
|
|
|
$fixture = $this->_fixtureMap[$name];
|
2010-05-07 22:36:17 +00:00
|
|
|
if (!$db) {
|
2011-10-20 04:24:10 +00:00
|
|
|
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
2013-03-12 13:34:20 +00:00
|
|
|
$this->_setupTable($fixture, $db, $dropTables);
|
2010-05-07 22:36:17 +00:00
|
|
|
$fixture->insert($db);
|
|
|
|
} else {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
|
2010-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
|
2010-05-08 22:20:55 +00:00
|
|
|
/**
|
|
|
|
* Drop all fixture tables loaded by this class
|
|
|
|
*
|
2013-03-09 01:55:44 +00:00
|
|
|
* This will also close the session, as failing to do so will cause
|
|
|
|
* fatal errors with database sessions.
|
|
|
|
*
|
2010-05-08 22:20:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 20:22:11 +00:00
|
|
|
public function shutDown() {
|
2013-03-09 01:55:44 +00:00
|
|
|
if (session_id()) {
|
|
|
|
session_write_close();
|
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
foreach ($this->_loaded as $fixture) {
|
|
|
|
if (!empty($fixture->created)) {
|
2011-10-20 04:24:10 +00:00
|
|
|
foreach ($fixture->created as $ds) {
|
|
|
|
$db = ConnectionManager::getDataSource($ds);
|
|
|
|
$fixture->drop($db);
|
|
|
|
}
|
2010-05-08 20:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-20 04:24:10 +00:00
|
|
|
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|