Skip to content

Commit

Permalink
Merge pull request #671 from CakeDC/feature/add-table-to-behavior-con…
Browse files Browse the repository at this point in the history
…structor

add table to behavior constructor
  • Loading branch information
markstory authored Apr 10, 2020
2 parents 6491dc1 + 80835f8 commit a44c956
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ public function generateConstructor(string $type, string $fullClassName): array
"? [] : ['className' => {$className}::class];";
$construct = "TableRegistry::getTableLocator()->get('{$tableName}', \$config);";
}
if ($type === 'Behavior' || $type === 'Entity' || $type === 'Form') {
if ($type === 'Behavior') {
$pre = "\$table = new Table();";
$construct = "new {$className}(\$table);";
}
if ($type === 'Entity' || $type === 'Form') {
$construct = "new {$className}();";
}
if ($type === 'Helper') {
Expand Down Expand Up @@ -675,6 +679,9 @@ public function generateUses(string $type, string $fullClassName): array
$uses[] = 'Cake\TestSuite\Stub\ConsoleOutput';
$uses[] = 'Cake\Console\ConsoleIo';
}
if ($type === 'Behavior') {
$uses[] = 'Cake\ORM\Table';
}
$uses[] = $fullClassName;

return $uses;
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Command/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ public function testGenerateConstructor()
'',
];
$this->assertEquals($expected, $result);

$result = $command->generateConstructor('Behavior', 'App\Model\Behavior\PostsBehavior');
$expected = [
'$table = new Table();',
'new PostsBehavior($table);',
'',
];
$this->assertEquals($expected, $result);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/comparisons/Test/testBakeBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Bake\Test\App\Test\TestCase\Model\Behavior;

use Bake\Test\App\Model\Behavior\ExampleBehavior;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;

/**
Expand All @@ -26,7 +27,8 @@ class ExampleBehaviorTest extends TestCase
public function setUp(): void
{
parent::setUp();
$this->Example = new ExampleBehavior();
$table = new Table();
$this->Example = new ExampleBehavior($table);
}

/**
Expand Down

0 comments on commit a44c956

Please sign in to comment.