From d47b5b14fc10b3c4f43165d700a9ee083119e997 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 19 Sep 2006 22:34:28 +0000 Subject: update prado-cli.php --- framework/3rdParty/PhpShell/php-shell-cmd.php | 28 +-- framework/prado-cli.php | 264 ++++++++++++++++++++++---- 2 files changed, 238 insertions(+), 54 deletions(-) (limited to 'framework') diff --git a/framework/3rdParty/PhpShell/php-shell-cmd.php b/framework/3rdParty/PhpShell/php-shell-cmd.php index 1b389686..968a3369 100644 --- a/framework/3rdParty/PhpShell/php-shell-cmd.php +++ b/framework/3rdParty/PhpShell/php-shell-cmd.php @@ -10,13 +10,13 @@ set_time_limit(0); * - set default error-handler * - add exec-hooks for the extensions * -* To keep the namespace clashing between shell and your program +* To keep the namespace clashing between shell and your program * as small as possible all public variables and functions from * the shell are prefixed with __shell: -* +* * - $__shell is the object of the shell * can be read, this is the shell object itself, don't touch it -* - $__shell_retval is the return value of the eval() before +* - $__shell_retval is the return value of the eval() before * it is printed * can't be read, but overwrites existing vars with this name * - $__shell_exception is the catched Exception on Warnings, Notices, .. @@ -45,7 +45,7 @@ else require_once(dirname(__FILE__)."/PHP/Shell/Extensions/InlineHelp.php"); require_once(dirname(__FILE__)."/PHP/Shell/Extensions/VerbosePrint.php"); require_once(dirname(__FILE__)."/PHP/Shell/Extensions/LoadScript.php"); -} +} /** * default error-handler @@ -65,7 +65,7 @@ else function __shell_default_error_handler($errno, $errstr, $errfile, $errline, $errctx) { ## ... what is this errno again ? if ($errno == 2048) return; - + throw new Exception(sprintf("%s:%d\r\n%s", $errfile, $errline, $errstr)); } @@ -82,19 +82,19 @@ $__shell_exts->registerExtensions(array( "exectime" => new PHP_Shell_Extensions_ExecutionTime(), "inlinehelp" => new PHP_Shell_Extensions_InlineHelp(), "verboseprint" => new PHP_Shell_Extensions_VerbosePrint(), - "loadscript" => new PHP_Shell_Extensions_LoadScript(), + "loadscript" => new PHP_Shell_Extensions_LoadScript() )); $f = << ->> use '?' to open the inline help +>> use '?' to open the inline help EOF; -printf($f, - $__shell->getVersion(), +printf($f, + $__shell->getVersion(), $__shell->hasReadline() ? ', with readline() support' : ''); unset($f); @@ -109,7 +109,7 @@ while($__shell->input()) { * * you can set your own autoloader by defining __autoload() before including * this file - * + * * @param string $classname name of the class */ @@ -133,7 +133,7 @@ while($__shell->input()) { $__shell_exts->exectime->startExecTime(); - $__shell_retval = eval($__shell->getCode()); + $__shell_retval = eval($__shell->getCode()); if (isset($__shell_retval)) { print $__shell_exts->colour->getColour("value"); @@ -151,7 +151,7 @@ while($__shell->input()) { print $__shell_exts->colour->getColour("exception"); printf('%s (code: %d) got thrown'.PHP_EOL, get_class($__shell_exception), $__shell_exception->getCode()); print $__shell_exception; - + $__shell->resetCode(); ## cleanup the variable namespace @@ -160,7 +160,7 @@ while($__shell->input()) { print $__shell_exts->colour->getColour("default"); $__shell_exts->exectime->stopTime(); if ($__shell_exts->exectime->isShow()) { - printf(" (parse: %.4fs, exec: %.4fs)", + printf(" (parse: %.4fs, exec: %.4fs)", $__shell_exts->exectime->getParseTime(), $__shell_exts->exectime->getExecTime() ); @@ -168,4 +168,4 @@ while($__shell->input()) { } print $__shell_exts->colour->getColour("reset"); - + diff --git a/framework/prado-cli.php b/framework/prado-cli.php index 36dc03ac..341ddb36 100755 --- a/framework/prado-cli.php +++ b/framework/prado-cli.php @@ -3,12 +3,20 @@ /** * Prado command line developer tools. + * + * @author Wei Zhuo + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2006 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ */ -if(!isset($_SERVER['argv'])) +if(!isset($_SERVER['argv']) || php_sapi_name()!=='cli') die('Must be run from the command line'); require_once(dirname(__FILE__).'/prado.php'); + +//stub application class class PradoShellApplication extends TApplication { public function run() @@ -16,51 +24,78 @@ class PradoShellApplication extends TApplication $this->initApplication(); } } -class DummyHttpRequest extends THttpRequest -{ - protected function resolveRequest() - { - } -} - -function __shell_print_var($shell,$var) -{ - if(!$shell->has_semicolon) - echo Prado::varDump($var); -} restore_exception_handler(); +//register action classes +PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateProject'); +PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateTests'); +PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLinePhpShell'); +PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineUnitTest'); + //run it; -PradoCommandLineInterpreter::run($_SERVER['argv']); +PradoCommandLineInterpreter::getInstance()->run($_SERVER['argv']); + +//run PHP shell if(count($_SERVER['argv']) > 1 && strtolower($_SERVER['argv'][1])==='shell') +{ + function __shell_print_var($shell,$var) + { + if(!$shell->has_semicolon) echo Prado::varDump($var); + } include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php'); +} + +/**************** END CONFIGURATION **********************/ + +/** + * PradoCommandLineInterpreter Class + * + * Command line interface, configures the action classes and dispatches the command actions. + * + * @author Wei Zhuo + * @version $Id$ + * @since 3.0.5 + */ class PradoCommandLineInterpreter { - protected $_actions=array( - 'PradoCommandLineCreateProject' => null, - 'PradoCommandLineCreateTests' => null, - 'PradoCommandLinePhpShell' => null - ); + /** + * @var array command action classes + */ + protected $_actions=array(); - public function __construct() + /** + * @param string action class name + */ + public function addActionClass($class) + { + $this->_actions[$class] = new $class; + } + + /** + * @return PradoCommandLineInterpreter static instance + */ + public static function getInstance() { - foreach(array_keys($this->_actions) as $class) - $this->_actions[$class] = new $class; + static $instance; + if(is_null($instance)) + $instance = new self; + return $instance; } - public static function run($args) + /** + * Dispatch the command line actions. + * @param array command line arguments + */ + public function run($args) { echo "Command line tools for Prado ".Prado::getVersion().".\n"; if(count($args) > 1) array_shift($args); - static $int; - if(is_null($int)) - $int = new self; $valid = false; - foreach($int->_actions as $class => $action) + foreach($this->_actions as $class => $action) { if($action->isValidAction($args)) { @@ -73,9 +108,12 @@ class PradoCommandLineInterpreter } } if(!$valid) - $int->printHelp(); + $this->printHelp(); } + /** + * Print command line help, default action. + */ public function printHelp() { echo "usage: php prado-cli.php action [optional]\n"; @@ -86,8 +124,20 @@ class PradoCommandLineInterpreter } } +/** + * Base class for command line actions. + * + * @author Wei Zhuo + * @version $Id$ + * @since 3.0.5 + */ abstract class PradoCommandLineAction { + /** + * Execute the action. + * @param array command line parameters + * @return boolean true if action was handled + */ abstract public function performAction($args); protected function createDirectory($dir, $mask) @@ -135,9 +185,31 @@ abstract class PradoCommandLineAction EOD; } -} + protected function initializePradoApplication($directory) + { + $app_dir = realpath($directory.'/protected/'); + if($app_dir !== false) + { + $app = new PradoShellApplication($app_dir); + $app->run(); + $dir = substr(str_replace(realpath('./'),'',$app_dir),1); + + echo '** Loaded Prado appplication in directory "'.$dir."\".\n"; + } + else + echo '** Unable to load Prado application in directory "'.$directory."\".\n"; + } + +} +/** + * Create a Prado project skeleton, including directories and files. + * + * @author Wei Zhuo + * @version $Id$ + * @since 3.0.5 + */ class PradoCommandLineCreateProject extends PradoCommandLineAction { protected $action = '-c'; @@ -223,6 +295,13 @@ EOD; } } +/** + * Creates test fixtures for a Prado application. + * + * @author Wei Zhuo + * @version $Id$ + * @since 3.0.5 + */ class PradoCommandLineCreateTests extends PradoCommandLineAction { protected $action = '-t'; @@ -292,6 +371,13 @@ $tester->run(new SimpleReporter()); } +/** + * Creates and run a Prado application in a PHP Shell. + * + * @author Wei Zhuo + * @version $Id$ + * @since 3.0.5 + */ class PradoCommandLinePhpShell extends PradoCommandLineAction { protected $action = 'shell'; @@ -302,27 +388,125 @@ class PradoCommandLinePhpShell extends PradoCommandLineAction public function performAction($args) { if(count($args) > 1) - $this->createAndRunPradoApp($args); + $this->initializePradoApplication($args[1]); + return true; + } +} + +/** + * Runs unit test cases. + * + * @author Wei Zhuo + * @version $Id$ + * @since 3.0.5 + */ +class PradoCommandLineUnitTest extends PradoCommandLineAction +{ + protected $action = 'test'; + protected $parameters = array('directory'); + protected $optional = array('testcase ...'); + protected $description = 'Runs all unit test cases in the given . Use [testcase] option to run specific test cases.'; + + protected $matches = array(); + + public function performAction($args) + { + $dir = realpath($args[1]); + if($dir !== false) + $this->runUnitTests($dir,$args); + else + echo '** Unable to find directory "'.$args[1]."\".\n"; return true; } - protected function createAndRunPradoApp($args) + protected function initializeTestRunner() + { + $TEST_TOOLS = realpath(dirname(__FILE__).'/../tests/test_tools/'); + + require_once($TEST_TOOLS.'/simpletest/unit_tester.php'); + require_once($TEST_TOOLS.'/simpletest/web_tester.php'); + require_once($TEST_TOOLS.'/simpletest/mock_objects.php'); + require_once($TEST_TOOLS.'/simpletest/reporter.php'); + } + + protected function runUnitTests($dir, $args) { - $app_dir = realpath($args[1].'/protected/'); + $app_dir = $this->getAppDir($dir); if($app_dir !== false) - { - $app = new PradoShellApplication($app_dir); - $app->setRequest(new DummyHttpRequest()); - $app->run(); - $dir = substr(str_replace(realpath('./'),'',$app_dir),1); + $this->initializePradoApplication($app_dir.'/../'); - echo '** Loaded Prado appplication in directory "'.$dir."\".\n"; + $this->initializeTestRunner(); + $test_dir = $this->getTestDir($dir); + if($test_dir !== false) + { + $test =$this->getUnitTestCases($test_dir,$args); + $running_dir = substr(str_replace(realpath('./'),'',$test_dir),1); + echo 'Running unit tests in directory "'.$running_dir."\":\n"; + $test->run(new TextReporter()); } else - echo '** Unable to load Prado application in directory "'.$args[1]."\".\n"; + { + $running_dir = substr(str_replace(realpath('./'),'',$dir),1); + echo '** Unable to find test directory "'.$running_dir.'/unit" or "'.$running_dir.'/tests/unit".'."\n"; + } + } + + protected function getAppDir($dir) + { + $app_dir = realpath($dir.'/protected'); + if($app_dir !== false) + return $app_dir; + return realpath($dir.'/../protected'); + } + + protected function getTestDir($dir) + { + $test_dir = realpath($dir.'/unit'); + if($test_dir !== false) + return $test_dir; + return realpath($dir.'/tests/unit/'); + } + + protected function getUnitTestCases($dir,$args) + { + $matches = null; + if(count($args) > 2) + $matches = array_slice($args,2); + $test=new GroupTest(' '); + $this->addTests($test,$dir,true,$matches); + $test->setLabel(implode(' ',$this->matches)); + return $test; } -} + protected function addTests($test,$path,$recursive=true,$match=null) + { + $dir=opendir($path); + while(($entry=readdir($dir))!==false) + { + if(is_file($path.'/'.$entry) && (preg_match('/[^\s]*test[^\s]*\.php/', strtolower($entry)))) + { + if($match==null||($match!=null && $this->hasMatch($match,$entry))) + $test->addTestFile($path.'/'.$entry); + } + if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($path.'/'.$entry) && $recursive) + $this->addTests($test,$path.'/'.$entry,$recursive,$match); + } + closedir($dir); + } + protected function hasMatch($match,$entry) + { + $file = strtolower(substr($entry,0,strrpos($entry,'.'))); + foreach($match as $m) + { + if(strtolower($m) === $file) + { + $this->matches[] = $m; + return true; + } + } + return false; + } +} ?> \ No newline at end of file -- cgit v1.2.3