Skip to content

Commit b3ab244

Browse files
committed
Fix failing tests for constructor generation.
1 parent f109ca1 commit b3ab244

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/Console/Command/Task/TestTask.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,15 @@ public function generateConstructor($type, $fullClassName) {
508508
$type = strtolower($type);
509509
$pre = $construct = $post = '';
510510
if ($type === 'table') {
511-
$construct = "TableRegistry::init('{$className}', ['className' => '{$fullClassName}']);\n";
511+
$className = str_replace('Table', '', $className);
512+
$construct = "TableRegistry::get('{$className}', ['className' => '{$fullClassName}']);\n";
512513
}
513514
if ($type === 'behavior' || $type === 'entity') {
514515
$construct = "new {$className}();\n";
515516
}
516517
if ($type === 'helper') {
517-
$pre = "\$View = new View();\n";
518-
$construct = "new {$className}(\$View);\n";
518+
$pre = "\$view = new View();\n";
519+
$construct = "new {$className}(\$view);\n";
519520
}
520521
if ($type === 'component') {
521522
$pre = "\$registry = new ComponentRegistry();\n";

tests/TestCase/Console/Command/Task/TestTaskTest.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ public function testBakeHelperTest() {
462462
$this->assertContains('class ExampleHelperTest extends TestCase', $result);
463463

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

468468
$this->assertContains('function tearDown()', $result);
469469
$this->assertContains('unset($this->Example)', $result);
@@ -475,43 +475,50 @@ public function testBakeHelperTest() {
475475
* @return void
476476
*/
477477
public function testGenerateConstructor() {
478-
$result = $this->Task->generateConstructor('controller', 'PostsController', null);
478+
$result = $this->Task->generateConstructor('controller', 'PostsController');
479479
$expected = array('', '', '');
480480
$this->assertEquals($expected, $result);
481481

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

486-
$result = $this->Task->generateConstructor('helper', 'FormHelper', null);
487-
$expected = array("\$View = new View();\n", "new FormHelper(\$View);\n", '');
486+
$result = $this->Task->generateConstructor('helper', 'FormHelper');
487+
$expected = array("\$view = new View();\n", "new FormHelper(\$view);\n", '');
488+
$this->assertEquals($expected, $result);
489+
490+
$result = $this->Task->generateConstructor('entity', 'TestPlugin\Model\Entity\Article', null);
491+
$expected = array("", "new Article();\n", '');
488492
$this->assertEquals($expected, $result);
489493
}
490494

491495
/**
492496
* Test generateUses()
497+
*
498+
* @return void
493499
*/
494500
public function testGenerateUses() {
495-
$result = $this->Task->generateUses('model', 'Model', 'App\Model\Post');
501+
$result = $this->Task->generateUses('table', 'App\Model\Table\PostsTable');
496502
$expected = array(
497-
'App\Model\Post',
503+
'Cake\ORM\TableRegistry',
504+
'App\Model\Table\PostsTable',
498505
);
499506
$this->assertEquals($expected, $result);
500507

501-
$result = $this->Task->generateUses('controller', 'Controller', 'App\Controller\PostsController');
508+
$result = $this->Task->generateUses('controller', 'App\Controller\PostsController');
502509
$expected = array(
503510
'App\Controller\PostsController',
504511
);
505512
$this->assertEquals($expected, $result);
506513

507-
$result = $this->Task->generateUses('helper', 'View/Helper', 'App\View\Helper\FormHelper');
514+
$result = $this->Task->generateUses('helper', 'App\View\Helper\FormHelper');
508515
$expected = array(
509516
'Cake\View\View',
510517
'App\View\Helper\FormHelper',
511518
);
512519
$this->assertEquals($expected, $result);
513520

514-
$result = $this->Task->generateUses('component', 'Controller/Component', 'App\Controller\Component\AuthComponent');
521+
$result = $this->Task->generateUses('component', 'App\Controller\Component\AuthComponent');
515522
$expected = array(
516523
'Cake\Controller\ComponentRegistry',
517524
'App\Controller\Component\AuthComponent',

0 commit comments

Comments
 (0)