Adding Model::hasMethod() and tests.

This commit is contained in:
mark_story 2010-12-26 17:35:22 -05:00
parent c5fa93b0fb
commit fd3b4b2cd5
2 changed files with 40 additions and 0 deletions

View file

@ -1066,6 +1066,23 @@ class Model extends Object {
return false; 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 * Returns true if the supplied field is a model Virtual Field
* *

View file

@ -2008,4 +2008,27 @@ class ModelIntegrationTest extends BaseModelTest {
$expected = $db->name('Domain.DomainHandle'); $expected = $db->name('Domain.DomainHandle');
$this->assertEqual($result, $expected); $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'));
}
} }