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