Skip to content

Commit

Permalink
Adding Shell::hasMethod and tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 18c5a62 commit cd18c82
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cake/console/libs/shell.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -305,7 +305,21 @@ public function hasTask($task) {
* @return boolean * @return boolean
*/ */
public function hasMethod($name) { 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;
}
} }


/** /**
Expand Down
24 changes: 24 additions & 0 deletions cake/tests/cases/console/libs/shell.test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ class TestShell extends Shell {
protected function _stop($status = 0) { protected function _stop($status = 0) {
$this->stopped = $status; $this->stopped = $status;
} }

public function do_something() {

}

public function _secret() {

}

protected function no_access() {

}
} }


/** /**
Expand Down Expand Up @@ -578,4 +590,16 @@ function testHasTask() {
$this->assertTrue($this->Shell->hasTask('db_config')); $this->assertTrue($this->Shell->hasTask('db_config'));
$this->assertTrue($this->Shell->hasTask('DbConfig')); $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');
}
} }

0 comments on commit cd18c82

Please sign in to comment.