Browse Source

Refactoring TestTask so it generates tests according to the new file naming standard

Jose Lorenzo Rodriguez 15 years ago
parent
commit
d561460fda

+ 27 - 13
lib/Cake/Console/Command/Task/TestTask.php

@@ -49,7 +49,13 @@ class TestTask extends BakeTask {
  * @var array
  * @access public
  */
-	public $classTypes =  array('Model', 'Controller', 'Component', 'Behavior', 'Helper');
+	public $classTypes =  array(
+		'Model' => 'Model',
+		'Controller' => 'Controller',
+		'Component' => 'Controller/Component',
+		'Behavior' => 'Model/Behavior',
+		'Helper' => 'View/Helper'
+	);
 
 /**
  * Internal list of fixtures that have been added so far.
@@ -95,8 +101,8 @@ class TestTask extends BakeTask {
 
 		if ($type) {
 			$type = Inflector::camelize($type);
-			if (!in_array($type, $this->classTypes)) {
-				$this->error(__d('cake_console', 'Incorrect type provided. Please choose one of %s', implode(', ', $this->classTypes)));
+			if (!isset($this->classTypes[$type])) {
+				$this->error(__d('cake_console', 'Incorrect type provided. Please choose one of %s', implode(', ', array_keys($this->classTypes))));
 			}
 		} else {
 			$type = $this->getObjectType();
@@ -119,7 +125,11 @@ class TestTask extends BakeTask {
 		} elseif ($this->interactive) {
 			$this->getUserFixtures();
 		}
-		$fullClassName = $this->getRealClassName($type, $className);
+		$fullClassName = $className;
+
+		if (!$this->interactive) {
+			$fullClassName = $this->getRealClassName($type, $className);
+		}
 
 		$methods = array();
 		if (class_exists($fullClassName)) {
@@ -158,16 +168,18 @@ class TestTask extends BakeTask {
 		$this->hr();
 
 		$keys = array();
-		foreach ($this->classTypes as $key => $option) {
-			$this->out(++$key . '. ' . $option);
-			$keys[] = $key;
+		$i = 0;
+		foreach ($this->classTypes as $option => $package) {
+			$this->out(++$i . '. ' . $option);
+			$keys[] = $i;
 		}
 		$keys[] = 'q';
 		$selection = $this->in(__d('cake_console', 'Enter the type of object to bake a test for or (q)uit'), $keys, 'q');
 		if ($selection == 'q') {
 			return $this->_stop();
 		}
-		return $this->classTypes[$selection - 1];
+		$types = array_keys($this->classTypes);
+		return $types[$selection - 1];
 	}
 
 /**
@@ -257,7 +269,7 @@ class TestTask extends BakeTask {
  * @return string Real classname
  */
 	public function getRealClassName($type, $class) {
-		if (strtolower($type) == 'model') {
+		if (strtolower($type) == 'model' || empty($this->classTypes[$type])) {
 			return $class;
 		}
 		return $class . $type;
@@ -417,12 +429,14 @@ class TestTask extends BakeTask {
  * @return string filename the test should be created on.
  */
 	public function testCaseFileName($type, $className) {
-		$path = $this->getPath();;
-		$path .= 'cases' . DS . strtolower($type) . 's' . DS;
-		if (strtolower($type) == 'controller') {
+		$path = $this->getPath() . 'Case' . DS;
+		if (isset($this->classTypes[$type])) {
+			$path .= $this->classTypes[$type] . DS;
+		}
+		if (!$this->interactive) {
 			$className = $this->getRealClassName($type, $className);
 		}
-		return $path . Inflector::underscore($className) . '.test.php';
+		return $path . Inflector::camelize($className) . 'Test.php';
 	}
 
 /**

+ 11 - 11
lib/Cake/tests/Case/Console/Command/Task/TestTaskTest.php

@@ -269,7 +269,7 @@ class TestTaskTest extends CakeTestCase {
 		$this->Task->expects($this->never())->method('err');
 		$this->Task->expects($this->never())->method('_stop');
 
-		$file = TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php';
+		$file = TESTS . 'Case' . DS . 'Model' . DS . 'MyClassTest.php';
 
 		$this->Task->expects($this->at(1))->method('createFile')
 			->with($file, new PHPUnit_Framework_Constraint_IsAnything());
@@ -277,7 +277,7 @@ class TestTaskTest extends CakeTestCase {
 		$this->Task->expects($this->at(3))->method('createFile')
 			->with($file, new PHPUnit_Framework_Constraint_IsAnything());
 
-		$file = TESTS . 'cases' . DS . 'controllers' . DS . 'comments_controller.test.php';
+		$file = TESTS . 'Case' . DS . 'Controller' . DS . 'CommentsControllerTest.php';
 		$this->Task->expects($this->at(5))->method('createFile')
 			->with($file, new PHPUnit_Framework_Constraint_IsAnything());
 
@@ -339,7 +339,7 @@ class TestTaskTest extends CakeTestCase {
 		$this->Task->getObjectType();
 
 		$result = $this->Task->getObjectType();
-		$this->assertEqual($result, $this->Task->classTypes[1]);
+		$this->assertEqual($result, $this->Task->classTypes['Controller']);
 	}
 
 /**
@@ -525,7 +525,7 @@ class TestTaskTest extends CakeTestCase {
 	public function testBakeWithPlugin() {
 		$this->Task->plugin = 'TestTest';
 
-		$path = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'form.test.php';
+		$path = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS  .'FormHelperTest.php';
 		$this->Task->expects($this->once())->method('createFile')
 			->with($path, new PHPUnit_Framework_Constraint_IsAnything());
 
@@ -544,7 +544,7 @@ class TestTaskTest extends CakeTestCase {
 		), true);
 
 		$this->Task->plugin = 'TestPlugin';
-		$path = $testApp . 'test_plugin' . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'other_helper_helper.test.php';
+		$path = $testApp . 'test_plugin' . DS . 'tests' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'OtherHelperHelperTest.php';
 		$this->Task->expects($this->any())
 			->method('in')
 			->will($this->onConsecutiveCalls(
@@ -572,28 +572,28 @@ class TestTaskTest extends CakeTestCase {
 		$this->Task->path = '/my/path/tests/';
 
 		$result = $this->Task->testCaseFileName('Model', 'Post');
-		$expected = $this->Task->path . 'cases' . DS . 'models' . DS . 'post.test.php';
+		$expected = $this->Task->path . 'Case' . DS . 'Model' . DS . 'PostTest.php';
 		$this->assertEqual($result, $expected);
 
 		$result = $this->Task->testCaseFileName('Helper', 'Form');
-		$expected = $this->Task->path . 'cases' . DS . 'helpers' . DS . 'form.test.php';
+		$expected = $this->Task->path . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php';
 		$this->assertEqual($result, $expected);
 
 		$result = $this->Task->testCaseFileName('Controller', 'Posts');
-		$expected = $this->Task->path . 'cases' . DS . 'controllers' . DS . 'posts_controller.test.php';
+		$expected = $this->Task->path . 'Case' . DS . 'Controller' . DS . 'PostsControllerTest.php';
 		$this->assertEqual($result, $expected);
 
 		$result = $this->Task->testCaseFileName('Behavior', 'Containable');
-		$expected = $this->Task->path . 'cases' . DS . 'behaviors' . DS . 'containable.test.php';
+		$expected = $this->Task->path . 'Case' . DS . 'Model' . DS . 'Behavior' . DS . 'ContainableBehaviorTest.php';
 		$this->assertEqual($result, $expected);
 
 		$result = $this->Task->testCaseFileName('Component', 'Auth');
-		$expected = $this->Task->path . 'cases' . DS . 'components' . DS . 'auth.test.php';
+		$expected = $this->Task->path . 'Case' . DS . 'Controller' . DS  . 'Component' . DS . 'AuthComponentTest.php';
 		$this->assertEqual($result, $expected);
 
 		$this->Task->plugin = 'TestTest';
 		$result = $this->Task->testCaseFileName('Model', 'Post');
-		$expected = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'cases' . DS . 'models' . DS . 'post.test.php';
+		$expected = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'Case' . DS . 'Model' . DS . 'PostTest.php';
 		$this->assertEqual($result, $expected);
 	}