mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-07 12:36:25 +00:00
Adding basic BehaviorCollection::hasMethod implementation. Tests added.
This commit is contained in:
parent
edf567b9f9
commit
769da1a7c8
2 changed files with 59 additions and 1 deletions
|
@ -237,4 +237,23 @@ class BehaviorCollection extends ObjectCollection {
|
||||||
return $this->__methods;
|
return $this->__methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check to see if a behavior in this collection implements the provided method. Will
|
||||||
|
* also check mappedMethods.
|
||||||
|
*
|
||||||
|
* @param string $method The method to find.
|
||||||
|
* @return boolean Method was found.
|
||||||
|
*/
|
||||||
|
public function hasMethod($method) {
|
||||||
|
if (isset($this->__methods[$method])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
foreach ($this->__mappedMethods as $pattern => $target) {
|
||||||
|
if (preg_match($pattern . 'i', $method)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -340,7 +340,16 @@ class TestBehavior extends ModelBehavior {
|
||||||
*
|
*
|
||||||
* @package cake.tests.cases.libs.model
|
* @package cake.tests.cases.libs.model
|
||||||
*/
|
*/
|
||||||
class Test2Behavior extends TestBehavior{
|
class Test2Behavior extends TestBehavior {
|
||||||
|
public $mapMethods = array('/mappingRobot(\w+)/' => 'mapped');
|
||||||
|
|
||||||
|
function resolveMethod($model, $stuff) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapped($model, $method, $query) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1116,4 +1125,34 @@ class BehaviorCollectionTest extends CakeTestCase {
|
||||||
|
|
||||||
$Sample->Behaviors->trigger('beforeTest', array(&$Sample));
|
$Sample->Behaviors->trigger('beforeTest', array(&$Sample));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that hasMethod works with basic functions.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testHasMethodBasic() {
|
||||||
|
$Sample = new Sample();
|
||||||
|
$Collection = new BehaviorCollection();
|
||||||
|
$Collection->init('Sample', array('Test', 'Test2'));
|
||||||
|
|
||||||
|
$this->assertTrue($Collection->hasMethod('testMethod'));
|
||||||
|
$this->assertTrue($Collection->hasMethod('resolveMethod'));
|
||||||
|
|
||||||
|
$this->assertFalse($Collection->hasMethod('No method'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that hasMethod works with mapped methods.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testHasMethodMappedMethods() {
|
||||||
|
$Sample = new Sample();
|
||||||
|
$Collection = new BehaviorCollection();
|
||||||
|
$Collection->init('Sample', array('Test', 'Test2'));
|
||||||
|
|
||||||
|
$this->assertTrue($Collection->hasMethod('look for the remote in the couch'));
|
||||||
|
$this->assertTrue($Collection->hasMethod('mappingRobotOnTheRoof'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue