From 769da1a7c87f993968efda28314e8ba76fe5c9fe Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 26 Dec 2010 14:25:57 -0500 Subject: [PATCH] Adding basic BehaviorCollection::hasMethod implementation. Tests added. --- cake/libs/model/behavior_collection.php | 19 +++++++++ .../libs/model/behavior_collection.test.php | 41 ++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/cake/libs/model/behavior_collection.php b/cake/libs/model/behavior_collection.php index da9970560..848894cb9 100644 --- a/cake/libs/model/behavior_collection.php +++ b/cake/libs/model/behavior_collection.php @@ -237,4 +237,23 @@ class BehaviorCollection extends ObjectCollection { 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; + } + } diff --git a/cake/tests/cases/libs/model/behavior_collection.test.php b/cake/tests/cases/libs/model/behavior_collection.test.php index efe549297..4b4ab980d 100644 --- a/cake/tests/cases/libs/model/behavior_collection.test.php +++ b/cake/tests/cases/libs/model/behavior_collection.test.php @@ -340,7 +340,16 @@ class TestBehavior extends ModelBehavior { * * @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)); } + +/** + * 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')); + } }