Skip to content

Commit

Permalink
Use helper to generate behavior config array.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Dec 2, 2017
1 parent de561b9 commit fa9639d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 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
11 changes: 7 additions & 4 deletions tests/TestCase/Shell/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,11 +1039,14 @@ 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'"
];
$this->assertEquals($expected, $result);

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

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/comparisons/Model/testGetBehaviors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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'
]);
}

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

0 comments on commit fa9639d

Please sign in to comment.