Skip to content

Commit

Permalink
Updated add*() methods to return $this for method chaining.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Pustułka committed May 15, 2017
1 parent 0c5c24c commit 8b1bcc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/ORM/Table.php
Expand Up @@ -816,13 +816,15 @@ public function entityClass($name = null)
*
* @param string $name The name of the behavior. Can be a short class reference.
* @param array $options The options for the behavior to use.
* @return void
* @return $this
* @throws \RuntimeException If a behavior is being reloaded.
* @see \Cake\ORM\Behavior
*/
public function addBehavior($name, array $options = [])
{
$this->_behaviors->load($name, $options);

return $this;
}

/**
Expand All @@ -837,12 +839,14 @@ public function addBehavior($name, array $options = [])
* ```
*
* @param string $name The alias that the behavior was added with.
* @return void
* @return $this
* @see \Cake\ORM\Behavior
*/
public function removeBehavior($name)
{
$this->_behaviors->unload($name);

return $this;
}

/**
Expand Down Expand Up @@ -908,7 +912,7 @@ public function associations()
* keys are used the values will be treated as association aliases.
*
* @param array $params Set of associations to bind (indexed by association type)
* @return void
* @return $this
* @see \Cake\ORM\Table::belongsTo()
* @see \Cake\ORM\Table::hasOne()
* @see \Cake\ORM\Table::hasMany()
Expand All @@ -925,6 +929,8 @@ public function addAssociations(array $params)
$this->{$assocType}($associated, $options);
}
}

return $this;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -822,7 +822,8 @@ public function testAddAssociations()
];

$table = new Table(['table' => 'dates']);
$table->addAssociations($params);
$result = $table->addAssociations($params);
$this->assertSame($table, $result);

$associations = $table->associations();

Expand Down Expand Up @@ -1550,7 +1551,8 @@ public function testAddBehavior()
'table' => 'articles',
'behaviors' => $mock
]);
$table->addBehavior('Sluggable');
$result = $table->addBehavior('Sluggable');
$this->assertSame($table, $result);
}

/**
Expand All @@ -1561,8 +1563,8 @@ public function testAddBehavior()
public function testAddBehaviorDuplicate()
{
$table = new Table(['table' => 'articles']);
$this->assertNull($table->addBehavior('Sluggable', ['test' => 'value']));
$this->assertNull($table->addBehavior('Sluggable', ['test' => 'value']));
$this->assertSame($table, $table->addBehavior('Sluggable', ['test' => 'value']));
$this->assertSame($table, $table->addBehavior('Sluggable', ['test' => 'value']));
try {
$table->addBehavior('Sluggable', ['thing' => 'thing']);
$this->fail('No exception raised');
Expand All @@ -1589,7 +1591,8 @@ public function testRemoveBehavior()
'table' => 'articles',
'behaviors' => $mock
]);
$table->removeBehavior('Sluggable');
$result = $table->removeBehavior('Sluggable');
$this->assertSame($table, $result);
}

/**
Expand Down

0 comments on commit 8b1bcc4

Please sign in to comment.