markstory/cakephp — pull request #5 by zuha (2012-05-09 00:12:50+00:00)
Title: Added a Fixture Import Type

Sorry if this pull request isn't formatted the right way. I haven't done many pull requests previously.

Summary : The current fixture import types are "model", and "table" and none (ie. in the fixture). I added a new one called "config". (Wanted to call it schema, but that caused some weird name conflict)

Details : The current import types require that you maintain a test database, or a fixture file. This seemed redundant to me because I have all of my schema's in Config/Schema/schema.php. So why not keep the schema in a single file, and allow that to maintain the fixture. This will keep the benefit of the "none" type where you don't need to maintain a test database, and add the benefit of only needing to have a single file for database structure that gets used in both the fixtures and when doing updates and migrations.

This only required about 20 lines of code to be added to the CakeTestFixture.php file all contained within a new elseif around line 125.

```ruby
} elseif (isset($import['config'])) {
list($plugin, $modelClass) = pluginSplit($import['config'], false);
$fileName = !empty($import['file']) ? $import['file'] : 'schema';
$filePath = 'Config' . DS. 'Schema' . DS . $fileName . '.php';
$file = !empty($plugin) ? App::pluginPath($plugin) . $filePath : ROOT . DS . APP_DIR . DS . $filePath;
$className = !empty($plugin) ? $plugin . 'Schema' : 'AppSchema';
$className = !empty($import['class']) ? $import['class'] : $className;
$this->table = !empty($import['table']) ? $import['table'] : Inflector::tableize($modelClass);
if (!file_exists($file)) {
throw new Exception(__('Schema file %s missing', $file));
}
include($file);
$Schema = new $className;
$model = new Model(null, $this->table, $import['connection']);
$db = ConnectionManager::getDataSource($import['connection']);
$db->cacheSources = false;
$model->useDbConfig = $import['connection'];
$model->name = Inflector::camelize(Inflector::singularize($this->table));
$model->table = $this->table;
$model->tablePrefix = $db->config['prefix'];
$this->fields = $Schema->tables[$this->table];
ClassRegistry::config(array('ds' => 'test'));
ClassRegistry::flush();
}
```

A sample import variable in your Fixture might look like this...

```ruby
public $import = array('config' => 'MyPlugin.MyModel', 'file' => 'my_plugin', 'class' => 'MyModelSchema', 'table' => 'custom_table');
```