Upgrade CakePHP from 2.2.5 to 2.9.5

This commit is contained in:
Brm Ko 2017-02-26 15:29:44 +01:00
parent 5a580df460
commit 235a541597
793 changed files with 60746 additions and 23753 deletions

View file

@ -2,19 +2,18 @@
/**
* A factory class to manage the life cycle of test fixtures
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.TestSuite.Fixture
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ConnectionManager', 'Model');
@ -30,7 +29,7 @@ class CakeFixtureManager {
/**
* Was this class already initialized?
*
* @var boolean
* @var bool
*/
protected $_initialized = false;
@ -96,6 +95,23 @@ class CakeFixtureManager {
$this->_initialized = true;
}
/**
* Parse the fixture path included in test cases, to get the fixture class name, and the
* real fixture path including sub-directories
*
* @param string $fixturePath the fixture path to parse
* @return array containing fixture class name and optional additional path
*/
protected function _parseFixturePath($fixturePath) {
$pathTokenArray = explode('/', $fixturePath);
$fixture = array_pop($pathTokenArray);
$additionalPath = '';
foreach ($pathTokenArray as $pathToken) {
$additionalPath .= DS . $pathToken;
}
return array('fixture' => $fixture, 'additionalPath' => $additionalPath);
}
/**
* Looks for fixture files and instantiates the classes accordingly
*
@ -104,7 +120,7 @@ class CakeFixtureManager {
* @throws UnexpectedValueException when a referenced fixture does not exist.
*/
protected function _loadFixtures($fixtures) {
foreach ($fixtures as $index => $fixture) {
foreach ($fixtures as $fixture) {
$fixtureFile = null;
$fixtureIndex = $fixture;
if (isset($this->_loaded[$fixture])) {
@ -115,17 +131,20 @@ class CakeFixtureManager {
$fixture = substr($fixture, strlen('core.'));
$fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
} elseif (strpos($fixture, 'app.') === 0) {
$fixture = substr($fixture, strlen('app.'));
$fixturePrefixLess = substr($fixture, strlen('app.'));
$fixtureParsedPath = $this->_parseFixturePath($fixturePrefixLess);
$fixture = $fixtureParsedPath['fixture'];
$fixturePaths = array(
TESTS . 'Fixture'
TESTS . 'Fixture' . $fixtureParsedPath['additionalPath']
);
} elseif (strpos($fixture, 'plugin.') === 0) {
$parts = explode('.', $fixture, 3);
$pluginName = $parts[1];
$fixture = $parts[2];
$explodedFixture = explode('.', $fixture, 3);
$pluginName = $explodedFixture[1];
$fixtureParsedPath = $this->_parseFixturePath($explodedFixture[2]);
$fixture = $fixtureParsedPath['fixture'];
$fixturePaths = array(
CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture',
TESTS . 'Fixture'
CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture' . $fixtureParsedPath['additionalPath'],
TESTS . 'Fixture' . $fixtureParsedPath['additionalPath']
);
} else {
$fixturePaths = array(
@ -160,7 +179,7 @@ class CakeFixtureManager {
*
* @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
* @param bool $drop whether drop the fixture if it is already created or not
* @return void
*/
protected function _setupTable($fixture, $db = null, $drop = true) {
@ -175,7 +194,7 @@ class CakeFixtureManager {
return;
}
$sources = $db->listSources();
$sources = (array)$db->listSources();
$table = $db->config['prefix'] . $fixture->table;
$exists = in_array($table, $sources);
@ -200,7 +219,7 @@ class CakeFixtureManager {
return;
}
$fixtures = $test->fixtures;
if (empty($fixtures) || $test->autoFixtures == false) {
if (empty($fixtures) || !$test->autoFixtures) {
return;
}
@ -210,6 +229,7 @@ class CakeFixtureManager {
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
$db->begin();
$this->_setupTable($fixture, $db, $test->dropTables);
$fixture->truncate($db);
$fixture->insert($db);
$db->commit();
}
@ -242,17 +262,18 @@ class CakeFixtureManager {
*
* @param string $name of the fixture
* @param DataSource $db DataSource instance or leave null to get DataSource from the fixture
* @param bool $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) {
public function loadSingle($name, $db = null, $dropTables = true) {
$name .= 'Fixture';
if (isset($this->_fixtureMap[$name])) {
$fixture = $this->_fixtureMap[$name];
if (!$db) {
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
}
$this->_setupTable($fixture, $db);
$this->_setupTable($fixture, $db, $dropTables);
$fixture->truncate($db);
$fixture->insert($db);
} else {
@ -263,9 +284,15 @@ class CakeFixtureManager {
/**
* 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) {

View file

@ -1,16 +1,17 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.TestSuite.Fixture
* @since CakePHP(tm) v 1.2.0.4667
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeSchema', 'Model');
@ -31,7 +32,7 @@ class CakeTestFixture {
public $name = null;
/**
* Cake's DBO driver (e.g: DboMysql).
* CakePHP's DBO driver (e.g: DboMysql).
*
* @var object
*/
@ -58,6 +59,37 @@ class CakeTestFixture {
*/
public $created = array();
/**
* Fields / Schema for the fixture.
* This array should match the output of Model::schema()
*
* @var array
*/
public $fields = array();
/**
* Fixture records to be inserted.
*
* @var array
*/
public $records = array();
/**
* The primary key for the table this fixture represents.
*
* @var string
*/
public $primaryKey = null;
/**
* Fixture data can be stored in memory by default.
* When table is created for a fixture the MEMORY engine is used
* where possible. Set $canUseMemory to false if you don't want this.
*
* @var bool
*/
public $canUseMemory = true;
/**
* Instantiate the fixture.
*
@ -116,6 +148,7 @@ class CakeTestFixture {
$this->fields = $model->schema(true);
$this->fields[$model->primaryKey]['key'] = 'primary';
$this->table = $db->fullTableName($model, false, false);
$this->primaryKey = $model->primaryKey;
ClassRegistry::config(array('ds' => 'test'));
ClassRegistry::flush();
} elseif (isset($import['table'])) {
@ -127,6 +160,7 @@ class CakeTestFixture {
$model->table = $import['table'];
$model->tablePrefix = $db->config['prefix'];
$this->fields = $model->schema(true);
$this->primaryKey = $model->primaryKey;
ClassRegistry::flush();
}
@ -165,8 +199,8 @@ class CakeTestFixture {
/**
* Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
*
* @param object $db An instance of the database object used to create the fixture table
* @return boolean True on success, false on failure
* @param DboSource $db An instance of the database object used to create the fixture table
* @return bool True on success, false on failure
*/
public function create($db) {
if (!isset($this->fields) || empty($this->fields)) {
@ -174,8 +208,8 @@ class CakeTestFixture {
}
if (empty($this->fields['tableParameters']['engine'])) {
$canUseMemory = true;
foreach ($this->fields as $field => $args) {
$canUseMemory = $this->canUseMemory;
foreach ($this->fields as $args) {
if (is_string($args)) {
$type = $args;
@ -216,8 +250,8 @@ class CakeTestFixture {
/**
* Run after all tests executed, should return SQL statement to drop table for this fixture.
*
* @param object $db An instance of the database object used to create the fixture table
* @return boolean True on success, false on failure
* @param DboSource $db An instance of the database object used to create the fixture table
* @return bool True on success, false on failure
*/
public function drop($db) {
if (empty($this->fields)) {
@ -238,8 +272,9 @@ class CakeTestFixture {
* Run before each tests is executed, should return a set of SQL statements to insert records for the table
* of this fixture could be executed successfully.
*
* @param object $db An instance of the database into which the records will be inserted
* @return boolean on success or if there are no records to insert, or false on failure
* @param DboSource $db An instance of the database into which the records will be inserted
* @return bool on success or if there are no records to insert, or false on failure
* @throws CakeException if counts of values and fields do not match.
*/
public function insert($db) {
if (!isset($this->_insert)) {
@ -252,12 +287,30 @@ class CakeTestFixture {
$fields = array_unique($fields);
$default = array_fill_keys($fields, null);
foreach ($this->records as $record) {
$fields = array_keys($record);
$values[] = array_values(array_merge($default, $record));
$mergeData = array_merge($default, $record);
$merge = array_values($mergeData);
if (count($fields) !== count($merge)) {
$mergeFields = array_diff_key(array_keys($mergeData), $fields);
$message = 'Fixture invalid: Count of fields does not match count of values in ' . get_class($this) . "\n";
foreach ($mergeFields as $field) {
$message .= "The field '" . $field . "' is in the data fixture but not in the schema." . "\n";
}
throw new CakeException($message);
}
$values[] = $merge;
}
$nested = $db->useNestedTransactions;
$db->useNestedTransactions = false;
$result = $db->insertMulti($this->table, $fields, $values);
if ($this->primaryKey &&
isset($this->fields[$this->primaryKey]['type']) &&
in_array($this->fields[$this->primaryKey]['type'], array('integer', 'biginteger'))
) {
$db->resetSequence($this->table, $this->primaryKey);
}
$db->useNestedTransactions = $nested;
return $result;
}
@ -266,11 +319,11 @@ class CakeTestFixture {
}
/**
* Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
* truncate.
* Truncates the current fixture. Can be overwritten by classes extending
* CakeFixture to trigger other events before / after truncate.
*
* @param object $db A reference to a db instance
* @return boolean
* @param DboSource $db A reference to a db instance
* @return bool
*/
public function truncate($db) {
$fullDebug = $db->fullDebug;

View file

@ -1,16 +1,17 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.TestSuite.Fixture
* @since CakePHP(tm) v 1.2.0.4667
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Model', 'Model');
@ -31,40 +32,27 @@ class CakeTestModel extends Model {
* incorrect order when no order has been defined in the finds.
* Postgres can return the results in any order it considers appropriate if none is specified
*
* @param array $queryData
* @return array $queryData
* @param int|string|array $id Set this ID for this model on startup, can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
*/
public function beforeFind($queryData) {
$pk = $this->primaryKey;
$aliasedPk = $this->alias . '.' . $this->primaryKey;
switch (true) {
case !$pk:
case !$this->useTable:
case !$this->schema('id'):
case !empty($queryData['order'][0]):
case !empty($queryData['group']):
case
(is_string($queryData['fields']) && !($queryData['fields'] == $pk || $queryData['fields'] == $aliasedPk)) ||
(is_array($queryData['fields']) && !(array_key_exists($pk, $queryData['fields']) || array_key_exists($aliasedPk, $queryData['fields']))):
break;
default:
$queryData['order'] = array($this->alias . '.' . $this->primaryKey => 'ASC');
break;
}
return $queryData;
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->order = array($this->alias . '.' . $this->primaryKey => 'ASC');
}
/**
* Overriding save() to set CakeTestSuiteDispatcher::date() as formatter for created, modified and updated fields
*
* @param array $data
* @param boolean|array $validate
* @param array $fieldList
* @param array $data Data to save
* @param bool|array $validate Validate or options.
* @param array $fieldList Whitelist of fields
* @return mixed
*/
public function save($data = null, $validate = true, $fieldList = array()) {
$db = $this->getDataSource();
$db->columns['datetime']['formatter'] = 'CakeTestSuiteDispatcher::date';
return parent::save($data, $validate, $fieldList);
}
}
}