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