1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: App::uses('CakeSchema', 'Model');
17:
18: 19: 20: 21: 22: 23:
24: class CakeTestFixture {
25:
26: 27: 28: 29: 30:
31: public $name = null;
32:
33: 34: 35: 36:
37: public $db = null;
38:
39: 40: 41: 42:
43: public $table = null;
44:
45: 46: 47: 48:
49: public function __construct() {
50: if ($this->name === null) {
51: if (preg_match('/^(.*)Fixture$/', get_class($this), $matches)) {
52: $this->name = $matches[1];
53: } else {
54: $this->name = get_class($this);
55: }
56: }
57: $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test'));
58: $this->init();
59: }
60:
61: 62: 63: 64:
65: public function init() {
66: if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
67: $import = array_merge(
68: array('connection' => 'default', 'records' => false),
69: is_array($this->import) ? $this->import : array('model' => $this->import)
70: );
71:
72: if (isset($import['model'])) {
73: list($plugin, $modelClass) = pluginSplit($import['model'], true);
74: App::uses($modelClass, $plugin . 'Model');
75: if (!class_exists($modelClass)) {
76: throw new MissingModelException(array('class' => $modelClass));
77: }
78: $model = new $modelClass(null, null, $import['connection']);
79: $db = $model->getDataSource();
80: if (empty($model->tablePrefix)) {
81: $model->tablePrefix = $db->config['prefix'];
82: }
83: $this->fields = $model->schema(true);
84: $this->fields[$model->primaryKey]['key'] = 'primary';
85: $this->table = $db->fullTableName($model, false);
86: ClassRegistry::config(array('ds' => 'test'));
87: ClassRegistry::flush();
88: } elseif (isset($import['table'])) {
89: $model = new Model(null, $import['table'], $import['connection']);
90: $db = ConnectionManager::getDataSource($import['connection']);
91: $db->cacheSources = false;
92: $model->useDbConfig = $import['connection'];
93: $model->name = Inflector::camelize(Inflector::singularize($import['table']));
94: $model->table = $import['table'];
95: $model->tablePrefix = $db->config['prefix'];
96: $this->fields = $model->schema(true);
97: ClassRegistry::flush();
98: }
99:
100: if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
101: $this->table = str_replace($db->config['prefix'], '', $this->table);
102: }
103:
104: if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
105: $this->records = array();
106: $query = array(
107: 'fields' => $db->fields($model, null, array_keys($this->fields)),
108: 'table' => $db->fullTableName($model),
109: 'alias' => $model->alias,
110: 'conditions' => array(),
111: 'order' => null,
112: 'limit' => null,
113: 'group' => null
114: );
115: $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
116:
117: if ($records !== false && !empty($records)) {
118: $this->records = Set::extract($records, '{n}.' . $model->alias);
119: }
120: }
121: }
122:
123: if (!isset($this->table)) {
124: $this->table = Inflector::underscore(Inflector::pluralize($this->name));
125: }
126:
127: if (!isset($this->primaryKey) && isset($this->fields['id'])) {
128: $this->primaryKey = 'id';
129: }
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function create($db) {
139: if (!isset($this->fields) || empty($this->fields)) {
140: return false;
141: }
142:
143: if (empty($this->fields['tableParameters']['engine'])) {
144: $canUseMemory = true;
145: foreach ($this->fields as $field => $args) {
146:
147: if (is_string($args)) {
148: $type = $args;
149: } elseif (!empty($args['type'])) {
150: $type = $args['type'];
151: } else {
152: continue;
153: }
154:
155: if (in_array($type, array('blob', 'text', 'binary'))) {
156: $canUseMemory = false;
157: break;
158: }
159: }
160:
161: if ($canUseMemory) {
162: $this->fields['tableParameters']['engine'] = 'MEMORY';
163: }
164: }
165: $this->Schema->build(array($this->table => $this->fields));
166: try {
167: $db->execute($db->createSchema($this->Schema), array('log' => false));
168: } catch (Exception $e) {
169: return false;
170: }
171: return true;
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function drop($db) {
181: if (empty($this->fields)) {
182: return false;
183: }
184: $this->Schema->build(array($this->table => $this->fields));
185: try {
186:
187: $db->execute($db->dropSchema($this->Schema), array('log' => false));
188: } catch (Exception $e) {
189: return false;
190: }
191: return true;
192: }
193:
194: 195: 196: 197: 198: 199: 200:
201: public function insert($db) {
202: if (!isset($this->_insert)) {
203: $values = array();
204: if (isset($this->records) && !empty($this->records)) {
205: $fields = array();
206: foreach ($this->records as $record) {
207: $fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
208: }
209: $fields = array_unique($fields);
210: $default = array_fill_keys($fields, null);
211: foreach ($this->records as $record) {
212: $fields = array_keys($record);
213: $values[] = array_values(array_merge($default, $record));
214: }
215: return $db->insertMulti($this->table, $fields, $values);
216: }
217: return true;
218: }
219: }
220:
221:
222: 223: 224: 225: 226: 227: 228:
229: public function truncate($db) {
230: $fullDebug = $db->fullDebug;
231: $db->fullDebug = false;
232: $return = $db->truncate($this->table);
233: $db->fullDebug = $fullDebug;
234: return $return;
235: }
236: }
237: