Skip to content
Permalink
Browse files

Updating fixture task. Refactored methods.

Multiple rows can now be generated.
  • Loading branch information...
markstory committed Apr 30, 2009
1 parent d705e89 commit 0b2232d05a1daf2630e8d30c2022c278053fa8b9
Showing with 174 additions and 15 deletions.
  1. +174 −15 cake/console/libs/tasks/fixture.php
@@ -57,6 +57,9 @@ class FixtureTask extends Shell {
*/
function initialize() {
$this->path = $this->params['working'] . DS . 'tests' . DS . 'fixtures' . DS;
if (!class_exists('CakeSchema')) {
App::import('Model', 'Schema');
}
}
/**
* Execution method always used for tasks
@@ -72,19 +75,7 @@ function execute() {
if (strtolower($this->args[0]) == 'all') {
return $this->all();
}
$controller = Inflector::camelize($this->args[0]);
$actions = null;
if (isset($this->args[1]) && $this->args[1] == 'scaffold') {
$this->out('Baking scaffold for ' . $controller);
$actions = $this->bakeActions($controller);
} else {
$actions = 'scaffold';
}
if ($this->bake($controller, $actions)) {
if ($this->_checkUnitTest()) {
$this->bakeTest($controller);
}
}
$model = Inflector::camelize($this->args[0]);
}
}
@@ -113,16 +104,184 @@ function __interactive($modelName = false) {
$this->hr();
$useDbConfig = $this->DbConfig->getConfig();
$modelName = $this->Model->getName($useDbConfig);
$useTable = $this->Model->getTable($modelName, $useDbConfig);
$importOptions = $this->importOptions($modelName);
$this->bake($modelName, $useTable, $importOptions);
}
/**
* Interacts with the User to setup an array of import options. For a fixture.
*
* @param string $modelName Name of model you are dealing with.
* @return array Array of import options.
**/
function importOptions($modelName) {
$options = array();
$doSchema = $this->in('Would you like to import schema for this fixture?', array('y', 'n'), 'n');
if ($doSchema == 'y') {
$options['schema'] = $modelName;
}
$doRecords = $this->in('Would you like to import records for this fixture?', array('y', 'n'), 'n');
if ($doRecords == 'y') {
$options['records'] = true;
}
return $options;
}
/**
* Assembles and writes a Fixture file
*
* @param string $model Name of model to bake.
* @param string $useTable Name of table to use.
* @param array $importOptions Options for var $import
* @return string Baked fixture
* @access private
*/
function bake() {
function bake($model, $useTable = false, $importOptions = array()) {
$out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
$out .= "\tvar \$name = '$model';\n";
if (!$useTable) {
$useTable = Inflector::tableize($model);
} elseif ($useTable != Inflector::tableize($model)) {
$out .= "\tvar \$table = '$useTable';\n";
}
$modelImport = $recordImport = null;
if (!empty($importOptions)) {
if (isset($importOptions['schema'])) {
$modelImport = "'model' => '{$importOptions['schema']}'";
}
if (isset($importOptions['records'])) {
$recordImport = ", 'records' => true";
}
$out .= sprintf("\tvar \$import = array(%s%s);\n", $modelImport, $recordImport);
}
$this->_Schema = new CakeSchema();
$data = $this->_Schema->read(array('models' => false));
if (!isset($data['tables'][$useTable])) {
$this->err('Could not find your selected table ' . $useTable);
return false;
}
$tableInfo = $data['tables'][$useTable];
if (is_null($modelImport)) {
$out .= $this->_generateSchema($tableInfo);
}
if (is_null($recordImport)) {
$recordCount = 1;
if (isset($this->params['count'])) {
$recordCount = $this->params['count'];
}
$out .= $this->_generateRecords($tableInfo, $recordCount);
}
$out .= "}\n";
$path = TESTS . DS . 'fixtures' . DS;
if (isset($this->plugin)) {
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
$path = APP . $pluginPath . 'tests' . DS . 'fixtures' . DS;
}
$filename = Inflector::underscore($model).'_fixture.php';
$header = '$Id';
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $model ." Fixture generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
$this->out("\nBaking test fixture for $model...");
$this->createFile($path . $filename, $content);
}
/**
* Generates a string representation of a schema.
*
* @param array $table Table schema array
* @return string fields definitions
**/
function _generateSchema($tableInfo) {
$cols = array();
$out = "\n\tvar \$fields = array(\n";
foreach ($tableInfo as $field => $fieldInfo) {
if (is_array($fieldInfo)) {
if ($field != 'indexes') {
$col = "\t\t'{$field}' => array('type'=>'" . $fieldInfo['type'] . "', ";
$col .= join(', ', $this->_Schema->__values($fieldInfo));
} else {
$col = "\t\t'indexes' => array(";
$props = array();
foreach ((array)$fieldInfo as $key => $index) {
$props[] = "'{$key}' => array(".join(', ', $this->_Schema->__values($index)).")";
}
$col .= join(', ', $props);
}
$col .= ")";
$cols[] = $col;
}
}
$out .= join(",\n", $cols);
$out .= "\n\t);\n";
return $out;
}
/**
* Generate String representation of Records
*
* @param array $table Table schema array
* @return string
**/
function _generateRecords($tableInfo, $recordCount = 1) {
$out = "\t\$records = array(\n";
for ($i = 0; $i < $recordCount; $i++) {
$records = array();
foreach ($tableInfo as $field => $fieldInfo) {
if (empty($fieldInfo['type'])) {
continue;
}
switch ($fieldInfo['type']) {
case 'integer':
$insert = $i + 1;
break;
case 'string';
$insert = "Lorem ipsum dolor sit amet";
if (!empty($fieldInfo['length'])) {
$insert = substr($insert, 0, (int)$value['length'] - 2);
}
$insert = "'$insert'";
break;
case 'datetime':
$ts = date('Y-m-d H:i:s');
$insert = "'$ts'";
break;
case 'date':
$ts = date('Y-m-d');
$insert = "'$ts'";
break;
case 'time':
$ts = date('H:i:s');
$insert = "'$ts'";
break;
case 'boolean':
$insert = 1;
break;
case 'text':
$insert = "'Lorem ipsum dolor sit amet, aliquet feugiat.";
$insert .= " Convallis morbi fringilla gravida,";
$insert .= " phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin";
$insert .= " venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla";
$insert .= " vestibulum massa neque ut et, id hendrerit sit,";
$insert .= " feugiat in taciti enim proin nibh, tempor dignissim, rhoncus";
$insert .= " duis vestibulum nunc mattis convallis.'";
break;
}
$records[] = "\t\t\t'$field' => $insert";
}
$out .= "\t\tarray(\n";
$out .= implode(",\n", $records);
$out .= "\n\t\t),\n";
}
$out .= "\t);\n";
return $out;
}
/**

0 comments on commit 0b2232d

Please sign in to comment.
You can’t perform that action at this time.