2007-03-23 21:59:08 +00:00
|
|
|
<?php
|
2007-03-23 22:11:06 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4667
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.tests.lib
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
class CakeTestFixture extends Object {
|
|
|
|
var $db = null;
|
2007-03-23 22:11:06 +00:00
|
|
|
/**
|
|
|
|
* Instantiate the fixture.
|
|
|
|
*
|
|
|
|
* @param object Cake's DBO driver (e.g: DboMysql).
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function __construct(&$db) {
|
|
|
|
$this->db =& $db;
|
2007-04-17 21:20:38 +00:00
|
|
|
$this->init();
|
2007-08-21 21:46:59 +00:00
|
|
|
if(!class_exists('cakeschema')) {
|
|
|
|
uses('model' . DS .'schema');
|
|
|
|
}
|
|
|
|
$this->Schema = new CakeSchema(array('name'=>'TestSuite', 'connection'=>'test_suite'));
|
2007-04-17 21:20:38 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Initialize the fixture.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function init() {
|
2007-08-21 21:46:59 +00:00
|
|
|
|
2007-04-13 05:17:48 +00:00
|
|
|
if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
|
|
|
|
$import = array();
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-04-13 05:17:48 +00:00
|
|
|
if (is_string($this->import) || is_array($this->import) && isset($this->import['model'])) {
|
|
|
|
$import = am(array('records' => false), ife(is_array($this->import), $this->import, array()));
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-04-13 05:17:48 +00:00
|
|
|
$import['model'] = ife(is_array($this->import), $this->import['model'], $this->import);
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (isset($this->import['table'])) {
|
2007-04-13 05:17:48 +00:00
|
|
|
$import = am(array('connection' => 'default', 'records' => false), $this->import);
|
|
|
|
}
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-04-13 05:17:48 +00:00
|
|
|
if (isset($import['model']) && (class_exists($import['model']) || loadModel($import['model']))) {
|
|
|
|
$model =& new $import['model'];
|
2007-08-21 21:46:59 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($model->useDbConfig);
|
|
|
|
$db->cacheSources = false;
|
|
|
|
$this->table = $this->useTable;
|
|
|
|
$schema = $model->schema(true);
|
|
|
|
$this->fields = $schema->value;
|
2007-04-13 05:17:48 +00:00
|
|
|
$this->fields[$model->primaryKey]['key'] = 'primary';
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (isset($import['table'])) {
|
2007-08-21 21:46:59 +00:00
|
|
|
$model =& new Model(null, $import['table'], $import['connection']);
|
|
|
|
$db =& ConnectionManager::getDataSource($import['connection']);
|
|
|
|
$db->cacheSources = false;
|
2007-04-13 05:17:48 +00:00
|
|
|
$model->name = Inflector::camelize(Inflector::singularize($import['table']));
|
|
|
|
$model->table = $import['table'];
|
2007-08-21 21:46:59 +00:00
|
|
|
$model->tablePrefix = $db->config['prefix'];
|
|
|
|
$schema = $model->schema(true);
|
|
|
|
$this->fields = $schema->value;
|
2007-04-13 05:17:48 +00:00
|
|
|
}
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-08-21 21:46:59 +00:00
|
|
|
if ($import['records'] !== false && isset($model) && isset($db)) {
|
2007-04-13 05:17:48 +00:00
|
|
|
$this->records = array();
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-04-13 05:17:48 +00:00
|
|
|
$query = array(
|
2007-08-21 21:46:59 +00:00
|
|
|
'fields' => array_keys($this->fields),
|
|
|
|
'table' => $db->name($model->table),
|
2007-10-26 08:40:10 +00:00
|
|
|
'alias' => $model->currentModel,
|
2007-04-13 05:17:48 +00:00
|
|
|
'conditions' => array(),
|
|
|
|
'order' => null,
|
|
|
|
'limit' => null
|
|
|
|
);
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($query['fields'] as $index => $field) {
|
2007-08-21 21:46:59 +00:00
|
|
|
$query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field);
|
2007-04-13 05:17:48 +00:00
|
|
|
}
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-10-26 08:40:10 +00:00
|
|
|
$records = $db->fetchAll($db->buildStatement($query, $model), false, $model->currentModel);
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-04-13 05:17:48 +00:00
|
|
|
if ($records !== false && !empty($records)) {
|
2007-10-26 08:40:10 +00:00
|
|
|
$this->records = Set::extract($records, '{n}.' . $model->currentModel);
|
2007-04-13 05:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (!isset($this->table)) {
|
|
|
|
$this->table = Inflector::underscore(Inflector::pluralize($this->name));
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (!isset($this->primaryKey) && isset($this->fields['id'])) {
|
|
|
|
$this->primaryKey = 'id';
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-04-17 21:20:38 +00:00
|
|
|
if (isset($this->fields)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->fields as $index => $field) {
|
2007-04-17 21:20:38 +00:00
|
|
|
if (empty($field['default'])) {
|
|
|
|
unset($this->fields[$index]['default']);
|
|
|
|
}
|
2007-04-13 05:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
/**
|
|
|
|
* Run before all tests execute, should return SQL statement to create table for this fixture.
|
|
|
|
*
|
|
|
|
* @return string SQL CREATE TABLE statement, false if not applicable.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function create() {
|
|
|
|
if (!isset($this->_create)) {
|
|
|
|
if (!isset($this->fields) || empty($this->fields)) {
|
|
|
|
return null;
|
|
|
|
}
|
2007-08-21 21:46:59 +00:00
|
|
|
$this->Schema->_build(array($this->table => $this->fields));
|
|
|
|
$this->_create = $this->db->createSchema($this->Schema);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
return $this->_create;
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
/**
|
|
|
|
* Run after all tests executed, should return SQL statement to drop table for this fixture.
|
|
|
|
*
|
|
|
|
* @return string SQL DROP TABLE statement, false if not applicable.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function drop() {
|
|
|
|
if (!isset($this->_drop)) {
|
2007-08-21 21:46:59 +00:00
|
|
|
$this->Schema->_build(array($this->table => $this->fields));
|
|
|
|
$this->_drop = $this->db->dropSchema($this->Schema);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
return $this->_drop;
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
/**
|
|
|
|
* Run after each tests is executed, should return SQL statement to empty of records the table for this fixture.
|
|
|
|
*
|
|
|
|
* @return string SQL TRUNCATE TABLE statement, false if not applicable.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function truncate() {
|
|
|
|
if (!isset($this->_truncate)) {
|
2007-06-20 03:20:59 +00:00
|
|
|
$this->_truncate = 'TRUNCATE TABLE ' . $this->db->name($this->db->config['prefix'] . $this->table);
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
return $this->_truncate;
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
/**
|
|
|
|
* Run before each tests is executed, should return a set of SQL statements to insert records for the table of this fixture.
|
|
|
|
*
|
|
|
|
* @return array SQL INSERT statements, empty array if not applicable.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-03-23 21:59:08 +00:00
|
|
|
function insert() {
|
|
|
|
if (!isset($this->_insert)) {
|
|
|
|
$inserts = array();
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
if (isset($this->records) && !empty($this->records)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->records as $record) {
|
2007-03-23 21:59:08 +00:00
|
|
|
$fields = array_keys($record);
|
|
|
|
$values = array_values($record);
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
$insert = 'INSERT INTO ' . $this->db->name($this->db->config['prefix'] . $this->table) . '(';
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($fields as $field) {
|
2007-03-23 22:11:06 +00:00
|
|
|
$insert .= $this->db->name($field) . ',';
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
$insert = substr($insert, 0, -1);
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
$insert .= ') VALUES (';
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($values as $values) {
|
2007-03-23 22:11:06 +00:00
|
|
|
$insert .= $this->db->value($values) . ',';
|
2007-03-23 21:59:08 +00:00
|
|
|
}
|
|
|
|
$insert = substr($insert, 0, -1);
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
$insert .= ')';
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
$inserts[] = $insert;
|
|
|
|
}
|
|
|
|
}
|
2007-03-23 22:11:06 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
$this->_insert = $inserts;
|
|
|
|
}
|
2007-06-20 03:20:59 +00:00
|
|
|
|
2007-03-23 21:59:08 +00:00
|
|
|
return $this->_insert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|