Adding Shell::hasMethod and tests for it.

This commit is contained in:
mark_story 2010-10-09 21:52:12 -04:00
parent 18c5a62445
commit cd18c8214c
2 changed files with 39 additions and 1 deletions

View file

@ -305,7 +305,21 @@ class Shell extends Object {
* @return boolean
*/
public function hasMethod($name) {
if (empty($this->_reflection)) {
$this->_reflection = new ReflectionClass($this);
}
try {
$method = $this->_reflection->getMethod($name);
if (!$method->isPublic() || substr($name, 0, 1) === '_') {
return false;
}
if ($method->getDeclaringClass() != $this->_reflection) {
return false;
}
return true;
} catch (ReflectionException $e) {
return false;
}
}
/**

View file

@ -57,6 +57,18 @@ class TestShell extends Shell {
protected function _stop($status = 0) {
$this->stopped = $status;
}
public function do_something() {
}
public function _secret() {
}
protected function no_access() {
}
}
/**
@ -578,4 +590,16 @@ class ShellTest extends CakeTestCase {
$this->assertTrue($this->Shell->hasTask('db_config'));
$this->assertTrue($this->Shell->hasTask('DbConfig'));
}
/**
* test the hasMethod
*
* @return void
*/
function testHasMethod() {
$this->assertTrue($this->Shell->hasMethod('do_something'));
$this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
$this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
$this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
}
}