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 * @return string|boolean
*/ */
public function bake($type, $className) { 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...')); $this->out(__d('cake_console', 'Bake is detecting possible fixtures...'));
$testSubject = $this->buildTestSubject($type, $className); $testSubject = $this->buildTestSubject($type, $className);
$this->generateFixtureList($testSubject); $this->generateFixtureList($testSubject);
} elseif ($this->interactive) { } elseif ($this->interactive) {
$this->getUserFixtures(); $this->getUserFixtures();
} }
$fullClassName = $className; App::uses($fullClassName, $realType);
if (!$this->interactive) {
$fullClassName = $this->getRealClassName($type, $className);
}
$methods = array(); $methods = array();
if (class_exists($fullClassName)) { if (class_exists($fullClassName)) {
@ -136,15 +140,14 @@ class TestTask extends BakeTask {
$mock = $this->hasMockClass($type, $fullClassName); $mock = $this->hasMockClass($type, $fullClassName);
$construction = $this->generateConstructor($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->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('fixtures', $this->_fixtures);
$this->Template->set('plugin', $plugin); $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'); $out = $this->Template->generate('classes', 'test');
$filename = $this->testCaseFileName($type, $className); $filename = $this->testCaseFileName($type, $className);
@ -223,18 +226,19 @@ class TestTask extends BakeTask {
*/ */
public function typeCanDetectFixtures($type) { public function typeCanDetectFixtures($type) {
$type = strtolower($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. * @param string $class the Classname of the class the test is being generated for.
* @return boolean * @return boolean
*/ */
public function isLoadableClass($type, $class) { public function isLoadableClass($package, $class) {
return App::import($type, $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 $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. * @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])) { if (strtolower($type) == 'model' || empty($this->classTypes[$type])) {
return $class; return $class;
} }
if (strlen($class) - strpos($class, $type) == strlen($type)) {
return $class;
}
return $class . $type; 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. * Get methods declared in the class given.
* No parent methods will be returned * No parent methods will be returned
@ -429,9 +456,7 @@ class TestTask extends BakeTask {
if (isset($this->classTypes[$type])) { if (isset($this->classTypes[$type])) {
$path .= $this->classTypes[$type] . DS; $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'; return str_replace('/', DS, $path) . Inflector::camelize($className) . 'Test.php';
} }

View file

@ -20,7 +20,7 @@
echo "<?php\n"; echo "<?php\n";
echo "/* ". $className ." Test cases generated on: " . date('Y-m-d H:i:s') . " : ". time() . "*/\n"; echo "/* ". $className ." Test cases generated on: " . date('Y-m-d H:i:s') . " : ". time() . "*/\n";
?> ?>
App::uses('<?php echo $fullClassName; ?>', '<?php echo $plugin . $type;?>'); App::uses('<?php echo $fullClassName; ?>', '<?php echo $realType; ?>');
<?php if ($mock and strtolower($type) == 'controller'): ?> <?php if ($mock and strtolower($type) == 'controller'): ?>
/** /**

View file

@ -260,14 +260,14 @@ class TestTaskTest extends CakeTestCase {
$file = TESTS . 'Case' . DS . 'Model' . DS . 'MyClassTest.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, $this->anything());
$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, $this->anything());
$file = TESTS . 'Case' . DS . 'Controller' . DS . 'CommentsControllerTest.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, $this->anything());
$this->Task->bake('Model', 'MyClass'); $this->Task->bake('Model', 'MyClass');
$this->Task->bake('Model', 'MyClass'); $this->Task->bake('Model', 'MyClass');
@ -401,12 +401,21 @@ class TestTaskTest extends CakeTestCase {
$result = $this->Task->getRealClassname('Controller', 'Posts'); $result = $this->Task->getRealClassname('Controller', 'Posts');
$this->assertEqual($result, 'PostsController'); $this->assertEqual($result, 'PostsController');
$result = $this->Task->getRealClassname('Controller', 'PostsController');
$this->assertEqual($result, 'PostsController');
$result = $this->Task->getRealClassname('Helper', 'Form'); $result = $this->Task->getRealClassname('Helper', 'Form');
$this->assertEqual($result, 'FormHelper'); $this->assertEqual($result, 'FormHelper');
$result = $this->Task->getRealClassname('Helper', 'FormHelper');
$this->assertEqual($result, 'FormHelper');
$result = $this->Task->getRealClassname('Behavior', 'Containable'); $result = $this->Task->getRealClassname('Behavior', 'Containable');
$this->assertEqual($result, 'ContainableBehavior'); $this->assertEqual($result, 'ContainableBehavior');
$result = $this->Task->getRealClassname('Behavior', 'ContainableBehavior');
$this->assertEqual($result, 'ContainableBehavior');
$result = $this->Task->getRealClassname('Component', 'Auth'); $result = $this->Task->getRealClassname('Component', 'Auth');
$this->assertEqual($result, 'AuthComponent'); $this->assertEqual($result, 'AuthComponent');
} }
@ -515,7 +524,7 @@ class TestTaskTest extends CakeTestCase {
CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS)); CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
$path = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS .'FormHelperTest.php'; $path = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . 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, $this->anything());
$this->Task->bake('Helper', 'Form'); $this->Task->bake('Helper', 'Form');
CakePlugin::unload(); CakePlugin::unload();
@ -599,7 +608,7 @@ class TestTaskTest extends CakeTestCase {
$this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true)); $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
$this->Task->expects($this->once())->method('createFile') $this->Task->expects($this->once())->method('createFile')
->with( ->with(
new PHPUnit_Framework_Constraint_IsAnything(), $this->anything(),
$this->stringContains('class TestTaskTagTestCase extends CakeTestCase') $this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
); );
$this->Task->execute(); $this->Task->execute();
@ -615,10 +624,41 @@ class TestTaskTest extends CakeTestCase {
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag')); $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
$this->Task->expects($this->once())->method('createFile') $this->Task->expects($this->once())->method('createFile')
->with( ->with(
new PHPUnit_Framework_Constraint_IsAnything(), $this->anything(),
$this->stringContains('class TestTaskTagTestCase extends CakeTestCase') $this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
); );
$this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true)); $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
$this->Task->execute(); $this->Task->execute();
} }
/**
* Data provider for mapType() tests.
*
* @return array
*/
public static function mapTypeProvider() {
return array(
array('controller', null, 'Controller'),
array('Controller', null, 'Controller'),
array('component', null, 'Controller/Component'),
array('Component', null, 'Controller/Component'),
array('model', null, 'Model'),
array('Model', null, 'Model'),
array('behavior', null, 'Model/Behavior'),
array('Behavior', null, 'Model/Behavior'),
array('helper', null, 'View/Helper'),
array('Helper', null, 'View/Helper'),
array('Helper', 'DebugKit', 'DebugKit.View/Helper'),
);
}
/**
* Test that mapType returns the correct package names.
*
* @dataProvider mapTypeProvider
* @return void
*/
public function testMapType($original, $plugin, $expected) {
$this->assertEquals($expected, $this->Task->mapType($original, $plugin));
}
} }