Updating API shell to use reflection as the regular expression was not working with visibility keywords.

Updating tests for changes in output.
Updating tests to work with PHPUnit.
This commit is contained in:
mark_story 2010-05-22 22:09:07 -04:00
parent 1955b0fb1f
commit 89a8ef3871
2 changed files with 61 additions and 59 deletions

View file

@ -19,6 +19,7 @@
* @since CakePHP(tm) v 1.2.0.5012
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', 'File');
/**
* API shell to show method signatures of CakePHP core classes.
@ -91,7 +92,7 @@ class ApiShell extends Shell {
$this->_stop();
}
$parsed = $this->__parseClass($path . $file .'.php');
$parsed = $this->__parseClass($path . $file .'.php', $class);
if (!empty($parsed)) {
if (isset($this->params['m'])) {
@ -116,7 +117,7 @@ class ApiShell extends Shell {
while ($number = strtolower($this->in(__('Select a number to see the more information about a specific method. q to quit. l to list.'), null, 'q'))) {
if ($number === 'q') {
$this->out(__('Done'));
$this->_stop();
return $this->_stop();
}
if ($number === 'l') {
@ -180,29 +181,34 @@ class ApiShell extends Shell {
* @return array Methods and signatures indexed by method name
* @access private
*/
function __parseClass($path) {
function __parseClass($path, $class) {
$parsed = array();
$File = new File($path);
if (!$File->exists()) {
$this->err(sprintf(__('%s could not be found'), $File->name));
$this->_stop();
if (!include_once($path)) {
$this->err(sprintf(__('%s could not be found'), $path));
}
$reflection = new ReflectionClass($class);
$contents = $File->read();
if (preg_match_all('%(/\\*\\*[\\s\\S]*?\\*/)(\\s+function\\s+\\w+)(\\(.*\\))%', $contents, $result, PREG_PATTERN_ORDER)) {
foreach ($result[2] as $key => $method) {
$method = str_replace('function ', '', trim($method));
if (strpos($method, '__') === false && $method[0] != '_') {
$parsed[$method] = array(
'comment' => str_replace(array('/*', '*/', '*'), '', trim($result[1][$key])),
'method' => $method,
'parameters' => trim($result[3][$key])
);
}
foreach ($reflection->getMethods() as $method) {
if (!$method->isPublic() || strpos($method->getName(), '_') === 0) {
continue;
}
if ($method->getDeclaringClass()->getName() != $class) {
continue;
}
$args = array();
foreach ($method->getParameters() as $param) {
$paramString = '$' . $param->getName();
if ($param->isDefaultValueAvailable()) {
$paramString .= ' = ' . str_replace("\n", '', var_export($param->getDefaultValue(), true));
}
$args[] = $paramString;
}
$parsed[$method->getName()] = array(
'comment' => str_replace(array('/*', '*/', '*'), '', $method->getDocComment()),
'method' => $method->getName(),
'parameters' => '(' . implode(', ', $args) . ')'
);
}
ksort($parsed);
return $parsed;

View file

@ -34,15 +34,6 @@ if (!class_exists('ApiShell')) {
require CAKE . 'console' . DS . 'libs' . DS . 'api.php';
}
Mock::generatePartial(
'ShellDispatcher', 'ApiShellMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
Mock::generatePartial(
'ApiShell', 'MockApiShell',
array('in', 'out', 'createFile', 'hr', '_stop')
);
/**
* ApiShellTest class
*
@ -52,14 +43,20 @@ Mock::generatePartial(
class ApiShellTest extends CakeTestCase {
/**
* setUp method
* startTest method
*
* @return void
*/
public function startTest() {
$this->Dispatcher =& new ApiShellMockShellDispatcher();
$this->Shell =& new MockApiShell($this->Dispatcher);
$this->Shell->Dispatch =& $this->Dispatcher;
$this->Dispatcher = $this->getMock(
'ShellDispather',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment', 'dispatch')
);
$this->Shell = $this->getMock(
'ApiShell',
array('in', 'out', 'createFile', 'hr', '_stop'),
array(&$this->Dispatcher)
);
}
/**
@ -77,34 +74,33 @@ class ApiShellTest extends CakeTestCase {
* @return void
*/
public function testMethodNameDetection () {
$this->Shell->setReturnValueAt(0, 'in', 'q');
$this->Shell->expectAt(0, 'out', array('Controller'));
$this->Shell->expects($this->any())->method('in')->will($this->returnValue('q'));
$this->Shell->expects($this->at(0))->method('out')->with('Controller');
$expected = array(
array(
'1. afterFilter()',
'2. beforeFilter()',
'3. beforeRender()',
'4. constructClasses()',
'5. disableCache()',
'6. flash($message, $url, $pause = 1, $layout = \'flash\')',
'7. header($status)',
'8. httpCodes($code = null)',
'9. isAuthorized()',
'10. loadModel($modelClass = null, $id = null)',
'11. paginate($object = null, $scope = array(), $whitelist = array())',
'12. postConditions($data = array(), $op = null, $bool = \'AND\', $exclusive = false)',
'13. redirect($url, $status = null, $exit = true)',
'14. referer($default = null, $local = false)',
'15. render($action = null, $layout = null, $file = null)',
'16. set($one, $two = null)',
'17. setAction($action)',
'18. shutdownProcess()',
'19. startupProcess()',
'20. validate()',
'21. validateErrors()'
)
'1. afterFilter()',
'2. beforeFilter()',
'3. beforeRender()',
'4. constructClasses()',
'5. disableCache()',
'6. flash($message, $url, $pause = 1, $layout = \'flash\')',
'7. header($status)',
'8. httpCodes($code = NULL)',
'9. isAuthorized()',
'10. loadModel($modelClass = NULL, $id = NULL)',
'11. paginate($object = NULL, $scope = array (), $whitelist = array ())',
'12. postConditions($data = array (), $op = NULL, $bool = \'AND\', $exclusive = false)',
'13. redirect($url, $status = NULL, $exit = true)',
'14. referer($default = NULL, $local = false)',
'15. render($action = NULL, $layout = NULL, $file = NULL)',
'16. set($one, $two = NULL)',
'17. setAction($action)',
'18. shutdownProcess()',
'19. startupProcess()',
'20. validate()',
'21. validateErrors()'
);
$this->Shell->expectAt(1, 'out', $expected);
$this->Shell->expects($this->at(2))->method('out')->with($expected);
$this->Shell->args = array('controller');
$this->Shell->paths['controller'] = CAKE_CORE_INCLUDE_PATH . DS . LIBS . 'controller' . DS;