Fixing issue baking controller tests.

During interactive controller baking, controller tests
would be generated with the wrong filename.
During interactive baking fixtures would not be detected.
Fixes #2052
This commit is contained in:
mark_story 2011-10-02 21:46:35 -04:00
parent adcb80b5b9
commit aae2b0c257
3 changed files with 92 additions and 27 deletions

View file

@ -116,18 +116,22 @@ class TestTask extends BakeTask {
* @return string|boolean
*/
public function bake($type, $className) {
if ($this->typeCanDetectFixtures($type) && $this->isLoadableClass($type, $className)) {
$plugin = null;
if ($this->plugin) {
$plugin = $this->plugin . '.';
}
$realType = $this->mapType($type, $plugin);
$fullClassName = $this->getRealClassName($type, $className);
if ($this->typeCanDetectFixtures($type) && $this->isLoadableClass($realType, $fullClassName)) {
$this->out(__d('cake_console', 'Bake is detecting possible fixtures...'));
$testSubject = $this->buildTestSubject($type, $className);
$this->generateFixtureList($testSubject);
} elseif ($this->interactive) {
$this->getUserFixtures();
}
$fullClassName = $className;
if (!$this->interactive) {
$fullClassName = $this->getRealClassName($type, $className);
}
App::uses($fullClassName, $realType);
$methods = array();
if (class_exists($fullClassName)) {
@ -136,15 +140,14 @@ class TestTask extends BakeTask {
$mock = $this->hasMockClass($type, $fullClassName);
$construction = $this->generateConstructor($type, $fullClassName);
$plugin = null;
if ($this->plugin) {
$plugin = $this->plugin . '.';
}
$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'));
$this->Template->set(compact(
'className', 'methods', 'type', 'fullClassName', 'mock',
'construction', 'realType'
));
$out = $this->Template->generate('classes', 'test');
$filename = $this->testCaseFileName($type, $className);
@ -223,18 +226,19 @@ class TestTask extends BakeTask {
*/
public function typeCanDetectFixtures($type) {
$type = strtolower($type);
return ($type == 'controller' || $type == 'model');
return in_array($type, array('controller', 'model'));
}
/**
* Check if a class with the given type is loaded or can be loaded.
* Check if a class with the given package is loaded or can be loaded.
*
* @param string $type The Type of object you are generating tests for eg. controller
* @param string $package The package of object you are generating tests for eg. controller
* @param string $class the Classname of the class the test is being generated for.
* @return boolean
*/
public function isLoadableClass($type, $class) {
return App::import($type, $class);
public function isLoadableClass($package, $class) {
App::uses($class, $package);
return class_exists($class);
}
/**
@ -258,7 +262,8 @@ class TestTask extends BakeTask {
}
/**
* Gets the real class name from the cake short form.
* Gets the real class name from the cake short form. If the class name is already
* suffixed with the type, the type will not be duplicated.
*
* @param string $type The Type of object you are generating tests for eg. controller
* @param string $class the Classname of the class the test is being generated for.
@ -268,9 +273,31 @@ class TestTask extends BakeTask {
if (strtolower($type) == 'model' || empty($this->classTypes[$type])) {
return $class;
}
if (strlen($class) - strpos($class, $type) == strlen($type)) {
return $class;
}
return $class . $type;
}
/**
* Map the types that TestTask uses to concrete types that App::uses can use.
*
* @param string $type The type of thing having a test generated.
* @param string $plugin The plugin name.
* @return string
*/
public function mapType($type, $plugin) {
$type = ucfirst($type);
if (empty($this->classTypes[$type])) {
throw new CakeException(__d('cake_dev', 'Invalid object type.'));
}
$real = $this->classTypes[$type];
if ($plugin) {
$real = trim($plugin, '.') . '.' . $real;
}
return $real;
}
/**
* Get methods declared in the class given.
* No parent methods will be returned
@ -429,9 +456,7 @@ class TestTask extends BakeTask {
if (isset($this->classTypes[$type])) {
$path .= $this->classTypes[$type] . DS;
}
if (!$this->interactive) {
$className = $this->getRealClassName($type, $className);
}
$className = $this->getRealClassName($type, $className);
return str_replace('/', DS, $path) . Inflector::camelize($className) . 'Test.php';
}