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

This commit is contained in:
Jose Lorenzo Rodriguez 2011-04-10 20:49:16 -04:30
parent 0263ffc74e
commit d561460fda
2 changed files with 38 additions and 24 deletions

View file

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

View file

@ -269,7 +269,7 @@ class TestTaskTest extends CakeTestCase {
$this->Task->expects($this->never())->method('err'); $this->Task->expects($this->never())->method('err');
$this->Task->expects($this->never())->method('_stop'); $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') $this->Task->expects($this->at(1))->method('createFile')
->with($file, new PHPUnit_Framework_Constraint_IsAnything()); ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
@ -277,7 +277,7 @@ class TestTaskTest extends CakeTestCase {
$this->Task->expects($this->at(3))->method('createFile') $this->Task->expects($this->at(3))->method('createFile')
->with($file, new PHPUnit_Framework_Constraint_IsAnything()); ->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') $this->Task->expects($this->at(5))->method('createFile')
->with($file, new PHPUnit_Framework_Constraint_IsAnything()); ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
@ -339,7 +339,7 @@ class TestTaskTest extends CakeTestCase {
$this->Task->getObjectType(); $this->Task->getObjectType();
$result = $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() { public function testBakeWithPlugin() {
$this->Task->plugin = 'TestTest'; $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') $this->Task->expects($this->once())->method('createFile')
->with($path, new PHPUnit_Framework_Constraint_IsAnything()); ->with($path, new PHPUnit_Framework_Constraint_IsAnything());
@ -544,7 +544,7 @@ class TestTaskTest extends CakeTestCase {
), true); ), true);
$this->Task->plugin = 'TestPlugin'; $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()) $this->Task->expects($this->any())
->method('in') ->method('in')
->will($this->onConsecutiveCalls( ->will($this->onConsecutiveCalls(
@ -572,28 +572,28 @@ class TestTaskTest extends CakeTestCase {
$this->Task->path = '/my/path/tests/'; $this->Task->path = '/my/path/tests/';
$result = $this->Task->testCaseFileName('Model', 'Post'); $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); $this->assertEqual($result, $expected);
$result = $this->Task->testCaseFileName('Helper', 'Form'); $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); $this->assertEqual($result, $expected);
$result = $this->Task->testCaseFileName('Controller', 'Posts'); $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); $this->assertEqual($result, $expected);
$result = $this->Task->testCaseFileName('Behavior', 'Containable'); $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); $this->assertEqual($result, $expected);
$result = $this->Task->testCaseFileName('Component', 'Auth'); $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->assertEqual($result, $expected);
$this->Task->plugin = 'TestTest'; $this->Task->plugin = 'TestTest';
$result = $this->Task->testCaseFileName('Model', 'Post'); $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); $this->assertEqual($result, $expected);
} }