Skip to content

Commit

Permalink
Fix failing tests for constructor generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 31, 2014
1 parent f109ca1 commit b3ab244
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/Console/Command/Task/TestTask.php
Expand Up @@ -508,14 +508,15 @@ public function generateConstructor($type, $fullClassName) {
$type = strtolower($type);
$pre = $construct = $post = '';
if ($type === 'table') {
$construct = "TableRegistry::init('{$className}', ['className' => '{$fullClassName}']);\n";
$className = str_replace('Table', '', $className);
$construct = "TableRegistry::get('{$className}', ['className' => '{$fullClassName}']);\n";
}
if ($type === 'behavior' || $type === 'entity') {
$construct = "new {$className}();\n";
}
if ($type === 'helper') {
$pre = "\$View = new View();\n";
$construct = "new {$className}(\$View);\n";
$pre = "\$view = new View();\n";
$construct = "new {$className}(\$view);\n";
}
if ($type === 'component') {
$pre = "\$registry = new ComponentRegistry();\n";
Expand Down
31 changes: 19 additions & 12 deletions tests/TestCase/Console/Command/Task/TestTaskTest.php
Expand Up @@ -462,8 +462,8 @@ public function testBakeHelperTest() {
$this->assertContains('class ExampleHelperTest extends TestCase', $result);

$this->assertContains('function setUp()', $result);
$this->assertContains("\$View = new View()", $result);
$this->assertContains("\$this->Example = new ExampleHelper(\$View)", $result);
$this->assertContains("\$view = new View()", $result);
$this->assertContains("\$this->Example = new ExampleHelper(\$view)", $result);

$this->assertContains('function tearDown()', $result);
$this->assertContains('unset($this->Example)', $result);
Expand All @@ -475,43 +475,50 @@ public function testBakeHelperTest() {
* @return void
*/
public function testGenerateConstructor() {
$result = $this->Task->generateConstructor('controller', 'PostsController', null);
$result = $this->Task->generateConstructor('controller', 'PostsController');
$expected = array('', '', '');
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('model', 'Post', null);
$expected = array('', "ClassRegistry::init('Post');\n", '');
$result = $this->Task->generateConstructor('table', 'App\Model\\Table\PostsTable');
$expected = array('', "TableRegistry::get('Posts', ['className' => 'App\Model\\Table\PostsTable']);\n", '');
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('helper', 'FormHelper', null);
$expected = array("\$View = new View();\n", "new FormHelper(\$View);\n", '');
$result = $this->Task->generateConstructor('helper', 'FormHelper');
$expected = array("\$view = new View();\n", "new FormHelper(\$view);\n", '');
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('entity', 'TestPlugin\Model\Entity\Article', null);
$expected = array("", "new Article();\n", '');
$this->assertEquals($expected, $result);
}

/**
* Test generateUses()
*
* @return void
*/
public function testGenerateUses() {
$result = $this->Task->generateUses('model', 'Model', 'App\Model\Post');
$result = $this->Task->generateUses('table', 'App\Model\Table\PostsTable');
$expected = array(
'App\Model\Post',
'Cake\ORM\TableRegistry',
'App\Model\Table\PostsTable',
);
$this->assertEquals($expected, $result);

$result = $this->Task->generateUses('controller', 'Controller', 'App\Controller\PostsController');
$result = $this->Task->generateUses('controller', 'App\Controller\PostsController');
$expected = array(
'App\Controller\PostsController',
);
$this->assertEquals($expected, $result);

$result = $this->Task->generateUses('helper', 'View/Helper', 'App\View\Helper\FormHelper');
$result = $this->Task->generateUses('helper', 'App\View\Helper\FormHelper');
$expected = array(
'Cake\View\View',
'App\View\Helper\FormHelper',
);
$this->assertEquals($expected, $result);

$result = $this->Task->generateUses('component', 'Controller/Component', 'App\Controller\Component\AuthComponent');
$result = $this->Task->generateUses('component', 'App\Controller\Component\AuthComponent');
$expected = array(
'Cake\Controller\ComponentRegistry',
'App\Controller\Component\AuthComponent',
Expand Down

0 comments on commit b3ab244

Please sign in to comment.