Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding tests and code for controller stub generation.

  • Loading branch information...
commit 518c318887485291c54e9c1de9f33dcc22c48321 1 parent 877616a
@markstory markstory authored
View
12 cake/console/libs/tasks/test.php
@@ -143,7 +143,7 @@ function bake($type, $className) {
if (class_exists($fullClassName)) {
$methods = $this->getTestableMethods($fullClassName);
}
- $mock = $this->generateMockClass($type, $fullClassName);
+ $mock = $this->hasMockClass($type, $fullClassName);
$construction = $this->generateConstructor($type, $fullClassName);
$plugin = null;
@@ -374,12 +374,14 @@ function getUserFixtures() {
}
/**
- * Generate a stub class or mock as needed.
+ * Is a mock class required for this type of test?
+ * Controllers require a mock class.
*
- * @return void
+ * @return boolean
**/
- function generateMockClass($type, $class) {
- return '';
+ function hasMockClass($type) {
+ $type = strtolower($type);
+ return $type == 'controller';
}
/**
View
10 cake/console/libs/templates/objects/test.ctp
@@ -23,8 +23,16 @@ echo "/* ". $className ." Test cases generated on: " . date('Y-m-d H:m:s') . " :
?>
App::import('<?php echo $type; ?>', '<?php echo $plugin . $className;?>');
-<?php echo $mock; ?>
+<?php if ($mock and strtolower($type) == 'controller'): ?>
+class Test<?php echo $fullClassName; ?> extends <?php echo $fullClassName; ?> {
+ var $autoRender = false;
+ function redirect($url, $status = null, $exit = true) {
+ $this->redirectUrl = $url;
+ }
+}
+
+<?php endif; ?>
class <?php echo $fullClassName; ?>TestCase extends CakeTestCase {
<?php if (!empty($fixtures)): ?>
var $fixtures = array('<?php echo join("', '", $fixtures); ?>');
View
42 cake/tests/cases/console/libs/tasks/test.test.php
@@ -281,7 +281,7 @@ function testGetRealClassname() {
*
* @return void
**/
- function testBake() {
+ function testBakeModelTest() {
$this->Task->setReturnValue('createFile', true);
$this->Task->setReturnValue('isLoadableClass', true);
@@ -306,6 +306,36 @@ function testBake() {
}
/**
+ * test baking controller test files, ensure that the stub class is generated.
+ *
+ * @return void
+ **/
+ function testBakeControllerTest() {
+ $this->Task->setReturnValue('createFile', true);
+ $this->Task->setReturnValue('isLoadableClass', true);
+
+ $result = $this->Task->bake('Controller', 'TestTaskComments');
+
+ $this->assertPattern('/App::import\(\'Controller\', \'TestTaskComments\'\)/', $result);
+ $this->assertPattern('/class TestTaskCommentsControllerTestCase extends CakeTestCase/', $result);
+
+ $this->assertPattern('/class TestTestTaskCommentsController extends TestTaskCommentsController/', $result);
+ $this->assertPattern('/var \$autoRender = false/', $result);
+ $this->assertPattern('/function redirect\(\$url, \$status = null, \$exit = true\)/', $result);
+
+ $this->assertPattern('/function startTest\(\)/', $result);
+ $this->assertPattern("/\\\$this->TestTaskComments \=\& new TestTestTaskCommentsController()/", $result);
+
+ $this->assertPattern('/function endTest\(\)/', $result);
+ $this->assertPattern('/unset\(\$this->TestTaskComments\)/', $result);
+
+ $this->assertPattern("/'app\.test_task_article'/", $result);
+ $this->assertPattern("/'plugin\.test_task\.test_task_comment'/", $result);
+ $this->assertPattern("/'app\.test_task_tag'/", $result);
+ $this->assertPattern("/'app\.articles_tag'/", $result);
+ }
+
+/**
* test Constructor generation ensure that constructClasses is called for controllers
*
* @return void
@@ -323,5 +353,15 @@ function testGenerateContsructor() {
$expected = "new FormHelper()\n";
$this->assertEqual($result, $expected);
}
+
+/**
+ * Test that mock class generation works for the appropriate classes
+ *
+ * @return void
+ **/
+ function testMockClassGeneration() {
+ $result = $this->Task->hasMockClass('controller');
+ $this->assertTrue($result);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.