mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding Shell::hasMethod and tests for it.
This commit is contained in:
parent
18c5a62445
commit
cd18c8214c
2 changed files with 39 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue