修正异常处理和单元测试

This commit is contained in:
yunwuxin
2016-05-15 10:14:17 +08:00
parent 00b17411b6
commit f78ba76767
6 changed files with 36 additions and 48 deletions

View File

@@ -16,7 +16,7 @@ use think\console\helper\Formatter as FormatterHelper;
use think\console\helper\Process as ProcessHelper;
use think\console\helper\Question as QuestionHelper;
use think\console\helper\Set as HelperSet;
use think\console\Input;
use think\console\Input as ConsoleInput;
use think\console\input\Argument as InputArgument;
use think\console\input\Definition as InputDefinition;
use think\console\input\Option as InputOption;
@@ -74,7 +74,7 @@ class Console
*/
public function run()
{
$input = new Input();
$input = new ConsoleInput();
$output = new Output();
$this->configureIO($input, $output);
@@ -112,11 +112,11 @@ class Console
/**
* 执行指令
* @param Input $input
* @param ConsoleInput $input
* @param Output $output
* @return int
*/
public function doRun(Input $input, Output $output)
public function doRun(ConsoleInput $input, Output $output)
{
if (true === $input->hasParameterOption(['--version', '-V'])) {
$output->writeln($this->getLongVersion());
@@ -129,7 +129,7 @@ class Console
if (true === $input->hasParameterOption(['--help', '-h'])) {
if (!$name) {
$name = 'help';
$input = new Input(['help']);
$input = new ConsoleInput(['help']);
} else {
$this->wantHelps = true;
}
@@ -137,7 +137,7 @@ class Console
if (!$name) {
$name = $this->defaultCommand;
$input = new Input([$this->defaultCommand]);
$input = new ConsoleInput([$this->defaultCommand]);
}
$command = $this->find($name);
@@ -640,10 +640,10 @@ class Console
/**
* 配置基于用户的参数和选项的输入和输出实例。
* @param Input $input 输入实例
* @param ConsoleInput $input 输入实例
* @param Output $output 输出实例
*/
protected function configureIO(Input $input, Output $output)
protected function configureIO(ConsoleInput $input, Output $output)
{
if (true === $input->hasParameterOption(['--ansi'])) {
$output->setDecorated(true);
@@ -683,22 +683,22 @@ class Console
/**
* 执行指令
* @param Command $command 指令实例
* @param Input $input 输入实例
* @param ConsoleInput $input 输入实例
* @param Output $output 输出实例
* @return int
* @throws \Exception
*/
protected function doRunCommand(Command $command, Input $input, Output $output)
protected function doRunCommand(Command $command, ConsoleInput $input, Output $output)
{
return $command->run($input, $output);
}
/**
* 获取指令的基础名称
* @param Input $input
* @param ConsoleInput $input
* @return string
*/
protected function getCommandName(Input $input)
protected function getCommandName(ConsoleInput $input)
{
return $input->getFirstArgument();
}

View File

@@ -14,6 +14,7 @@ namespace think;
use think\exception\ErrorException;
use think\exception\Handle;
use think\exception\ThrowableError;
use think\console\Output as ConsoleOutput;
class Error
{
@@ -35,7 +36,7 @@ class Error
/**
* Exception Handler
* @param \Exception $e
* @param \Exception|\Throwable $e
*/
public static function appException($e)
{
@@ -44,8 +45,11 @@ class Error
}
self::getExceptionHandler()->report($e);
self::getExceptionHandler()->render($e)->send();
if(IS_CLI){
self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);
}else{
self::getExceptionHandler()->render($e)->send();
}
}
/**

View File

@@ -13,6 +13,8 @@ namespace think\exception;
use Exception;
use think\Config;
use think\Console;
use think\console\Output;
use think\Log;
use think\Response;
@@ -79,6 +81,15 @@ class Handle
}
}
/**
* @param $output
* @param Exception $e
*/
public function renderForConsole(Output $output, Exception $e)
{
(new Console)->renderException($e, $output);
}
/**
* @param HttpException $e
* @return \think\Response
@@ -110,8 +121,7 @@ class Handle
'code' => $this->getCode($exception),
'source' => $this->getSourceCode($exception),
'datas' => $this->getExtendData($exception),
'tables' => [
'tables' => [
'GET Data' => $_GET,
'POST Data' => $_POST,
'Files' => $_FILES,

View File

@@ -23,8 +23,7 @@ return [
],
// 别名定义
'alias' => [
'think\App' => MODE_PATH . 'console/App' . EXT,
'think\Error' => MODE_PATH . 'console/Error' . EXT
'think\App' => MODE_PATH . 'console/App' . EXT
],
// 配置文件
'config' => THINK_PATH . 'convention' . EXT

View File

@@ -1,26 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think;
class Error
{
/**
* 注册异常处理
* @return void
*/
public static function register()
{
ini_set("display_errors","Off");
//TODO
}
}

View File

@@ -18,6 +18,7 @@ namespace tests\thinkphp\library\think;
use ReflectionMethod;
use think\Exception as ThinkException;
use think\exception\HttpException;
class MyException extends ThinkException
{
@@ -29,9 +30,9 @@ class exceptionTest extends \PHPUnit_Framework_TestCase
public function testGetHttpStatus()
{
try {
throw new ThinkException("Error Processing Request", 1);
} catch (ThinkException $e) {
$this->assertEquals(500, $e->getHttpStatus());
throw new HttpException(404, "Error Processing Request");
} catch (HttpException $e) {
$this->assertEquals(404, $e->getStatusCode());
}
}