Skip to content
Permalink
Browse files

Adding test cases for bake()

Adding support for -connection param
Adding partial support for -plugin param
Separating file writing and code generation functions.
Updating help()
  • Loading branch information...
markstory committed May 4, 2009
1 parent 6180ca8 commit e21cc3db72bd7c63e9bb87cb0d6fdd7c24637592
Showing with 96 additions and 18 deletions.
  1. +47 −17 cake/console/libs/tasks/fixture.php
  2. +49 −1 cake/tests/cases/console/libs/tasks/fixture.test.php
@@ -50,6 +50,12 @@ class FixtureTask extends Shell {
* @access public
*/
var $path = null;
/**
* The db connection being used for baking
*
* @var string
**/
var $connection = null;
/**
* Override initialize
*
@@ -63,6 +69,7 @@ function initialize() {
}
/**
* Execution method always used for tasks
* Handles dispatching to interactive, named, or all processess.
*
* @access public
*/
@@ -87,12 +94,11 @@ function execute() {
* @return void
**/
function all() {
$ds = 'default';
if (isset($this->params['connection'])) {
$ds = $this->params['connection'];
if (!isset($this->connection)) {
$this->connection = 'default';
}
$this->interactive = false;
$tables = $this->Model->listAll($ds, false);
$tables = $this->Model->listAll($this->connection, false);
foreach ($tables as $table) {
$model = $this->_modelName($table);
$this->bake($model);
@@ -110,9 +116,12 @@ function __interactive($modelName = false) {
$this->out(sprintf("Bake Fixture\nPath: %s", $this->path));
$this->hr();
$useDbConfig = $this->DbConfig->getConfig();
$modelName = $this->Model->getName($useDbConfig);
$useTable = $this->Model->getTable($modelName, $useDbConfig);
$useDbConfig = $this->connection;
if (!isset($this->connection)) {
$this->connection = $this->DbConfig->getConfig();
}
$modelName = $this->Model->getName($this->connection);
$useTable = $this->Model->getTable($modelName, $this->connection);
$importOptions = $this->importOptions($modelName);
$this->bake($modelName, $useTable, $importOptions);
}
@@ -145,7 +154,7 @@ function importOptions($modelName) {
* @access private
*/
function bake($model, $useTable = false, $importOptions = array()) {
$out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
$out = "class {$model}Fixture extends CakeTestFixture {\n";
$out .= "\tvar \$name = '$model';\n";
if (!$useTable) {
@@ -160,13 +169,16 @@ function bake($model, $useTable = false, $importOptions = array()) {
$modelImport = "'model' => '{$importOptions['schema']}'";
}
if (isset($importOptions['records'])) {
$recordImport = ", 'records' => true";
$recordImport = "'records' => true";
}
if ($modelImport && $recordImport) {
$modelImport .= ', ';
}
$out .= sprintf("\tvar \$import = array(%s%s);\n", $modelImport, $recordImport);
}
$this->_Schema = new CakeSchema();
$data = $this->_Schema->read(array('models' => false));
$data = $this->_Schema->read(array('models' => false, 'connection' => $this->connection));
if (!isset($data['tables'][$useTable])) {
$this->err('Could not find your selected table ' . $useTable);
@@ -186,15 +198,29 @@ function bake($model, $useTable = false, $importOptions = array()) {
$out .= $this->_generateRecords($tableInfo, $recordCount);
}
$out .= "}\n";
$this->generateFixtureFile($model, $out);
return $out;
}
$path = TESTS . DS . 'fixtures' . DS;
/**
* Generate the fixture file, and write to disk
*
* @param string $model name of the model being generated
* @param string $fixture Contents of the fixture file.
* @access public
* @return void
**/
function generateFixtureFile($model, $fixture) {
//@todo fix plugin pathing.
$path = $this->path;
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}?>";
$filename = Inflector::underscore($model) . '_fixture.php';
$content = "<?php\n/* " . $model . " Fixture generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n";
$content .= $fixture;
$content .= "?>";
$this->out("\nBaking test fixture for $model...");
$this->createFile($path . $filename, $content);
}
@@ -226,7 +252,7 @@ function _generateSchema($tableInfo) {
}
}
$out .= join(",\n", $cols);
$out .= "\n\t);\n";
$out .= "\n\t);\n\n";
return $out;
}
@@ -237,7 +263,7 @@ function _generateSchema($tableInfo) {
* @return string
**/
function _generateRecords($tableInfo, $recordCount = 1) {
$out = "\t\$records = array(\n";
$out = "\tvar \$records = array(\n";
for ($i = 0; $i < $recordCount; $i++) {
$records = array();
@@ -306,9 +332,13 @@ function help() {
$this->hr();
$this->out('Commands:');
$this->out("\nfixture <name>\n\tbakes fixture with specified name.");
$this->out("\nfixture -count <n>\n\tbakes fixture with <n> records.");
$this->out("\nfixture all\n\tbakes all fixtures.");
$this->out("");
$this->out('Parameters:');
$this->out("\t-count The number of records to include in the fixture(s).");
$this->out("\t-connection Which database configuration to use for baking.");
$this->out("\t-plugin lowercased_underscored name of plugin to bake fixtures for.");
$this->out("");
$this->_stop();
}
}
@@ -108,14 +108,62 @@ function testImportOptions() {
$result = $this->Task->importOptions('Article');
$expected = array('schema' => 'Article', 'records' => true);
$this->assertEqual($result, $expected);
$this->Task->setReturnValueAt(2, 'in', 'n');
$this->Task->setReturnValueAt(3, 'in', 'n');
$result = $this->Task->importOptions('Article');
$expected = array();
$this->assertEqual($result, $expected);
}
/**
* Test that bake works
*
* @return void
**/
function testBake() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$result = $this->Task->bake('Article');
$this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
$this->assertPattern('/var \$fields/', $result);
$this->assertPattern('/var \$records/', $result);
$this->assertNoPattern('/var \$import/', $result);
$result = $this->Task->bake('Article', 'comments');
$this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
$this->assertPattern('/var \$name \= \'Article\';/', $result);
$this->assertPattern('/var \$table \= \'comments\';/', $result);
$result = $this->Task->bake('Article', 'comments', array('records' => true));
$this->assertPattern("/var \\\$import \= array\('records' \=\> true\);/", $result);
$this->assertNoPattern('/var \$records/', $result);
$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
$this->assertPattern("/var \\\$import \= array\('model' \=\> 'Article'\);/", $result);
$this->assertNoPattern('/var \$fields/', $result);
$result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
$this->assertPattern("/var \\\$import \= array\('model' \=\> 'Article'\, 'records' \=\> true\);/", $result);
$this->assertNoPattern('/var \$fields/', $result);
$this->assertNoPattern('/var \$records/', $result);
}
/**
* Test that file generation includes headers and correct path for plugins.
*
* @return void
**/
function testGenerateFixtureFile() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/my fixture/')));
$result = $this->Task->generateFixtureFile('Article', 'my fixture');
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/\<\?php(.*)\?\>/ms')));
$result = $this->Task->generateFixtureFile('Article', 'my fixture');
}
}
?>

0 comments on commit e21cc3d

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