Skip to content

Commit

Permalink
separate call into call and callFinder
Browse files Browse the repository at this point in the history
Having explicit methods should aide debugging - and make the intention
clear. also the finder index is indexed by type, not find method (e.g.
"list", not "findlist").
  • Loading branch information
AD7six committed Nov 10, 2013
1 parent 9fdc819 commit 1c7924e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
22 changes: 18 additions & 4 deletions Cake/ORM/BehaviorRegistry.php
Expand Up @@ -196,7 +196,7 @@ public function hasFinder($method) {
}

/**
* Invoke a method or finder on a behavior.
* Invoke a method on a behavior.
*
* @param string $method The method to invoke.
* @param array $args The arguments you want to invoke the method with.
Expand All @@ -210,12 +210,26 @@ public function call($method, array $args = []) {
return call_user_func_array([$this->_loaded[$behavior], $callMethod], $args);
}

if ($this->hasFinder($method)) {
list($behavior, $callMethod) = $this->_finderMap[$method];
throw new Error\Exception(__d('cake_dev', 'Cannot call "%s" it does not belong to any attached behaviors.', $method));
}

/**
* Invoke a finder on a behavior.
*
* @param string $type The finder type to invoke.
* @param array $args The arguments you want to invoke the method with.
* @return mixed The return value depends on the underlying behavior method.
* @throws Cake\Error\Exception When the method is unknown.
*/
public function callFinder($type, array $args = []) {
$type = strtolower($type);

if ($this->hasFinder($type)) {
list($behavior, $callMethod) = $this->_finderMap[$type];
return call_user_func_array([$this->_loaded[$behavior], $callMethod], $args);
}

throw new Error\Exception(__d('cake_dev', 'Cannot call "%s" it does not belong to any attached behaviors.', $method));
throw new Error\Exception(__d('cake_dev', 'Cannot call finder "%s" it does not belong to any attached behaviors.', $type));
}

}
30 changes: 26 additions & 4 deletions Cake/Test/TestCase/ORM/BehaviorRegistryTest.php
Expand Up @@ -179,10 +179,6 @@ public function testCall() {
$this->Behaviors->load('Sluggable');
$result = $this->Behaviors->call('slugify', ['some value']);
$this->assertEquals('some_value', $result);

$query = $this->getMock('Cake\ORM\Query', [], [null, null]);
$result = $this->Behaviors->call('noSlug', [$query]);
$this->assertEquals($query, $result);
}

/**
Expand All @@ -196,4 +192,30 @@ public function testCallError() {
$this->Behaviors->call('nope');
}

/**
* test call finder
*
* @return void
*/
public function testCallFinder() {
$this->Behaviors->load('Sluggable');
$result = $this->Behaviors->call('slugify', ['some value']);
$this->assertEquals('some_value', $result);

$query = $this->getMock('Cake\ORM\Query', [], [null, null]);
$result = $this->Behaviors->callFinder('noSlug', [$query]);
$this->assertEquals($query, $result);
}

/**
* Test errors on unknown methods.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage Cannot call finder "nope"
*/
public function testCallFinderError() {
$this->Behaviors->load('Sluggable');
$this->Behaviors->callFinder('nope');
}

}

0 comments on commit 1c7924e

Please sign in to comment.