File: cake_test_fixture.php

package info (click to toggle)
cakephp 1.2.0.7296-rc2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,032 kB
  • ctags: 23,481
  • sloc: php: 77,476; sql: 92; makefile: 34; sh: 5
file content (189 lines) | stat: -rw-r--r-- 6,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/* SVN FILE: $Id: cake_test_fixture.php 7296 2008-06-27 09:09:03Z gwoo $ */
/**
 * 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-2008, 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-2008, 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: 7296 $
 * @modifiedby		$LastChangedBy: gwoo $
 * @lastmodified	$Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 2008) $
 * @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
 */
class CakeTestFixture extends Object {
/**
 * Cake's DBO driver (e.g: DboMysql).
 *
 * @access public
 */
	var $db = null;
/**
 * Full Table Name
 *
 * @access public
 */
	var $table = null;

/**
 * Instantiate the fixture.
 *
 * @param object	Cake's DBO driver (e.g: DboMysql).
 *
 * @access public
 */
	function __construct(&$db) {
		App::import('Model', 'Schema');
		$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test_suite'));

		$this->init();
	}
/**
 * Initialize the fixture.
 *
 * @param object	Cake's DBO driver (e.g: DboMysql).
 * @access public
 *
 */
	function init() {
		if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
			$import = array();

			if (is_string($this->import) || is_array($this->import) && isset($this->import['model'])) {
				$import = array_merge(array('records' => false), ife(is_array($this->import), $this->import, array()));
				$import['model'] = ife(is_array($this->import), $this->import['model'], $this->import);
			} elseif (isset($this->import['table'])) {
				$import = array_merge(array('connection' => 'default', 'records' => false), $this->import);
			}

			if (isset($import['model']) && (class_exists($import['model']) || App::import('Model', $import['model']))) {
				$model =& new $import['model'];

				$db =& ConnectionManager::getDataSource($model->useDbConfig);
				$db->cacheSources = false;
				$this->fields = $model->schema(true);
				$this->fields[$model->primaryKey]['key'] = 'primary';
			} elseif (isset($import['table'])) {
				$model =& new Model(null, $import['table'], $import['connection']);
				$db =& ConnectionManager::getDataSource($import['connection']);
				$db->cacheSources = false;
				$model->name = Inflector::camelize(Inflector::singularize($import['table']));
				$model->table = $import['table'];
				$model->tablePrefix = $db->config['prefix'];
				$this->fields = $model->schema(true);
			}

			if ($import['records'] !== false && isset($model) && isset($db)) {
				$this->records = array();

				$query = array(
					'fields' => array_keys($this->fields),
					'table' => $db->name($model->table),
					'alias' => $model->alias,
					'conditions' => array(),
					'order' => null,
					'limit' => null,
					'group' => null
				);

				foreach ($query['fields'] as $index => $field) {
					$query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field);
				}
				$records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);

				if ($records !== false && !empty($records)) {
					$this->records = Set::extract($records, '{n}.' . $model->alias);
				}
			}
		}

		if (!isset($this->table)) {
			$this->table = Inflector::underscore(Inflector::pluralize($this->name));
		}
				
		if (!isset($this->primaryKey) && isset($this->fields['id'])) {
			$this->primaryKey = 'id';
		}
	}
/**
 * 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
 * @access public
 */
	function create(&$db) {
		if (!isset($this->fields) || empty($this->fields)) {
			return false;
		}

		$this->Schema->_build(array($this->table => $this->fields));
		return ($db->execute($db->createSchema($this->Schema)) !== false);
	}
/**
 * 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
 * @access public
 */
	function drop(&$db) {
		$this->Schema->_build(array($this->table => $this->fields));
		return ($db->execute($db->dropSchema($this->Schema)) !== false);
	}
/**
 * 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
 * @access public
 */
	function insert(&$db) {
		if (!isset($this->_insert)) {
			$values = array();

			if (isset($this->records) && !empty($this->records)) {
				foreach ($this->records as $record) {
					$fields = array_keys($record);
					$values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')';
				}
				return $db->insertMulti($this->table, $fields, $values);
			}
			return true;
		}
	}
/**
 * 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 void
 * @access public
 */
	function truncate(&$db) {
		return $db->truncate($this->table);
	}
}
?>