Skip to content

Commit

Permalink
Merge pull request #389 from cakephp/table-template
Browse files Browse the repository at this point in the history
Use helper to generate behavior config array.
  • Loading branch information
markstory authored Dec 3, 2017
2 parents 13dbee1 + b334b48 commit 55cb0e2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Template/Bake/Model/table.twig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class {{ name }}Table extends Table
{% endif %}
{%- for behavior, behaviorData in behaviors %}
$this->addBehavior('{{ behavior }}'{{ (behaviorData ? (", [" ~ behaviorData|join(', ') ~ ']') : '')|raw }});
$this->addBehavior('{{ behavior }}'{{ (behaviorData ? (", [" ~ Bake.stringifyList(behaviorData, {'indent': 3, 'quotes': false})|raw ~ ']') : '')|raw }});
{% endfor %}
{%- if associations.belongsTo or associations.hasMany or associations.belongsToMany %}
Expand Down
14 changes: 13 additions & 1 deletion src/View/Helper/BakeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ public function stringifyList(array $list, array $options = [])
$v = "'$v'";
}
if (!is_numeric($k)) {
$v = "'$k' => $v";
$nestedOptions = $options;
if ($nestedOptions['indent']) {
$nestedOptions['indent'] += 1;
}
if (is_array($v)) {
$v = sprintf(
"'%s' => [%s]",
$k,
$this->stringifyList($v, $nestedOptions)
);
} else {
$v = "'$k' => $v";
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions tests/TestCase/Shell/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,11 +1039,15 @@ public function testGetBehaviors()
$model = TableRegistry::get('Posts', [
'table' => 'counter_cache_posts'
]);
$result = $this->Task->getBehaviors($model);
$expected = [
'CounterCache' => ["'Users' => ['post_count']"]
$behaviors = $this->Task->getBehaviors($model);

$behaviors['Translate'] = [
'defaultLocale' => "'fr_FR'",
'implementedFinders' => ['translations' => "'findTranslations'"],
];
$this->assertEquals($expected, $result);

$result = $this->Task->bakeTable($model, ['behaviors' => $behaviors]);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down
58 changes: 58 additions & 0 deletions tests/comparisons/Model/testGetBehaviors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
* Posts Model
*
* @method \App\Model\Entity\Post get($primaryKey, $options = [])
* @method \App\Model\Entity\Post newEntity($data = null, array $options = [])
* @method \App\Model\Entity\Post[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Post|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Post patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Post[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Post findOrCreate($search, callable $callback = null, $options = [])
*
* @mixin \Cake\ORM\Behavior\CounterCacheBehavior
* @mixin \Cake\ORM\Behavior\TranslateBehavior
*/
class PostsTable extends Table
{

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);

$this->setPrimaryKey('id');

$this->addBehavior('CounterCache', [
'Users' => ['post_count']
]);
$this->addBehavior('Translate', [
'defaultLocale' => 'fr_FR',
'implementedFinders' => [
'translations' => 'findTranslations'
]
]);
}

/**
* Returns the database connection name to use by default.
*
* @return string
*/
public static function defaultConnectionName()
{
return 'test';
}
}

0 comments on commit 55cb0e2

Please sign in to comment.