Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: cake_test_fixture.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Short description for file.
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 *  Licensed under The Open Group Test Suite License
14
 *  Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
19
 * @package       cake
20
 * @subpackage    cake.cake.tests.libs
21
 * @since         CakePHP(tm) v 1.2.0.4667
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
26
 */
27
/**
28
 * Short description for class.
29
 *
30
 * @package       cake
31
 * @subpackage    cake.cake.tests.lib
32
 */
33
class CakeTestFixture extends Object {
34
/**
35
 * Cake's DBO driver (e.g: DboMysql).
36
 *
37
 * @access public
38
 */
39
	var $db = null;
40
/**
41
 * Full Table Name
42
 *
43
 * @access public
44
 */
45
	var $table = null;
46
 
47
/**
48
 * Instantiate the fixture.
49
 *
50
 * @access public
51
 */
52
	function __construct() {
53
		App::import('Model', 'Schema');
54
		$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test_suite'));
55
 
56
		$this->init();
57
	}
58
/**
59
 * Initialize the fixture.
60
 *
61
 * @param object	Cake's DBO driver (e.g: DboMysql).
62
 * @access public
63
 *
64
 */
65
	function init() {
66
		if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
67
			$import = array_merge(array('connection' => 'default', 'records' => false), is_array($this->import) ? $this->import : array('model' => $this->import));
68
 
69
			if (isset($import['model']) && App::import('Model', $import['model'])) {
70
				ClassRegistry::config(array('ds' => $import['connection']));
71
				$model =& ClassRegistry::init($import['model']);
72
				$db =& ConnectionManager::getDataSource($model->useDbConfig);
73
				$db->cacheSources = false;
74
				$this->fields = $model->schema(true);
75
				$this->fields[$model->primaryKey]['key'] = 'primary';
76
				ClassRegistry::config(array('ds' => 'test_suite'));
77
				ClassRegistry::flush();
78
			} elseif (isset($import['table'])) {
79
				$model =& new Model(null, $import['table'], $import['connection']);
80
				$db =& ConnectionManager::getDataSource($import['connection']);
81
				$db->cacheSources = false;
82
				$model->useDbConfig = $import['connection'];
83
				$model->name = Inflector::camelize(Inflector::singularize($import['table']));
84
				$model->table = $import['table'];
85
				$model->tablePrefix = $db->config['prefix'];
86
				$this->fields = $model->schema(true);
87
			}
88
 
89
			if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
90
				$this->records = array();
91
				$query = array(
92
					'fields' => array_keys($this->fields),
93
					'table' => $db->fullTableName($model->table),
94
					'alias' => $model->alias,
95
					'conditions' => array(),
96
					'order' => null,
97
					'limit' => null,
98
					'group' => null
99
				);
100
 
101
				foreach ($query['fields'] as $index => $field) {
102
					$query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field);
103
				}
104
				$records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
105
 
106
				if ($records !== false && !empty($records)) {
107
					$this->records = Set::extract($records, '{n}.' . $model->alias);
108
				}
109
			}
110
		}
111
 
112
		if (!isset($this->table)) {
113
			$this->table = Inflector::underscore(Inflector::pluralize($this->name));
114
		}
115
 
116
		if (!isset($this->primaryKey) && isset($this->fields['id'])) {
117
			$this->primaryKey = 'id';
118
		}
119
	}
120
/**
121
 * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
122
 *
123
 * @param object	$db	An instance of the database object used to create the fixture table
124
 * @return boolean True on success, false on failure
125
 * @access public
126
 */
127
	function create(&$db) {
128
		if (!isset($this->fields) || empty($this->fields)) {
129
			return false;
130
		}
131
 
132
		$this->Schema->_build(array($this->table => $this->fields));
133
		return (
134
			$db->execute($db->createSchema($this->Schema), array('log' => false)) !== false
135
		);
136
	}
137
/**
138
 * Run after all tests executed, should return SQL statement to drop table for this fixture.
139
 *
140
 * @param object	$db	An instance of the database object used to create the fixture table
141
 * @return boolean True on success, false on failure
142
 * @access public
143
 */
144
	function drop(&$db) {
145
		$this->Schema->_build(array($this->table => $this->fields));
146
		return (
147
			$db->execute($db->dropSchema($this->Schema), array('log' => false)) !== false
148
		);
149
	}
150
/**
151
 * Run before each tests is executed, should return a set of SQL statements to insert records for the table
152
 * of this fixture could be executed successfully.
153
 *
154
 * @param object $db An instance of the database into which the records will be inserted
155
 * @return boolean on success or if there are no records to insert, or false on failure
156
 * @access public
157
 */
158
	function insert(&$db) {
159
		if (!isset($this->_insert)) {
160
			$values = array();
161
 
162
			if (isset($this->records) && !empty($this->records)) {
163
				foreach ($this->records as $record) {
164
					$fields = array_keys($record);
165
					$values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')';
166
				}
167
				return $db->insertMulti($this->table, $fields, $values);
168
			}
169
			return true;
170
		}
171
	}
172
/**
173
 * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
174
 * truncate.
175
 *
176
 * @param object $db A reference to a db instance
177
 * @return boolean
178
 * @access public
179
 */
180
	function truncate(&$db) {
181
		$fullDebug = $db->fullDebug;
182
		$db->fullDebug = false;
183
		$return = $db->truncate($this->table);
184
		$db->fullDebug = $fullDebug;
185
		return $return;
186
	}
187
}
188
?>