Permalink
Browse files

Improving test generation.

Class construction can be a bit more complicated now.
  • Loading branch information...
1 parent d904ab0 commit 9ff0739504da434a87add14905179f66d17dcab6 @markstory markstory committed Jan 27, 2012
@@ -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');
@@ -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;
}
/**
@@ -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'): ?>
/**
@@ -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; ?>
}
/**
@@ -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);
}

0 comments on commit 9ff0739

Please sign in to comment.