Skip to content

Commit

Permalink
Improving test generation.
Browse files Browse the repository at this point in the history
Class construction can be a bit more complicated now.
  • Loading branch information
markstory committed Jan 28, 2012
1 parent d904ab0 commit 9ff0739
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
45 changes: 39 additions & 6 deletions lib/Cake/Console/Command/Task/TestTask.php
Expand Up @@ -139,15 +139,17 @@ public function bake($type, $className) {
$methods = $this->getTestableMethods($fullClassName);
}
$mock = $this->hasMockClass($type, $fullClassName);
$construction = $this->generateConstructor($type, $fullClassName);
list($preConstruct, $construction, $postConstruct) = $this->generateConstructor($type, $fullClassName);
$uses = $this->generateUses($type, $realType, $fullClassName);

$this->out("\n" . __d('cake_console', 'Baking test case for %s %s ...', $className, $type), 1, Shell::QUIET);

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

Expand Down Expand Up @@ -434,18 +436,49 @@ public function hasMockClass($type) {
*
* @param string $type The Type of object you are generating tests for eg. controller
* @param string $fullClassName The Classname of the class the test is being generated for.
* @return string Constructor snippet for the thing you are building.
* @return array Constructor snippets for the thing you are building.
*/
public function generateConstructor($type, $fullClassName) {
$type = strtolower($type);
$pre = $post = '';
if ($type == 'model') {
return "ClassRegistry::init('$fullClassName');\n";
$construct = "ClassRegistry::init('$fullClassName');\n";
}
if ($type == 'controller') {
$className = substr($fullClassName, 0, strlen($fullClassName) - 10);
return "new Test$fullClassName();\n\t\t\$this->{$className}->constructClasses();\n";
$construct = "new Test$fullClassName();\n";
$post = "\$this->{$className}->constructClasses();\n";
}
return "new $fullClassName();\n";
if ($type == 'helper') {
$pre = "\$View = new View();\n";
$construct = "new {$fullClassName}(\$View);\n";
}
if ($type == 'component') {
$pre = "\$Collection = new ComponentCollection();\n";
$construct = "new {$fullClassName}(\$Collection);\n";
}
return array($pre, $construct, $post);
}

/**
* Generate the uses() calls for a type & classname
*
* @param string $type The Type of object you are generating tests for eg. controller
* @param string $className The Classname of the class the test is being generated for.
* @return array Constructor snippets for the thing you are building.
*/
public function generateUses($type, $realType, $className) {
$uses = array();
if ($type == 'component') {
$uses[] = array('ComponentCollection', 'Controller');
$uses[] = array('Component', 'Controller');
}
if ($type == 'helper') {
$uses[] = array('View', 'View');
$uses[] = array('Helper', 'View');
}
$uses[] = array($className, $realType);
return $uses;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/Cake/Console/Templates/default/classes/test.ctp
Expand Up @@ -19,7 +19,9 @@
*/
echo "<?php\n";
?>
App::uses('<?php echo $fullClassName; ?>', '<?php echo $realType; ?>');
<?php foreach ($uses as $dependency): ?>
App::uses('<?php echo $dependency[0]; ?>', '<?php echo $dependency[1]; ?>');
<?php endforeach; ?>

<?php if ($mock and strtolower($type) == 'controller'): ?>
/**
Expand Down Expand Up @@ -69,8 +71,9 @@ class <?php echo $fullClassName; ?>TestCase extends CakeTestCase {
*/
public function setUp() {
parent::setUp();

<?php echo $preConstruct; ?>
$this-><?php echo $className . ' = ' . $construction; ?>
<?php echo $postConstruct; ?>
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/Case/Console/Command/Task/TestTaskTest.php
Expand Up @@ -490,15 +490,15 @@ public function testBakeControllerTest() {
*/
public function testGenerateConstructor() {
$result = $this->Task->generateConstructor('controller', 'PostsController');
$expected = "new TestPostsController();\n\t\t\$this->Posts->constructClasses();\n";
$expected = array('', "new TestPostsController();\n", "\$this->Posts->constructClasses();\n");
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('model', 'Post');
$expected = "ClassRegistry::init('Post');\n";
$expected = array('', "ClassRegistry::init('Post');\n", '');
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('helper', 'FormHelper');
$expected = "new FormHelper();\n";
$expected = array("\$View = new View();\n", "new FormHelper(\$View);\n", '');
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit 9ff0739

Please sign in to comment.