From fd3b4b2cd51ed5f2356156e8fa7d7cdd5d052564 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 26 Dec 2010 17:35:22 -0500 Subject: [PATCH] Adding Model::hasMethod() and tests. --- cake/libs/model/model.php | 17 ++++++++++++++ .../libs/model/model_integration.test.php | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index b55616028..396da7d4e 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1066,6 +1066,23 @@ class Model extends Object { return false; } +/** + * Check that a method is callable on a model. This will check both the model's own methods, its + * inherited methods and methods that could be callable through behaviors. + * + * @param string $method The method to be called. + * @return boolean True on method being callable. + */ + public function hasMethod($method) { + if (method_exists($this, $method)) { + return true; + } + if ($this->Behaviors->hasMethod($method)) { + return true; + } + return false; + } + /** * Returns true if the supplied field is a model Virtual Field * diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index fdd410bde..94714a276 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -2008,4 +2008,27 @@ class ModelIntegrationTest extends BaseModelTest { $expected = $db->name('Domain.DomainHandle'); $this->assertEqual($result, $expected); } + +/** + * test that model->hasMethod checks self and behaviors. + * + * @return void + */ + function testHasMethod() { + $Article = new Article(); + $Article->Behaviors = $this->getMock('BehaviorCollection'); + + $Article->Behaviors->expects($this->at(0)) + ->method('hasMethod') + ->will($this->returnValue(true)); + + $Article->Behaviors->expects($this->at(1)) + ->method('hasMethod') + ->will($this->returnValue(false)); + + $this->assertTrue($Article->hasMethod('find')); + + $this->assertTrue($Article->hasMethod('pass')); + $this->assertFalse($Article->hasMethod('fail')); + } }