Skip to content

Commit

Permalink
Fix issue with getting Table instances.
Browse files Browse the repository at this point in the history
If a table instance is configured an exception will be thrown if you try
to reconfigure it. Use exists() to work around that issue.
  • Loading branch information
markstory committed Mar 31, 2014
1 parent 08289ba commit cd2d0a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Console/Command/Task/TestTask.php
Expand Up @@ -423,7 +423,8 @@ public function generateConstructor($type, $fullClassName) {
$pre = $construct = $post = '';
if ($type === 'table') {
$className = str_replace('Table', '', $className);
$construct = "TableRegistry::get('{$className}', ['className' => '{$fullClassName}']);\n";
$pre = "\$config = TableRegistry::exists('{$className}') ? [] : ['className' => '{$fullClassName}'];\n";
$construct = "TableRegistry::get('{$className}', \$config);\n";
}
if ($type === 'behavior' || $type === 'entity') {
$construct = "new {$className}();\n";
Expand Down
17 changes: 11 additions & 6 deletions tests/TestCase/Console/Command/Task/TestTaskTest.php
Expand Up @@ -338,7 +338,8 @@ public function testBakeModelTest() {
$this->assertContains('class ArticlesTableTest extends TestCase', $result);

$this->assertContains('function setUp()', $result);
$this->assertContains("\$this->Articles = TableRegistry::get('Articles', [", $result);
$this->assertContains("\$config = TableRegistry::exists('Articles') ?", $result);
$this->assertContains("\$this->Articles = TableRegistry::get('Articles', \$config", $result);

$this->assertContains('function tearDown()', $result);
$this->assertContains('unset($this->Articles)', $result);
Expand Down Expand Up @@ -478,19 +479,23 @@ public function testBakeHelperTest() {
*/
public function testGenerateConstructor() {
$result = $this->Task->generateConstructor('controller', 'PostsController');
$expected = array('', '', '');
$expected = ['', '', ''];
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('table', 'App\Model\\Table\PostsTable');
$expected = array('', "TableRegistry::get('Posts', ['className' => 'App\Model\\Table\PostsTable']);\n", '');
$expected = [
"\$config = TableRegistry::exists('Posts') ? [] : ['className' => 'App\Model\\Table\PostsTable'];\n",
"TableRegistry::get('Posts', \$config);\n",
''
];
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('helper', 'FormHelper');
$expected = array("\$view = new View();\n", "new FormHelper(\$view);\n", '');
$expected = ["\$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", '');
$result = $this->Task->generateConstructor('entity', 'TestPlugin\Model\Entity\Article');
$expected = ["", "new Article();\n", ''];
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit cd2d0a6

Please sign in to comment.