From cd18c8214c64858092def81e295b028ac669d33c Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 9 Oct 2010 21:52:12 -0400 Subject: [PATCH] Adding Shell::hasMethod and tests for it. --- cake/console/libs/shell.php | 16 ++++++++++++- cake/tests/cases/console/libs/shell.test.php | 24 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index 90cfc6711..35f6e5b24 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -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; + } } /** diff --git a/cake/tests/cases/console/libs/shell.test.php b/cake/tests/cases/console/libs/shell.test.php index fc449b34c..fd91aa5e9 100644 --- a/cake/tests/cases/console/libs/shell.test.php +++ b/cake/tests/cases/console/libs/shell.test.php @@ -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'); + } }