Skip to content

Commit

Permalink
Add CounterCacheBehavior integration into bake.
Browse files Browse the repository at this point in the history
Refs #1908
  • Loading branch information
markstory committed Apr 2, 2014
1 parent 3169924 commit 5e6ffd5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/Console/Command/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,27 @@ public function getBehaviors($model) {
return [];
}
if (in_array('created', $fields) || in_array('modified', $fields)) {
$behaviors[] = 'Timestamp';
$behaviors['Timestamp'] = '';
}

if (in_array('lft', $fields) && $schema->columnType('lft') === 'integer' &&
in_array('rght', $fields) && $schema->columnType('rght') === 'integer' &&
in_array('parent_id', $fields)
) {
$behaviors[] = 'Tree';
$behaviors['Tree'] = '';
}

$counterCache = [];
foreach ($fields as $field) {
if (strpos($field, '_count') === false) {
continue;
}
list($name) = explode('_count', $field);
$assoc = $this->_modelName($name);
$counterCache[] = "'{$assoc}' => ['{$field}']";
}
if (!empty($counterCache)) {
$behaviors['CounterCache'] = $counterCache;
}
return $behaviors;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Templates/default/classes/table.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ $key = array_map(function($el) { return "'$el'"; }, (array)$primaryKey);
?>
$this->primaryKey([<?= implode(', ', $key) ?>]);
<?php endif ?>
<?php foreach ($behaviors as $behavior): ?>
$this->addBehavior('<?= $behavior ?>');
<?php foreach ($behaviors as $behavior => $behaviorData): ?>
$this->addBehavior('<?= $behavior ?>'<?= $behaviorData ? ", [" . implode(', ', $behaviorData) . ']' : '' ?>);
<?php endforeach ?>

<?php foreach ($associations as $type => $assocs): ?>
Expand Down
27 changes: 20 additions & 7 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class ModelTaskTest extends TestCase {
*/
public $fixtures = array(
'core.bake_article', 'core.bake_comment', 'core.bake_articles_bake_tag',
'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree'
'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree',
'core.counter_cache_user'
);

/**
Expand Down Expand Up @@ -441,11 +442,18 @@ public function testGetValidation() {
public function testGetBehaviors() {
$model = TableRegistry::get('NumberTrees');
$result = $this->Task->getBehaviors($model);
$this->assertEquals(['Tree'], $result);
$this->assertEquals(['Tree' => ''], $result);

$model = TableRegistry::get('BakeArticles');
$result = $this->Task->getBehaviors($model);
$this->assertEquals(['Timestamp'], $result);
$this->assertEquals(['Timestamp' => ''], $result);

$model = TableRegistry::get('CounterCacheUsers');
$result = $this->Task->getBehaviors($model);
$expected = [
'CounterCache' => ["'Posts' => ['post_count']"]
];
$this->assertEquals($expected, $result);
}

/**
Expand Down Expand Up @@ -562,7 +570,7 @@ public function testBakeTableConfig() {
'table' => 'articles',
'primaryKey' => ['id'],
'displayField' => 'title',
'behaviors' => ['Timestamp'],
'behaviors' => ['Timestamp' => ''],
];
$model = TableRegistry::get('BakeArticles');
$result = $this->Task->bakeTable($model, $config);
Expand Down Expand Up @@ -873,9 +881,9 @@ public function testSkipTablesAndAll() {
$this->Task->args = ['all'];
$this->Task->skipTables = ['bake_tags'];

$this->Task->Fixture->expects($this->exactly(6))
$this->Task->Fixture->expects($this->exactly(7))
->method('bake');
$this->Task->Test->expects($this->exactly(6))
$this->Task->Test->expects($this->exactly(7))
->method('bake');

$filename = '/my/path/Entity/BakeArticle.php';
Expand All @@ -898,11 +906,16 @@ public function testSkipTablesAndAll() {
->method('createFile')
->with($filename);

$filename = '/my/path/Entity/NumberTree.php';
$filename = '/my/path/Entity/CounterCacheUser.php';
$this->Task->expects($this->at(9))
->method('createFile')
->with($filename);

$filename = '/my/path/Entity/NumberTree.php';
$this->Task->expects($this->at(11))
->method('createFile')
->with($filename);

$this->Task->execute();
}

Expand Down

0 comments on commit 5e6ffd5

Please sign in to comment.