Skip to content

Commit

Permalink
Fixing plugin imports
Browse files Browse the repository at this point in the history
Adding contstructor generation.
and test case for controller construction.
  • Loading branch information
markstory committed May 25, 2009
1 parent 587b0ca commit 975aab8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 57 deletions.
91 changes: 36 additions & 55 deletions cake/console/libs/tasks/test.php
Expand Up @@ -143,10 +143,17 @@ function bake($type, $className) {
if (class_exists($fullClassName)) {
$methods = $this->getTestableMethods($fullClassName);
}
$mock = $this->generateMockClass($type, $fullClassName);
$construction = $this->generateConstructor($type, $fullClassName);

$plugin = null;
if ($this->plugin) {
$plugin = $this->plugin . '.';
}

$this->Template->set('fixtures', $this->_fixtures);
$this->Template->set('plugin', $this->plugin);
$this->Template->set(compact('className', 'methods', 'type', 'fullClassName'));
$this->Template->set('plugin', $plugin);
$this->Template->set(compact('className', 'methods', 'type', 'fullClassName', 'mock', 'construction'));
$out = $this->Template->generate('objects', 'test');

if (strpos($this->path, $type) === false) {
Expand All @@ -157,58 +164,6 @@ function bake($type, $className) {
return $out;
}
return false;

/*
if (!$name) {
return false;
}
if (!is_array($cases)) {
$cases = array($cases);
}
if (strpos($this->path, $class) === false) {
$this->filePath = $this->path . 'cases' . DS . Inflector::tableize($class) . DS;
}
$class = Inflector::classify($class);
$name = Inflector::classify($name);
$import = $name;
if (isset($this->plugin)) {
$import = $this->plugin . '.' . $name;
}
$extras = $this->__extras($class);
$out = "App::import('$class', '$import');\n";
if ($class == 'Model') {
$class = null;
}
$out .= "class Test{$name} extends {$name}{$class} {\n";
$out .= "{$extras}";
$out .= "}\n\n";
$out .= "class {$name}{$class}Test extends CakeTestCase {\n";
$out .= "\n\tfunction startTest() {";
$out .= "\n\t\t\$this->{$name} = new Test{$name}();";
$out .= "\n\t}\n";
$out .= "\n\tfunction test{$name}Instance() {\n";
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n";
foreach ($cases as $case) {
$case = Inflector::classify($case);
$out .= "\n\tfunction test{$case}() {\n\n\t}\n";
}
$out .= "}\n";
$this->out("Baking unit test for $name...");
$this->out($out);
$ok = $this->in(__('Is this correct?', true), array('y', 'n'), 'y');
if ($ok == 'n') {
return false;
}
$header = '$Id';
$content = "<?php \n/* SVN FILE: $header$ * /\n/* ". $name ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "* /\n{$out}?>";
return $this->createFile($this->filePath . Inflector::underscore($name) . '.test.php', $content);
*/
}

/**
Expand Down Expand Up @@ -277,7 +232,7 @@ function isLoadableClass($type, $class) {

/**
* Construct an instance of the class to be tested.
* So that fixtures and methods can be detected
* So that fixtures can be detected
*
* @return object
**/
Expand Down Expand Up @@ -418,6 +373,31 @@ function getUserFixtures() {
return $fixtures;
}

/**
* Generate a stub class or mock as needed.
*
* @return void
**/
function generateMockClass($type, $class) {
return '';
}

/**
* Generate a constructor code snippet for the type and classname
*
* @return string Constructor snippet for the thing you are building.
**/
function generateConstructor($type, $fullClassName) {
$type = strtolower($type);
if ($type == 'model') {
return "ClassRegistry::init('$fullClassName');";
}
if ($type == 'controller') {
return "new Test$fullClassName();\n\t\t\$this->{$fullClassName}->constructClasses();";
}
return "new $fullClassName";
}

/**
* Handles the extra stuff needed
*
Expand All @@ -433,6 +413,7 @@ function __extras($class) {
}
return $extras;
}

/**
* Create a test for a Model object.
*
Expand Down
16 changes: 14 additions & 2 deletions cake/console/libs/templates/objects/test.ctp
Expand Up @@ -19,14 +19,26 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
echo "<?php\n";
echo "/* ". $className ." Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "* /"
?>
App::import('<?php echo $type; ?>', '<?php echo $className;?>');
App::import('<?php echo $type; ?>', '<?php echo $plugin . $className;?>');

class <?php echo $className; ?>TestCase extends CakeTestCase {
<?php echo $mock; ?>

class <?php echo $fullClassName; ?>TestCase extends CakeTestCase {
<?php if (!empty($fixtures)): ?>
var $fixtures = array('<?php echo join("', '", $fixtures); ?>');

<?php endif; ?>
function startTest() {
$this-><?php echo $className . ' =& ' . $construction; ?>
}

function endTest() {
unset($this-><?php echo $className;?>);
ClassRegistry::flush();
}

<?php foreach ($methods as $method): ?>
function test<?php echo Inflector::classify($method); ?>() {

Expand Down
18 changes: 18 additions & 0 deletions cake/tests/cases/console/libs/tasks/test.test.php
Expand Up @@ -289,6 +289,13 @@ function testBake() {

$this->assertPattern('/App::import\(\'Model\', \'TestTaskArticle\'\)/', $result);
$this->assertPattern('/class TestTaskArticleTestCase extends CakeTestCase/', $result);

$this->assertPattern('/function startTest\(\)/', $result);
$this->assertPattern("/\\\$this->TestTaskArticle \=\& ClassRegistry::init\('TestTaskArticle'\)/", $result);

$this->assertPattern('/function endTest\(\)/', $result);
$this->assertPattern('/unset\(\$this->TestTaskArticle\)/', $result);

$this->assertPattern('/function testDoSomething\(\)/', $result);
$this->assertPattern('/function testDoSomethingElse\(\)/', $result);

Expand All @@ -297,5 +304,16 @@ function testBake() {
$this->assertPattern("/'app\.test_task_tag'/", $result);
$this->assertPattern("/'app\.articles_tag'/", $result);
}

/**
* test Constructor generation ensure that constructClasses is called for controllers
*
* @return void
**/
function testGenerateContsructor() {
$result = $this->Task->generateConstructor('controller', 'PostsController');
$expected = "new TestPostsController();\n\t\t\$this->PostsController->constructClasses();";
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 975aab8

Please sign in to comment.