Browse Source

Improving test generation.

Class construction can be a bit more complicated now.
mark_story 14 years ago
parent
commit
9ff0739504

+ 39 - 6
lib/Cake/Console/Command/Task/TestTask.php

@@ -139,7 +139,8 @@ class TestTask extends BakeTask {
 			$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);
 
@@ -147,7 +148,8 @@ class TestTask extends BakeTask {
 		$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 @@ class TestTask extends BakeTask {
  *
  * @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;
 	}
 
 /**

+ 5 - 2
lib/Cake/Console/Templates/default/classes/test.ctp

@@ -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; ?>
 	}
 
 /**

+ 3 - 3
lib/Cake/Test/Case/Console/Command/Task/TestTaskTest.php

@@ -490,15 +490,15 @@ class TestTaskTest extends CakeTestCase {
  */
 	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);
 	}