2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +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-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite.Fixture
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4667
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2010-12-10 06:23:27 +00:00
|
|
|
App::uses('CakeSchema', 'Model');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-07-18 01:55:29 +00:00
|
|
|
* CakeTestFixture is responsible for building and destroying tables to be used
|
2011-12-08 15:35:02 +00:00
|
|
|
* during testing.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite.Fixture
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-15 03:27:34 +00:00
|
|
|
class CakeTestFixture {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-24 16:29:18 +00:00
|
|
|
/**
|
|
|
|
* Name of the object
|
|
|
|
*
|
|
|
|
* @var string
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = null;
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-09-27 17:36:43 +00:00
|
|
|
* CakePHP's DBO driver (e.g: DboMysql).
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-03-05 02:51:44 +00:00
|
|
|
* @var object
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $db = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-10-20 04:24:10 +00:00
|
|
|
/**
|
|
|
|
* Fixture Datasource
|
|
|
|
*
|
2012-03-05 02:51:44 +00:00
|
|
|
* @var string
|
2011-10-20 04:24:10 +00:00
|
|
|
*/
|
|
|
|
public $useDbConfig = 'test';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Full Table Name
|
|
|
|
*
|
2012-03-05 02:51:44 +00:00
|
|
|
* @var string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $table = null;
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2011-10-20 04:24:10 +00:00
|
|
|
/**
|
|
|
|
* List of datasources where this fixture has been created
|
|
|
|
*
|
2012-03-05 02:51:44 +00:00
|
|
|
* @var array
|
2011-10-20 04:24:10 +00:00
|
|
|
*/
|
|
|
|
public $created = array();
|
|
|
|
|
2012-09-18 18:11:51 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2014-03-28 13:20:48 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2014-03-28 13:20:48 +00:00
|
|
|
*/
|
|
|
|
public $canUseMemory = true;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Instantiate the fixture.
|
|
|
|
*
|
2012-03-05 02:51:44 +00:00
|
|
|
* @throws CakeException on invalid datasource usage.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function __construct() {
|
2011-01-19 23:15:33 +00:00
|
|
|
if ($this->name === null) {
|
|
|
|
if (preg_match('/^(.*)Fixture$/', get_class($this), $matches)) {
|
|
|
|
$this->name = $matches[1];
|
|
|
|
} else {
|
|
|
|
$this->name = get_class($this);
|
|
|
|
}
|
|
|
|
}
|
2011-10-20 04:24:10 +00:00
|
|
|
$connection = 'test';
|
|
|
|
if (!empty($this->useDbConfig)) {
|
|
|
|
$connection = $this->useDbConfig;
|
|
|
|
if (strpos($connection, 'test') !== 0) {
|
2012-10-05 00:41:02 +00:00
|
|
|
$message = __d(
|
|
|
|
'cake_dev',
|
|
|
|
'Invalid datasource name "%s" for "%s" fixture. Fixture datasource names must begin with "test".',
|
|
|
|
$connection,
|
|
|
|
$this->name
|
|
|
|
);
|
|
|
|
throw new CakeException($message);
|
2011-10-20 04:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => $connection));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->init();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Initialize the fixture.
|
|
|
|
*
|
2012-03-05 02:51:44 +00:00
|
|
|
* @return void
|
|
|
|
* @throws MissingModelException Whe importing from a model that does not exist.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-11 05:22:25 +00:00
|
|
|
public function init() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
|
2010-03-23 03:28:58 +00:00
|
|
|
$import = array_merge(
|
2011-04-08 17:50:36 +00:00
|
|
|
array('connection' => 'default', 'records' => false),
|
2010-03-23 03:28:58 +00:00
|
|
|
is_array($this->import) ? $this->import : array('model' => $this->import)
|
|
|
|
);
|
2008-10-15 16:50:10 +00:00
|
|
|
|
2011-10-20 04:24:10 +00:00
|
|
|
$this->Schema->connection = $import['connection'];
|
2010-12-10 06:23:27 +00:00
|
|
|
if (isset($import['model'])) {
|
|
|
|
list($plugin, $modelClass) = pluginSplit($import['model'], true);
|
|
|
|
App::uses($modelClass, $plugin . 'Model');
|
|
|
|
if (!class_exists($modelClass)) {
|
|
|
|
throw new MissingModelException(array('class' => $modelClass));
|
|
|
|
}
|
2010-09-26 02:23:48 +00:00
|
|
|
$model = new $modelClass(null, null, $import['connection']);
|
|
|
|
$db = $model->getDataSource();
|
|
|
|
if (empty($model->tablePrefix)) {
|
|
|
|
$model->tablePrefix = $db->config['prefix'];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->fields = $model->schema(true);
|
|
|
|
$this->fields[$model->primaryKey]['key'] = 'primary';
|
2011-11-05 10:57:08 +00:00
|
|
|
$this->table = $db->fullTableName($model, false, false);
|
2012-09-18 18:11:51 +00:00
|
|
|
$this->primaryKey = $model->primaryKey;
|
2010-09-20 02:58:30 +00:00
|
|
|
ClassRegistry::config(array('ds' => 'test'));
|
2008-11-12 23:04:05 +00:00
|
|
|
ClassRegistry::flush();
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (isset($import['table'])) {
|
2010-04-15 03:27:34 +00:00
|
|
|
$model = new Model(null, $import['table'], $import['connection']);
|
|
|
|
$db = ConnectionManager::getDataSource($import['connection']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$db->cacheSources = false;
|
2008-09-04 01:49:29 +00:00
|
|
|
$model->useDbConfig = $import['connection'];
|
2008-05-30 11:40:08 +00:00
|
|
|
$model->name = Inflector::camelize(Inflector::singularize($import['table']));
|
|
|
|
$model->table = $import['table'];
|
|
|
|
$model->tablePrefix = $db->config['prefix'];
|
|
|
|
$this->fields = $model->schema(true);
|
2012-09-18 18:11:51 +00:00
|
|
|
$this->primaryKey = $model->primaryKey;
|
2010-07-30 02:01:12 +00:00
|
|
|
ClassRegistry::flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
|
|
|
|
$this->table = str_replace($db->config['prefix'], '', $this->table);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->records = array();
|
|
|
|
$query = array(
|
2010-03-23 03:28:58 +00:00
|
|
|
'fields' => $db->fields($model, null, array_keys($this->fields)),
|
2010-06-09 03:30:57 +00:00
|
|
|
'table' => $db->fullTableName($model),
|
2008-05-30 11:40:08 +00:00
|
|
|
'alias' => $model->alias,
|
|
|
|
'conditions' => array(),
|
|
|
|
'order' => null,
|
2008-06-15 09:17:23 +00:00
|
|
|
'limit' => null,
|
|
|
|
'group' => null
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
$records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
|
|
|
|
|
|
|
|
if ($records !== false && !empty($records)) {
|
2012-03-12 03:03:25 +00:00
|
|
|
$this->records = Hash::extract($records, '{n}.' . $model->alias);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->table)) {
|
|
|
|
$this->table = Inflector::underscore(Inflector::pluralize($this->name));
|
|
|
|
}
|
2008-07-30 19:53:12 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($this->primaryKey) && isset($this->fields['id'])) {
|
|
|
|
$this->primaryKey = 'id';
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-22 15:15:28 +00:00
|
|
|
* Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-09-18 18:11:51 +00:00
|
|
|
* @param DboSource $db An instance of the database object used to create the fixture table
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True on success, false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public function create($db) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($this->fields) || empty($this->fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-22 15:15:28 +00:00
|
|
|
|
2011-05-14 16:08:11 +00:00
|
|
|
if (empty($this->fields['tableParameters']['engine'])) {
|
2014-03-28 13:20:48 +00:00
|
|
|
$canUseMemory = $this->canUseMemory;
|
2013-01-23 12:45:50 +00:00
|
|
|
foreach ($this->fields as $args) {
|
2011-05-14 16:08:11 +00:00
|
|
|
|
|
|
|
if (is_string($args)) {
|
|
|
|
$type = $args;
|
|
|
|
} elseif (!empty($args['type'])) {
|
|
|
|
$type = $args['type'];
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-05-14 16:44:26 +00:00
|
|
|
if (in_array($type, array('blob', 'text', 'binary'))) {
|
2011-05-14 16:08:11 +00:00
|
|
|
$canUseMemory = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($canUseMemory) {
|
|
|
|
$this->fields['tableParameters']['engine'] = 'MEMORY';
|
|
|
|
}
|
|
|
|
}
|
2010-04-09 10:41:28 +00:00
|
|
|
$this->Schema->build(array($this->table => $this->fields));
|
2011-09-03 23:28:53 +00:00
|
|
|
try {
|
|
|
|
$db->execute($db->createSchema($this->Schema), array('log' => false));
|
2011-10-20 04:24:10 +00:00
|
|
|
$this->created[] = $db->configKeyName;
|
2011-09-03 23:28:53 +00:00
|
|
|
} catch (Exception $e) {
|
2012-07-18 02:48:50 +00:00
|
|
|
$msg = __d(
|
|
|
|
'cake_dev',
|
|
|
|
'Fixture creation for "%s" failed "%s"',
|
|
|
|
$this->table,
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
CakeLog::error($msg);
|
|
|
|
trigger_error($msg, E_USER_WARNING);
|
2011-09-03 23:28:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Run after all tests executed, should return SQL statement to drop table for this fixture.
|
|
|
|
*
|
2012-09-18 18:11:51 +00:00
|
|
|
* @param DboSource $db An instance of the database object used to create the fixture table
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True on success, false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public function drop($db) {
|
2011-02-09 02:53:12 +00:00
|
|
|
if (empty($this->fields)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-09 10:41:28 +00:00
|
|
|
$this->Schema->build(array($this->table => $this->fields));
|
2011-09-03 23:28:53 +00:00
|
|
|
try {
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2011-09-03 23:28:53 +00:00
|
|
|
$db->execute($db->dropSchema($this->Schema), array('log' => false));
|
2012-03-05 02:51:44 +00:00
|
|
|
$this->created = array_diff($this->created, array($db->configKeyName));
|
2011-09-03 23:28:53 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-07-30 19:53:12 +00:00
|
|
|
* Run before each tests is executed, should return a set of SQL statements to insert records for the table
|
2008-06-22 15:15:28 +00:00
|
|
|
* of this fixture could be executed successfully.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-09-18 18:11:51 +00:00
|
|
|
* @param DboSource $db An instance of the database into which the records will be inserted
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool on success or if there are no records to insert, or false on failure
|
2014-01-06 19:43:30 +00:00
|
|
|
* @throws CakeException if counts of values and fields do not match.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public function insert($db) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($this->_insert)) {
|
|
|
|
$values = array();
|
|
|
|
if (isset($this->records) && !empty($this->records)) {
|
2011-04-08 17:50:36 +00:00
|
|
|
$fields = array();
|
2011-11-30 15:44:11 +00:00
|
|
|
foreach ($this->records as $record) {
|
2011-04-08 17:50:36 +00:00
|
|
|
$fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
|
|
|
|
}
|
|
|
|
$fields = array_unique($fields);
|
|
|
|
$default = array_fill_keys($fields, null);
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($this->records as $record) {
|
2014-12-09 11:56:34 +00:00
|
|
|
$mergeData = array_merge($default, $record);
|
|
|
|
$merge = array_values($mergeData);
|
2014-01-06 19:42:17 +00:00
|
|
|
if (count($fields) !== count($merge)) {
|
2014-12-05 18:23:48 +00:00
|
|
|
|
2014-12-09 11:56:34 +00:00
|
|
|
$mergeFields = array_diff_key(array_keys($mergeData), $fields);
|
2014-12-05 19:01:49 +00:00
|
|
|
|
2014-12-09 12:16:34 +00:00
|
|
|
$message = 'Fixture invalid: Count of fields does not match count of values in ' . get_class($this) . "\n";
|
2014-12-05 19:01:49 +00:00
|
|
|
foreach ($mergeFields as $field) {
|
2014-12-09 12:35:12 +00:00
|
|
|
$message .= "The field '" . $field . "' is in the data fixture but not in the schema." . "\n";
|
2014-12-05 19:01:49 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 03:17:02 +00:00
|
|
|
throw new CakeException($message);
|
2014-01-06 19:42:17 +00:00
|
|
|
}
|
|
|
|
$values[] = $merge;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-04-27 00:53:18 +00:00
|
|
|
$nested = $db->useNestedTransactions;
|
|
|
|
$db->useNestedTransactions = false;
|
2012-04-24 04:34:22 +00:00
|
|
|
$result = $db->insertMulti($this->table, $fields, $values);
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($this->primaryKey &&
|
2013-03-04 01:59:09 +00:00
|
|
|
isset($this->fields[$this->primaryKey]['type']) &&
|
|
|
|
in_array($this->fields[$this->primaryKey]['type'], array('integer', 'biginteger'))
|
|
|
|
) {
|
2012-09-18 18:11:51 +00:00
|
|
|
$db->resetSequence($this->table, $this->primaryKey);
|
|
|
|
}
|
2012-04-27 00:53:18 +00:00
|
|
|
$db->useNestedTransactions = $nested;
|
2012-04-24 04:34:22 +00:00
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-10-15 00:44:31 +00:00
|
|
|
* Truncates the current fixture. Can be overwritten by classes extending
|
2012-09-18 18:11:51 +00:00
|
|
|
* CakeFixture to trigger other events before / after truncate.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-09-18 18:11:51 +00:00
|
|
|
* @param DboSource $db A reference to a db instance
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-26 04:04:25 +00:00
|
|
|
public function truncate($db) {
|
2008-08-02 19:41:53 +00:00
|
|
|
$fullDebug = $db->fullDebug;
|
|
|
|
$db->fullDebug = false;
|
|
|
|
$return = $db->truncate($this->table);
|
|
|
|
$db->fullDebug = $fullDebug;
|
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|