diff --git a/library/think/App.php b/library/think/App.php index e7deb066..bb4dfae1 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -112,7 +112,7 @@ class App $data = $dispatch['response']; break; default: - throw new Exception('dispatch type not support', 10008); + throw new \InvalidArgumentException('dispatch type not support'); } } catch (HttpResponseException $exception) { $data = $exception->getResponse(); @@ -200,7 +200,7 @@ class App } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } else { - throw new Exception('method param miss:' . $name, 10004); + throw new \InvalidArgumentException('method param miss:' . $name); } } // 全局过滤 @@ -261,7 +261,7 @@ class App // 执行操作 if (!preg_match('/^[A-Za-z](\/|\.|\w)*$/', $controller)) { // 安全检测 - throw new Exception('illegal controller name:' . $controller, 10000); + throw new \InvalidArgumentException('illegal controller name:' . $controller); } // 设置当前请求的模块、控制器、操作 @@ -361,7 +361,7 @@ class App * @param \think\Request $request * @param array $config * @return array - * @throws Exception + * @throws HttpException */ public static function route($request, array $config) { @@ -384,7 +384,7 @@ class App $result = Route::check($request, $path, $depr, !IS_CLI ? $config['url_domain_deploy'] : false); if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { // 路由无效 - throw new HttpException(404, 'Not Found'); + throw new HttpException(404, 'Route Not Found'); } } if (false === $result) { diff --git a/library/think/Controller.php b/library/think/Controller.php index 03af3da6..dbb4aeae 100644 --- a/library/think/Controller.php +++ b/library/think/Controller.php @@ -14,6 +14,7 @@ namespace think; \think\Loader::import('controller/Jump', TRAIT_PATH, EXT); use think\Exception; +use think\Exception\ValidateException; use think\Request; use think\View; @@ -190,7 +191,7 @@ class Controller if (!$v->check($data)) { if ($this->failException) { - throw new Exception($v->getError()); + throw new ValidateException($v->getError()); } else { return $v->getError(); } diff --git a/library/think/Exception.php b/library/think/Exception.php index bc4cbdb2..ac648764 100644 --- a/library/think/Exception.php +++ b/library/think/Exception.php @@ -11,10 +11,6 @@ namespace think; -/** - * ThinkPHP核心异常类 - * 所有系统异常必须继承该类 - */ class Exception extends \Exception { diff --git a/library/think/Input.php b/library/think/Input.php index 15e8f880..8bb5334d 100644 --- a/library/think/Input.php +++ b/library/think/Input.php @@ -466,7 +466,7 @@ class Input if (is_scalar($data)) { $data = (string) $data; } else { - throw new Exception('变量类型不允许:' . gettype($data)); + throw new \InvalidArgumentException('变量类型不允许:' . gettype($data)); } } } diff --git a/library/think/Loader.php b/library/think/Loader.php index 451fb3c4..7972ade4 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -12,6 +12,7 @@ namespace think; use think\exception\HttpException; +use think\exception\ClassNotFoundException; use think\Request; class Loader @@ -269,6 +270,7 @@ class Loader * @param bool $appendSuffix 是否添加类名后缀 * @param string $common 公共模块名 * @return Object + * @throws ClassNotFoundException */ public static function model($name = '', $layer = 'model', $appendSuffix = false, $common = 'common') { @@ -289,7 +291,7 @@ class Loader if (class_exists($class)) { $model = new $class(); } else { - throw new Exception('class [ ' . $class . ' ] not exists', 10001); + throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); } } $_model[$name . $layer] = $model; @@ -303,6 +305,7 @@ class Loader * @param bool $appendSuffix 是否添加类名后缀 * @param string $empty 空控制器名称 * @return Object|false + * @throws ClassNotFoundException */ public static function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '') { @@ -324,7 +327,7 @@ class Loader } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { return new $emptyClass(Request::instance()); } else { - throw new HttpException(404, 'class [ ' . $class . ' ] not exists'); + throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); } } @@ -335,6 +338,7 @@ class Loader * @param bool $appendSuffix 是否添加类名后缀 * @param string $common 公共模块名 * @return Object|false + * @throws ClassNotFoundException */ public static function validate($name = '', $layer = 'validate', $appendSuffix = false, $common = 'common') { @@ -360,7 +364,7 @@ class Loader if (class_exists($class)) { $validate = new $class; } else { - throw new Exception('class [ ' . $class . ' ] not exists', 10001); + throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); } } $_instance[$name . $layer] = $validate; @@ -409,7 +413,7 @@ class Loader * @param string $method 类的静态方法名 * * @return mixed - * @throws Exception + * @throws ClassNotFoundException */ public static function instance($class, $method = '') { @@ -424,7 +428,7 @@ class Loader $_instance[$identify] = $o; } } else { - throw new Exception('class not exist :' . $class, 10007); + throw new ClassNotFoundException('class not exist :' . $class); } } return $_instance[$identify]; diff --git a/library/think/Model.php b/library/think/Model.php index 4815540d..0b86724f 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -15,6 +15,7 @@ use think\Cache; use think\Db; use think\db\Query; use think\Exception; +use think\Exception\ValidateException; use think\Loader; use think\model\Relation; use think\paginator\Collection as PaginatorCollection; @@ -773,7 +774,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (!$validate->check($data)) { $this->error = $validate->getError(); if ($this->failException) { - throw new Exception($this->error); + throw new ValidateException($this->error); } else { return false; } diff --git a/library/think/Paginator.php b/library/think/Paginator.php index 37433d35..ff243509 100644 --- a/library/think/Paginator.php +++ b/library/think/Paginator.php @@ -143,7 +143,7 @@ abstract class Paginator public function total() { if ($this->simple) { - throw new \Exception('简洁模式下不能获取数据总数'); + throw new \DomainException('简洁模式下不能获取数据总数'); } return $this->total; } @@ -161,7 +161,7 @@ abstract class Paginator public function lastPage() { if ($this->simple) { - throw new \Exception('简洁模式下不能获取最后一页'); + throw new \DomainException('简洁模式下不能获取最后一页'); } return $this->lastPage; } diff --git a/library/think/Response.php b/library/think/Response.php index 3a2c9034..c9c479b3 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -100,7 +100,7 @@ class Response * @access public * @param mixed $data 数据 * @return mixed - * @throws Exception + * @throws \InvalidArgumentException */ public function send($data = null) { @@ -137,7 +137,7 @@ class Response if (is_scalar($data)) { echo $data; } elseif (!is_null($data)) { - throw new Exception('不支持的数据类型输出:' . gettype($data)); + throw new \InvalidArgumentException('不支持的数据类型输出:' . gettype($data)); } if (function_exists('fastcgi_finish_request')) { diff --git a/library/think/Session.php b/library/think/Session.php index 17212b89..193165c9 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -11,6 +11,8 @@ namespace think; +use think\exception\ClassNotFoundException; + class Session { @@ -92,7 +94,7 @@ class Session // 检查驱动类 if (!class_exists($class) || !session_set_save_handler(new $class($config))) { - throw new \think\Exception('error session handler', 11700); + throw new ClassNotFoundException('error session handler'); } } if ($isDoStart) { diff --git a/library/think/Template.php b/library/think/Template.php index f13b5f96..ab14b12c 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -11,6 +11,8 @@ namespace think; +use think\exception\TemplateNotFoundException; + /** * ThinkPHP分离出来的模板引擎 * 支持XML标签和普通标签的模板解析 @@ -1066,7 +1068,7 @@ class Template $this->includeFile[$template] = filemtime($template); return $template; } else { - throw new Exception('template not exist:' . $template, 10700); + throw new TemplateNotFoundException('template not exist:' . $template); } } diff --git a/library/think/Validate.php b/library/think/Validate.php index 6b52c6c4..0eb1c8f7 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -1115,7 +1115,7 @@ class Validate if (method_exists($class, $method)) { return call_user_func_array([$class, $method], $params); } else { - throw new Exception(__CLASS__ . ':' . $method . ' method not exist'); + throw new \BadMethodCallException(__CLASS__ . ':' . $method . ' method not exist'); } } } diff --git a/library/think/cache/driver/Apc.php b/library/think/cache/driver/Apc.php index b622762a..95b27d14 100644 --- a/library/think/cache/driver/Apc.php +++ b/library/think/cache/driver/Apc.php @@ -36,7 +36,7 @@ class Apc public function __construct($options = []) { if (!function_exists('apc_cache_info')) { - throw new Exception('_NOT_SUPPERT_:Apc'); + throw new \BadFunctionCallException('not support Apc'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php index 370c6415..981436fd 100644 --- a/library/think/cache/driver/Memcache.php +++ b/library/think/cache/driver/Memcache.php @@ -30,12 +30,12 @@ class Memcache * 架构函数 * @param array $options 缓存参数 * @access public - * @throws Exception + * @throws \BadFunctionCallException */ public function __construct($options = []) { if (!extension_loaded('memcache')) { - throw new Exception('_NOT_SUPPERT_:memcache'); + throw new \BadFunctionCallException('not support memcache'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index e413b06f..b19b0c0c 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -33,7 +33,7 @@ class Memcached public function __construct($options = []) { if (!extension_loaded('memcached')) { - throw new Exception('_NOT_SUPPERT_:memcached'); + throw new \BadFunctionCallException('not support memcached'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index 403229ff..d872f5a7 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -42,7 +42,7 @@ class Redis public function __construct($options = []) { if (!extension_loaded('redis')) { - throw new Exception('_NOT_SUPPERT_:redis'); + throw new \BadFunctionCallException('not support redis'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Redisd.php b/library/think/cache/driver/Redisd.php index d32dbaca..ad59cf90 100644 --- a/library/think/cache/driver/Redisd.php +++ b/library/think/cache/driver/Redisd.php @@ -83,7 +83,7 @@ class Redisd public function __construct($options = []) { if (!extension_loaded('redis')) { - throw new Exception('_NOT_SUPPERT_:redis'); + throw new \BadFunctionCallException('not support redis'); } $this->options = $options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Sae.php b/library/think/cache/driver/Sae.php index 03b70735..c1cdda77 100644 --- a/library/think/cache/driver/Sae.php +++ b/library/think/cache/driver/Sae.php @@ -38,7 +38,7 @@ class Sae public function __construct($options = []) { if (!function_exists('memcache_init')) { - throw new Exception('请在SAE平台上运行代码。'); + throw new \BadFunctionCallException('请在SAE平台上运行代码。'); } $this->handler = memcache_init(); if (!$this->handler) { diff --git a/library/think/cache/driver/Sqlite.php b/library/think/cache/driver/Sqlite.php index 6144a366..ac903fdc 100644 --- a/library/think/cache/driver/Sqlite.php +++ b/library/think/cache/driver/Sqlite.php @@ -32,13 +32,13 @@ class Sqlite implements CacheInterface /** * 架构函数 * @param array $options 缓存参数 - * @throws Exception + * @throws \BadFunctionCallException * @access public */ public function __construct($options = []) { if (!extension_loaded('sqlite')) { - throw new Exception('_NOT_SUPPERT_:sqlite'); + throw new \BadFunctionCallException('not support sqlite'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Wincache.php b/library/think/cache/driver/Wincache.php index 66972a48..0717ef05 100644 --- a/library/think/cache/driver/Wincache.php +++ b/library/think/cache/driver/Wincache.php @@ -34,7 +34,7 @@ class Wincache public function __construct($options = []) { if (!function_exists('wincache_ucache_info')) { - throw new Exception('_NOT_SUPPERT_:WinCache'); + throw new \BadFunctionCallException('not support WinCache'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/cache/driver/Xcache.php b/library/think/cache/driver/Xcache.php index 3fea3296..891e6212 100644 --- a/library/think/cache/driver/Xcache.php +++ b/library/think/cache/driver/Xcache.php @@ -29,12 +29,12 @@ class Xcache * 架构函数 * @param array $options 缓存参数 * @access public - * @throws Exception + * @throws \BadFunctionCallException */ public function __construct($options = []) { if (!function_exists('xcache_info')) { - throw new Exception('_NOT_SUPPERT_:Xcache'); + throw new \BadFunctionCallException('not support Xcache'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index b913c0df..f77df8b7 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -18,8 +18,8 @@ use think\Db; use think\db\Query; use think\Debug; use think\Exception; -use think\exception\DbBindParamException; use think\exception\PDOException; +use think\db\exception\BindParamException; use think\Log; abstract class Connection @@ -326,7 +326,7 @@ abstract class Connection * @param boolean $master 是否在主服务器读操作 * @param bool|string $class 指定返回的数据集对象 * @return mixed - * @throws DbBindParamException + * @throws BindParamException * @throws PDOException */ public function query($sql, $bind = [], $fetch = false, $master = false, $class = false) @@ -375,7 +375,7 @@ abstract class Connection * @param boolean $getLastInsID 是否获取自增ID * @param string $sequence 自增序列名 * @return int - * @throws DbBindParamException + * @throws BindParamException * @throws PDOException */ public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null) @@ -465,7 +465,7 @@ abstract class Connection $result = $this->PDOStatement->bindValue($param, $val); } if (!$result) { - throw new DbBindParamException( + throw new BindParamException( "Error occurred when binding parameters '{$param}'", $this->config, $this->queryStr, diff --git a/library/think/exception/DbException.php b/library/think/db/DbException.php similarity index 97% rename from library/think/exception/DbException.php rename to library/think/db/DbException.php index 11c1c932..08c2a398 100644 --- a/library/think/exception/DbException.php +++ b/library/think/db/DbException.php @@ -9,7 +9,7 @@ // | Author: 麦当苗儿 // +---------------------------------------------------------------------- -namespace think\exception; +namespace think\db; use think\Exception; diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 43cfd300..2bad6e8e 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -21,6 +21,8 @@ use think\db\Connection; use think\Exception; use think\exception\DbException; use think\exception\PDOException; +use think\db\exception\ModelNotFoundException; +use think\db\exception\DataNotFoundException; use think\Loader; use think\Model; use think\model\Relation; @@ -379,6 +381,9 @@ class Query if (!empty($this->options['cache'])) { // 判断查询缓存 $cache = $this->options['cache']; + if (empty($this->options['table'])) { + $this->options['table'] = $this->getTable(); + } $key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options)); $result = Cache::get($key); } @@ -412,6 +417,9 @@ class Query if (!empty($this->options['cache'])) { // 判断查询缓存 $cache = $this->options['cache']; + if (empty($this->options['table'])) { + $this->options['table'] = $this->getTable(); + } $guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options)); $result = Cache::get($guid); } @@ -1775,7 +1783,11 @@ class Query } } } elseif (!empty($options['fail'])) { - throw new DbException('Data not Found', $options, $sql); + if(!empty($this->model)){ + throw new ModelNotFoundException('Data not Found', $this->model, $options); + }else{ + throw new DataNotFoundException('Data not Found', $options['table'], $options); + } } else { $data = false; } diff --git a/library/think/exception/DbBindParamException.php b/library/think/db/exception/BindParamException.php similarity index 90% rename from library/think/exception/DbBindParamException.php rename to library/think/db/exception/BindParamException.php index f39c39ac..2e998875 100644 --- a/library/think/exception/DbBindParamException.php +++ b/library/think/db/exception/BindParamException.php @@ -9,14 +9,14 @@ // | Author: 麦当苗儿 // +---------------------------------------------------------------------- -namespace think\exception; +namespace think\db\exception; -use think\exception\DbException; +use think\db\Exception; /** * PDO参数绑定异常 */ -class DbBindParamException extends DbException +class BindParamException extends Exception { /** diff --git a/library/think/db/exception/DataNotFoundException.php b/library/think/db/exception/DataNotFoundException.php new file mode 100644 index 00000000..27569e90 --- /dev/null +++ b/library/think/db/exception/DataNotFoundException.php @@ -0,0 +1,35 @@ + +// +---------------------------------------------------------------------- + +namespace think\db\exception; + +use think\db\DbException; + +class DataNotFoundException extends DbException +{ + protected $table; + + /** + * DbException constructor. + * @param string $message + * @param string $table + * @param array $config + */ + public function __construct($message, $table = '', Array $config = []) + { + $this->message = $message; + $this->table = $table; + + $this->setData('Database Config', $config); + } + + +} diff --git a/library/think/db/exception/ModelNotFoundException.php b/library/think/db/exception/ModelNotFoundException.php new file mode 100644 index 00000000..b12db4df --- /dev/null +++ b/library/think/db/exception/ModelNotFoundException.php @@ -0,0 +1,38 @@ + +// +---------------------------------------------------------------------- + +namespace think\db\exception; + +use think\db\DbException; + +class ModelNotFoundException extends DbException +{ + protected $model; + + /** + * 构造方法 + * @param string $message + * @param string $model + */ + public function __construct($message, $model = '', Array $config = []) + { + $this->message = $message; + $this->model = $model; + + $this->setData('Database Config', $config); + } + + public function getModel() + { + return $this->model; + } + +} diff --git a/library/think/exception/ClassNotFoundException.php b/library/think/exception/ClassNotFoundException.php new file mode 100644 index 00000000..a02d1cb1 --- /dev/null +++ b/library/think/exception/ClassNotFoundException.php @@ -0,0 +1,16 @@ + +// +---------------------------------------------------------------------- + +namespace think\exception; + +class ClassNotFoundException extends \RuntimeException +{ +} \ No newline at end of file diff --git a/library/think/exception/TemplateNotFoundException.php b/library/think/exception/TemplateNotFoundException.php new file mode 100644 index 00000000..5632a747 --- /dev/null +++ b/library/think/exception/TemplateNotFoundException.php @@ -0,0 +1,16 @@ + +// +---------------------------------------------------------------------- + +namespace think\exception; + +class TemplateNotFoundException extends \RuntimeException +{ +} \ No newline at end of file diff --git a/library/think/exception/ValidateException.php b/library/think/exception/ValidateException.php new file mode 100644 index 00000000..34cdae83 --- /dev/null +++ b/library/think/exception/ValidateException.php @@ -0,0 +1,16 @@ + +// +---------------------------------------------------------------------- + +namespace think\exception; + +class ValidateException extends \RuntimeException +{ +} \ No newline at end of file diff --git a/library/think/view/driver/Php.php b/library/think/view/driver/Php.php index 5eda4faf..de841824 100644 --- a/library/think/view/driver/Php.php +++ b/library/think/view/driver/Php.php @@ -11,7 +11,7 @@ namespace think\view\driver; -use think\Exception; +use think\exception\TemplateNotFoundException; use think\Log; use think\Request; @@ -62,7 +62,7 @@ class Php } // 模板不存在 抛出异常 if (!is_file($template)) { - throw new Exception('template file not exists:' . $template, 10700); + throw new TemplateNotFoundException('template file not exists:' . $template); } // 记录视图信息 APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); diff --git a/library/think/view/driver/Think.php b/library/think/view/driver/Think.php index 00bfbccc..4a14fc8f 100644 --- a/library/think/view/driver/Think.php +++ b/library/think/view/driver/Think.php @@ -11,7 +11,7 @@ namespace think\view\driver; -use think\Exception; +use think\exception\TemplateNotFoundException; use think\Log; use think\Request; use think\Template; @@ -72,7 +72,7 @@ class Think } // 模板不存在 抛出异常 if (!is_file($template)) { - throw new Exception('template file not exists:' . $template, 10700); + throw new TemplateNotFoundException('template file not exists:' . $template); } // 记录视图信息 APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); diff --git a/tests/thinkphp/library/think/controllerTest.php b/tests/thinkphp/library/think/controllerTest.php index dbf60d8e..ce5f49a0 100644 --- a/tests/thinkphp/library/think/controllerTest.php +++ b/tests/thinkphp/library/think/controllerTest.php @@ -31,6 +31,48 @@ class Foo extends Controller { $this->test = 'abcd'; } + + public function assignTest() + { + $this->assign('abcd', 'dcba'); + $this->assign(['key1' => 'value1', 'key2' => 'value2']); + } + + public function fetchTest() + { + $template = dirname(__FILE__) . '/display.html'; + return $this->fetch($template, ['name' => 'ThinkPHP']); + } + + public function displayTest() + { + $template = dirname(__FILE__) . '/display.html'; + return $this->display($template, ['name' => 'ThinkPHP']); + } + public function test() + { + $data = [ + 'username' => 'username', + 'nickname' => 'nickname', + 'password' => '123456', + 'repassword' => '123456', + 'email' => 'abc@abc.com', + 'sex' => '0', + 'age' => '20', + 'code' => '1234', + ]; + + $validate = [ + ['username', 'length:5,15', '用户名长度为5到15个字符'], + ['nickname', 'require', '请填昵称'], + ['password', '[\w-]{6,15}', '密码长度为6到15个字符'], + ['repassword', 'confirm:password', '两次密码不一到致'], + ['email', 'filter:validate_email', '邮箱格式错误'], + ['sex', 'in:0,1', '性别只能为为男或女'], + ['age', 'between:1,80', '年龄只能在10-80之间'], + ]; + return $this->validate($data, $validate); + } } class Bar extends Controller @@ -88,8 +130,6 @@ class Baz extends Controller } } -define('ACTION_NAME', 'index'); - class controllerTest extends \PHPUnit_Framework_TestCase { public function testInitialize() @@ -123,8 +163,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase $view = $this->getView($controller); $template = dirname(__FILE__) . '/display.html'; $viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']); - $controllerFetch = $controller->fetch($template, ['name' => 'ThinkPHP']); - $this->assertEquals($controllerFetch, $viewFetch); + $this->assertEquals($controller->fetchTest(), $viewFetch); } public function testDisplay() @@ -133,16 +172,15 @@ class controllerTest extends \PHPUnit_Framework_TestCase $view = $this->getView($controller); $template = dirname(__FILE__) . '/display.html'; $viewFetch = $view->display($template, ['name' => 'ThinkPHP']); - $controllerFetch = $controller->display($template, ['name' => 'ThinkPHP']); - $this->assertEquals($controllerFetch, $viewFetch); + + $this->assertEquals($controller->displayTest(), $viewFetch); } public function testAssign() { $controller = new Foo(Request::instance()); $view = $this->getView($controller); - $controller->assign('abcd', 'dcba'); - $controller->assign(['key1' => 'value1', 'key2' => 'value2']); + $controller->assignTest(); $expect = ['abcd' => 'dcba', 'key1' => 'value1', 'key2' => 'value2']; $this->assertAttributeEquals($expect, 'data', $view); } @@ -150,27 +188,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase public function testValidate() { $controller = new Foo(Request::instance()); - $data = [ - 'username' => 'username', - 'nickname' => 'nickname', - 'password' => '123456', - 'repassword' => '123456', - 'email' => 'abc@abc.com', - 'sex' => '0', - 'age' => '20', - 'code' => '1234', - ]; - - $validate = [ - ['username', 'length:5,15', '用户名长度为5到15个字符'], - ['nickname', 'require', '请填昵称'], - ['password', '[\w-]{6,15}', '密码长度为6到15个字符'], - ['repassword', 'confirm:password', '两次密码不一到致'], - ['email', 'filter:validate_email', '邮箱格式错误'], - ['sex', 'in:0,1', '性别只能为为男或女'], - ['age', 'between:1,80', '年龄只能在10-80之间'], - ]; - $result = $controller->validate($data, $validate); + $result = $controller->test(); $this->assertTrue($result); } } diff --git a/tests/thinkphp/library/think/sessionTest.php b/tests/thinkphp/library/think/sessionTest.php index 50ae99b6..8c3c2d53 100644 --- a/tests/thinkphp/library/think/sessionTest.php +++ b/tests/thinkphp/library/think/sessionTest.php @@ -163,7 +163,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase // 测试session驱动是否存在 // @expectedException 异常类名 - $this->setExpectedException('\think\Exception', 'error session handler', 11700); + $this->setExpectedException('\think\exception\ClassNotFoundException', 'error session handler'); Session::init($config); }