From 975aab8009d0f2bf63a17ae837b40c3d37607296 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 25 May 2009 00:54:23 -0400 Subject: [PATCH] Fixing plugin imports Adding contstructor generation. and test case for controller construction. --- cake/console/libs/tasks/test.php | 91 ++++++++----------- cake/console/libs/templates/objects/test.ctp | 16 +++- .../cases/console/libs/tasks/test.test.php | 18 ++++ 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/cake/console/libs/tasks/test.php b/cake/console/libs/tasks/test.php index a520209684d..ae50b9da795 100644 --- a/cake/console/libs/tasks/test.php +++ b/cake/console/libs/tasks/test.php @@ -143,10 +143,17 @@ function bake($type, $className) { if (class_exists($fullClassName)) { $methods = $this->getTestableMethods($fullClassName); } + $mock = $this->generateMockClass($type, $fullClassName); + $construction = $this->generateConstructor($type, $fullClassName); + + $plugin = null; + if ($this->plugin) { + $plugin = $this->plugin . '.'; + } $this->Template->set('fixtures', $this->_fixtures); - $this->Template->set('plugin', $this->plugin); - $this->Template->set(compact('className', 'methods', 'type', 'fullClassName')); + $this->Template->set('plugin', $plugin); + $this->Template->set(compact('className', 'methods', 'type', 'fullClassName', 'mock', 'construction')); $out = $this->Template->generate('objects', 'test'); if (strpos($this->path, $type) === false) { @@ -157,58 +164,6 @@ function bake($type, $className) { return $out; } return false; - - /* - if (!$name) { - return false; - } - - if (!is_array($cases)) { - $cases = array($cases); - } - - if (strpos($this->path, $class) === false) { - $this->filePath = $this->path . 'cases' . DS . Inflector::tableize($class) . DS; - } - - $class = Inflector::classify($class); - $name = Inflector::classify($name); - - $import = $name; - if (isset($this->plugin)) { - $import = $this->plugin . '.' . $name; - } - $extras = $this->__extras($class); - $out = "App::import('$class', '$import');\n"; - if ($class == 'Model') { - $class = null; - } - $out .= "class Test{$name} extends {$name}{$class} {\n"; - $out .= "{$extras}"; - $out .= "}\n\n"; - $out .= "class {$name}{$class}Test extends CakeTestCase {\n"; - $out .= "\n\tfunction startTest() {"; - $out .= "\n\t\t\$this->{$name} = new Test{$name}();"; - $out .= "\n\t}\n"; - $out .= "\n\tfunction test{$name}Instance() {\n"; - $out .= "\t\t\$this->assertTrue(is_a(\$this->{$name}, '{$name}{$class}'));\n\t}\n"; - foreach ($cases as $case) { - $case = Inflector::classify($case); - $out .= "\n\tfunction test{$case}() {\n\n\t}\n"; - } - $out .= "}\n"; - - $this->out("Baking unit test for $name..."); - $this->out($out); - $ok = $this->in(__('Is this correct?', true), array('y', 'n'), 'y'); - if ($ok == 'n') { - return false; - } - - $header = '$Id'; - $content = ""; - return $this->createFile($this->filePath . Inflector::underscore($name) . '.test.php', $content); - */ } /** @@ -277,7 +232,7 @@ function isLoadableClass($type, $class) { /** * Construct an instance of the class to be tested. - * So that fixtures and methods can be detected + * So that fixtures can be detected * * @return object **/ @@ -418,6 +373,31 @@ function getUserFixtures() { return $fixtures; } +/** + * Generate a stub class or mock as needed. + * + * @return void + **/ + function generateMockClass($type, $class) { + return ''; + } + +/** + * Generate a constructor code snippet for the type and classname + * + * @return string Constructor snippet for the thing you are building. + **/ + function generateConstructor($type, $fullClassName) { + $type = strtolower($type); + if ($type == 'model') { + return "ClassRegistry::init('$fullClassName');"; + } + if ($type == 'controller') { + return "new Test$fullClassName();\n\t\t\$this->{$fullClassName}->constructClasses();"; + } + return "new $fullClassName"; + } + /** * Handles the extra stuff needed * @@ -433,6 +413,7 @@ function __extras($class) { } return $extras; } + /** * Create a test for a Model object. * diff --git a/cake/console/libs/templates/objects/test.ctp b/cake/console/libs/templates/objects/test.ctp index 8300c53562b..c1b3cc76d72 100644 --- a/cake/console/libs/templates/objects/test.ctp +++ b/cake/console/libs/templates/objects/test.ctp @@ -19,14 +19,26 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ echo " -App::import('', ''); +App::import('', ''); -class TestCase extends CakeTestCase { + + +class TestCase extends CakeTestCase { var $fixtures = array(''); + function startTest() { + $this-> + } + + function endTest() { + unset($this->); + ClassRegistry::flush(); + } + function test() { diff --git a/cake/tests/cases/console/libs/tasks/test.test.php b/cake/tests/cases/console/libs/tasks/test.test.php index 59e94780564..3a86100cc66 100644 --- a/cake/tests/cases/console/libs/tasks/test.test.php +++ b/cake/tests/cases/console/libs/tasks/test.test.php @@ -289,6 +289,13 @@ function testBake() { $this->assertPattern('/App::import\(\'Model\', \'TestTaskArticle\'\)/', $result); $this->assertPattern('/class TestTaskArticleTestCase extends CakeTestCase/', $result); + + $this->assertPattern('/function startTest\(\)/', $result); + $this->assertPattern("/\\\$this->TestTaskArticle \=\& ClassRegistry::init\('TestTaskArticle'\)/", $result); + + $this->assertPattern('/function endTest\(\)/', $result); + $this->assertPattern('/unset\(\$this->TestTaskArticle\)/', $result); + $this->assertPattern('/function testDoSomething\(\)/', $result); $this->assertPattern('/function testDoSomethingElse\(\)/', $result); @@ -297,5 +304,16 @@ function testBake() { $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 + **/ + function testGenerateContsructor() { + $result = $this->Task->generateConstructor('controller', 'PostsController'); + $expected = "new TestPostsController();\n\t\t\$this->PostsController->constructClasses();"; + $this->assertEqual($result, $expected); + } } ?> \ No newline at end of file