mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 15:02:47 +08:00
Merge branch 'master' of https://github.com/top-think/think
This commit is contained in:
@@ -19,6 +19,8 @@ class Loader
|
|||||||
protected static $load = [];
|
protected static $load = [];
|
||||||
// 命名空间
|
// 命名空间
|
||||||
protected static $namespace = [];
|
protected static $namespace = [];
|
||||||
|
// 命名空间别名
|
||||||
|
protected static $namespaceAlias = [];
|
||||||
// PSR-4
|
// PSR-4
|
||||||
private static $prefixLengthsPsr4 = [];
|
private static $prefixLengthsPsr4 = [];
|
||||||
private static $prefixDirsPsr4 = [];
|
private static $prefixDirsPsr4 = [];
|
||||||
@@ -28,6 +30,16 @@ class Loader
|
|||||||
// 自动加载
|
// 自动加载
|
||||||
public static function autoload($class)
|
public static function autoload($class)
|
||||||
{
|
{
|
||||||
|
// 检测命名空间别名
|
||||||
|
if (!empty(self::$namespaceAlias)) {
|
||||||
|
$namespace = dirname($class);
|
||||||
|
if (isset(self::$namespaceAlias[$namespace])) {
|
||||||
|
$original = self::$namespaceAlias[$namespace] . '\\' . basename($class);
|
||||||
|
if (class_exists($original)) {
|
||||||
|
return class_alias($original, $class, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// 检查是否定义类库映射
|
// 检查是否定义类库映射
|
||||||
if (isset(self::$map[$class])) {
|
if (isset(self::$map[$class])) {
|
||||||
if (is_file(self::$map[$class])) {
|
if (is_file(self::$map[$class])) {
|
||||||
@@ -47,6 +59,7 @@ class Loader
|
|||||||
if (!strpos($class, '\\')) {
|
if (!strpos($class, '\\')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// 解析命名空间
|
||||||
list($name, $class) = explode('\\', $class, 2);
|
list($name, $class) = explode('\\', $class, 2);
|
||||||
if (isset(self::$namespace[$name])) {
|
if (isset(self::$namespace[$name])) {
|
||||||
// 注册的命名空间
|
// 注册的命名空间
|
||||||
@@ -94,6 +107,16 @@ class Loader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注册命名空间别名
|
||||||
|
public static function addNamespaceAlias($namespace, $original = '')
|
||||||
|
{
|
||||||
|
if (is_array($namespace)) {
|
||||||
|
self::$namespaceAlias = array_merge(self::$namespace, $namespace);
|
||||||
|
} else {
|
||||||
|
self::$namespaceAlias[$namespace] = $original;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 注册自动加载机制
|
// 注册自动加载机制
|
||||||
public static function register($autoload = '')
|
public static function register($autoload = '')
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -340,12 +340,11 @@ class Model
|
|||||||
throw new Exception('no data to write');
|
throw new Exception('no data to write');
|
||||||
}
|
}
|
||||||
// 数据处理
|
// 数据处理
|
||||||
foreach ($dataList as $key => $data) {
|
foreach ($dataList as &$data) {
|
||||||
$data = $this->_write_data($data, 'insert');
|
$data = $this->_write_data($data, 'insert');
|
||||||
if (false === $data) {
|
if (false === $data) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$dataList[$key] = $data;
|
|
||||||
}
|
}
|
||||||
// 分析表达式
|
// 分析表达式
|
||||||
$options = $this->_parseOptions($options);
|
$options = $this->_parseOptions($options);
|
||||||
@@ -489,8 +488,11 @@ class Model
|
|||||||
// 判断查询缓存
|
// 判断查询缓存
|
||||||
if (isset($options['cache'])) {
|
if (isset($options['cache'])) {
|
||||||
$cache = $options['cache'];
|
$cache = $options['cache'];
|
||||||
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
if (!isset($cache['key']) || !is_string($cache['key'])) {
|
||||||
$data = Cache::get($key);
|
$cache['key'] = md5(serialize($options));
|
||||||
|
}
|
||||||
|
$cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null;
|
||||||
|
$data = Cache::get($cache['key']);
|
||||||
if (false !== $data) {
|
if (false !== $data) {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@@ -523,7 +525,7 @@ class Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isset($cache)) {
|
if (isset($cache)) {
|
||||||
Cache::set($key, $resultSet, $cache['expire']);
|
Cache::set($cache['key'], $resultSet, $cache['expire']);
|
||||||
}
|
}
|
||||||
return $resultSet;
|
return $resultSet;
|
||||||
}
|
}
|
||||||
@@ -542,7 +544,7 @@ class Model
|
|||||||
// 根据主键查询
|
// 根据主键查询
|
||||||
if (is_array($options)) {
|
if (is_array($options)) {
|
||||||
// 判断是否索引数组
|
// 判断是否索引数组
|
||||||
if (key($options) === 0) {
|
if (0 === key($options)) {
|
||||||
$where[$pk] = ['in', $options];
|
$where[$pk] = ['in', $options];
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
@@ -554,19 +556,10 @@ class Model
|
|||||||
$options['where'] = $where;
|
$options['where'] = $where;
|
||||||
} elseif (is_array($pk) && is_array($options) && !empty($options)) {
|
} elseif (is_array($pk) && is_array($options) && !empty($options)) {
|
||||||
// 根据复合主键查询
|
// 根据复合主键查询
|
||||||
$count = 0;
|
$array = array_intersect_key($options, $pk);
|
||||||
foreach (array_keys($options) as $key) {
|
if (count($pk) == count($array)) {
|
||||||
if (is_int($key)) {
|
$options = array_diff_key($options, $array);
|
||||||
$count++;
|
$options['where'] = array_combine($pk, $array);
|
||||||
}
|
|
||||||
}
|
|
||||||
if (count($pk) == $count) {
|
|
||||||
$i = 0;
|
|
||||||
foreach ($pk as $field) {
|
|
||||||
$where[$field] = $options[$i];
|
|
||||||
unset($options[$i++]);
|
|
||||||
}
|
|
||||||
$options['where'] = $where;
|
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('miss complex primary data');
|
throw new Exception('miss complex primary data');
|
||||||
}
|
}
|
||||||
@@ -604,8 +597,8 @@ class Model
|
|||||||
// 判断查询缓存
|
// 判断查询缓存
|
||||||
if (isset($options['cache'])) {
|
if (isset($options['cache'])) {
|
||||||
$cache = $options['cache'];
|
$cache = $options['cache'];
|
||||||
$key = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
|
$cache['key'] = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
|
||||||
$data = Cache::get($key);
|
$data = Cache::get($cache['key']);
|
||||||
if (false !== $data) {
|
if (false !== $data) {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@@ -620,22 +613,19 @@ class Model
|
|||||||
if (is_string($resultSet)) {
|
if (is_string($resultSet)) {
|
||||||
return $resultSet;
|
return $resultSet;
|
||||||
}
|
}
|
||||||
$_field = explode(',', $field);
|
|
||||||
$field = array_keys($resultSet[0]);
|
$field = array_keys($resultSet[0]);
|
||||||
$key1 = array_shift($field);
|
$cols = [];
|
||||||
$key2 = array_shift($field);
|
$count = count($field);
|
||||||
$cols = array();
|
|
||||||
$count = count($_field);
|
|
||||||
foreach ($resultSet as $result) {
|
foreach ($resultSet as $result) {
|
||||||
$name = $result[$key1];
|
$name = $result[$field[0]];
|
||||||
if (2 == $count) {
|
if (2 == $count) {
|
||||||
$cols[$name] = $result[$key2];
|
$cols[$name] = $result[$field[1]];
|
||||||
} else {
|
} else {
|
||||||
$cols[$name] = is_string($sepa) ? implode($sepa, array_slice($result, 1)) : $result;
|
$cols[$name] = is_string($sepa) ? implode($sepa, array_slice($result, 1)) : $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isset($cache)) {
|
if (isset($cache)) {
|
||||||
Cache::set($key, $cols, $cache['expire']);
|
Cache::set($cache['key'], $cols, $cache['expire']);
|
||||||
}
|
}
|
||||||
return $cols;
|
return $cols;
|
||||||
}
|
}
|
||||||
@@ -654,15 +644,16 @@ class Model
|
|||||||
if (true !== $sepa && 1 == $options['limit']) {
|
if (true !== $sepa && 1 == $options['limit']) {
|
||||||
$data = reset($result[0]);
|
$data = reset($result[0]);
|
||||||
if (isset($cache)) {
|
if (isset($cache)) {
|
||||||
Cache::set($key, $data, $cache['expire']);
|
Cache::set($cache['key'], $data, $cache['expire']);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
$array = [];
|
||||||
foreach ($result as $val) {
|
foreach ($result as $val) {
|
||||||
$array[] = $val[$field];
|
$array[] = $val[$field];
|
||||||
}
|
}
|
||||||
if (isset($cache)) {
|
if (isset($cache)) {
|
||||||
Cache::set($key, $array, $cache['expire']);
|
Cache::set($cache['key'], $array, $cache['expire']);
|
||||||
}
|
}
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
@@ -884,8 +875,11 @@ class Model
|
|||||||
// 判断查询缓存
|
// 判断查询缓存
|
||||||
if (isset($options['cache'])) {
|
if (isset($options['cache'])) {
|
||||||
$cache = $options['cache'];
|
$cache = $options['cache'];
|
||||||
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
if (!isset($cache['key']) || !is_string($cache['key'])) {
|
||||||
$data = Cache::get($key);
|
$cache['key'] = md5(serialize($options));
|
||||||
|
}
|
||||||
|
$cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null;
|
||||||
|
$data = Cache::get($cache['key']);
|
||||||
if (false !== $data) {
|
if (false !== $data) {
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
return $data;
|
return $data;
|
||||||
@@ -905,7 +899,7 @@ class Model
|
|||||||
// 回调
|
// 回调
|
||||||
$this->_after_find($data, $options);
|
$this->_after_find($data, $options);
|
||||||
if (isset($cache)) {
|
if (isset($cache)) {
|
||||||
Cache::set($key, $data, $cache['expire']);
|
Cache::set($cache['key'], $data, $cache['expire']);
|
||||||
}
|
}
|
||||||
// 数据对象赋值
|
// 数据对象赋值
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
@@ -1018,7 +1012,8 @@ class Model
|
|||||||
if (is_numeric($key) && is_array($rule)) {
|
if (is_numeric($key) && is_array($rule)) {
|
||||||
$key = array_shift($rule);
|
$key = array_shift($rule);
|
||||||
}
|
}
|
||||||
if (!empty($scene) && !in_array($key, $scene)) {
|
if ((!empty($scene) && !in_array($key, $scene)) || isset($this->error[$key])) {
|
||||||
|
// 不在指定的场景中或者已经验证过相同的字段
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!$this->fieldValidate($key, $rule, $data, $options)) {
|
if (!$this->fieldValidate($key, $rule, $data, $options)) {
|
||||||
@@ -1525,6 +1520,9 @@ class Model
|
|||||||
if (!$tableName) {
|
if (!$tableName) {
|
||||||
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTableName();
|
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTableName();
|
||||||
}
|
}
|
||||||
|
if (is_array($tableName)) {
|
||||||
|
$tableName = key($tableName) ?: current($tableName);
|
||||||
|
}
|
||||||
if (strpos($tableName, ',')) {
|
if (strpos($tableName, ',')) {
|
||||||
// 多表不获取字段信息
|
// 多表不获取字段信息
|
||||||
return false;
|
return false;
|
||||||
@@ -1675,7 +1673,7 @@ class Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (empty($condition)) {
|
if (empty($condition)) {
|
||||||
if (is_array($join) && is_array($join[0])) {
|
if (is_array($join) && is_array(current($join))) {
|
||||||
// 如果为组数,则循环调用join
|
// 如果为组数,则循环调用join
|
||||||
foreach ($join as $key => $value) {
|
foreach ($join as $key => $value) {
|
||||||
if (is_array($value) && 2 <= count($value)) {
|
if (is_array($value) && 2 <= count($value)) {
|
||||||
@@ -1744,21 +1742,22 @@ class Model
|
|||||||
}
|
}
|
||||||
if (is_object($union)) {
|
if (is_object($union)) {
|
||||||
$union = get_object_vars($union);
|
$union = get_object_vars($union);
|
||||||
|
} elseif (is_string($union)) {
|
||||||
|
// 转换union表达式
|
||||||
|
$union = (array) $union;
|
||||||
}
|
}
|
||||||
// 转换union表达式
|
if (is_array($union)) {
|
||||||
if (is_string($union)) {
|
|
||||||
$options = $this->parseSqlTable($union);
|
|
||||||
} elseif (is_array($union)) {
|
|
||||||
if (isset($union[0])) {
|
if (isset($union[0])) {
|
||||||
$this->options['union'] = array_merge($this->options['union'], $union);
|
foreach ($union as &$val) {
|
||||||
return $this;
|
$val = $this->parseSqlTable($val);
|
||||||
|
}
|
||||||
|
$this->options['union'] = isset($this->options['union']) ? array_merge($this->options['union'], $union) : $union;
|
||||||
} else {
|
} else {
|
||||||
$options = $union;
|
$this->options['union'][] = $union;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('data type invalid', 10300);
|
throw new Exception('data type invalid', 10300);
|
||||||
}
|
}
|
||||||
$this->options['union'][] = $options;
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -456,13 +456,18 @@ class Template
|
|||||||
// 分析模板文件名并读取内容
|
// 分析模板文件名并读取内容
|
||||||
$parseStr = $this->parseTemplateName($file);
|
$parseStr = $this->parseTemplateName($file);
|
||||||
// 替换变量
|
// 替换变量
|
||||||
|
$varStr = "";
|
||||||
foreach ($array as $k => $v) {
|
foreach ($array as $k => $v) {
|
||||||
// 以$开头字符串转换成模板变量
|
// 以$开头字符串转换成模板变量
|
||||||
if (0 === strpos($v, '$')) {
|
if (0 === strpos($v, '$')) {
|
||||||
$v = $this->get(substr($v, 1));
|
$v = $this->get(substr($v, 1));
|
||||||
}
|
}
|
||||||
|
// 兼容 [var_name] 静态渲染
|
||||||
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
|
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
|
||||||
|
// 支持 $var_name = value 动态赋值
|
||||||
|
$varStr .= "{assign name=\"{$k}\" value=\"{$v}\" /}";
|
||||||
}
|
}
|
||||||
|
$parseStr = $varStr . $parseStr;
|
||||||
$content = str_replace($match[0], $parseStr, $content);
|
$content = str_replace($match[0], $parseStr, $content);
|
||||||
// 再次对包含文件进行模板分析
|
// 再次对包含文件进行模板分析
|
||||||
$funReplace($parseStr);
|
$funReplace($parseStr);
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class Url
|
|||||||
$url = str_replace('/', $depr, $url);
|
$url = str_replace('/', $depr, $url);
|
||||||
|
|
||||||
// URL后缀
|
// URL后缀
|
||||||
$suffix = ('/' == $url) ? '' : self::parseSuffix($suffix);
|
$suffix = in_array($url, ['/', '']) ? '' : self::parseSuffix($suffix);
|
||||||
// 锚点
|
// 锚点
|
||||||
$anchor = !empty($anchor) ? '#' . $anchor : '';
|
$anchor = !empty($anchor) ? '#' . $anchor : '';
|
||||||
// 参数组装
|
// 参数组装
|
||||||
@@ -104,7 +104,6 @@ class Url
|
|||||||
|
|
||||||
// 检测域名
|
// 检测域名
|
||||||
$domain = self::parseDomain($url, $domain);
|
$domain = self::parseDomain($url, $domain);
|
||||||
|
|
||||||
// URL组装
|
// URL组装
|
||||||
$url = $domain . Config::get('base_url') . '/' . ltrim($url, '/');
|
$url = $domain . Config::get('base_url') . '/' . ltrim($url, '/');
|
||||||
return $url;
|
return $url;
|
||||||
|
|||||||
@@ -110,15 +110,15 @@ thinkphp5 的测试的主要流程是跟 thinkphp 的系统流程是相似的,
|
|||||||
|Base|||
|
|Base|||
|
||||||
|App|Haotong Lin||
|
|App|Haotong Lin||
|
||||||
|Build|刘志淳||
|
|Build|刘志淳||
|
||||||
|Config|Haotong Lin||
|
|Config|Haotong Lin|√|
|
||||||
|Cache|||
|
|Cache|||
|
||||||
|Controller|Haotong Lin||
|
|Controller|Haotong Lin|√|
|
||||||
|Cookie|Haotong Lin||
|
|Cookie|Haotong Lin|√|
|
||||||
|Db|||
|
|Db|||
|
||||||
|Debug|大漠|√|
|
|Debug|大漠|√|
|
||||||
|Error|大漠||
|
|Error|大漠||
|
||||||
|Hook|流年|√|
|
|Hook|流年|√|
|
||||||
|Input|Haotong Lin||
|
|Input|Haotong Lin|√|
|
||||||
|Lang|流年|√|
|
|Lang|流年|√|
|
||||||
|Loader|流年||
|
|Loader|流年||
|
||||||
|Log|||
|
|Log|||
|
||||||
|
|||||||
@@ -16,9 +16,13 @@
|
|||||||
|
|
||||||
namespace tests\thinkphp\library\think;
|
namespace tests\thinkphp\library\think;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use think\Controller;
|
||||||
|
use think\View;
|
||||||
|
|
||||||
require_once CORE_PATH . '../../helper.php';
|
require_once CORE_PATH . '../../helper.php';
|
||||||
|
|
||||||
class Foo extends \think\Controller
|
class Foo extends Controller
|
||||||
{
|
{
|
||||||
public $test = 'test';
|
public $test = 'test';
|
||||||
|
|
||||||
@@ -28,7 +32,7 @@ class Foo extends \think\Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Bar extends \think\Controller
|
class Bar extends Controller
|
||||||
{
|
{
|
||||||
public $test = 1;
|
public $test = 1;
|
||||||
|
|
||||||
@@ -47,15 +51,15 @@ class Bar extends \think\Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Baz extends \think\Controller
|
class Baz extends Controller
|
||||||
{
|
{
|
||||||
public $test = 1;
|
public $test = 1;
|
||||||
|
|
||||||
public $beforeActionList = [
|
public $beforeActionList = [
|
||||||
'action1' => ['only' => ['index']],
|
'action1' => ['only' => 'index'],
|
||||||
'action2' => ['except' => ['index']],
|
'action2' => ['except' => 'index'],
|
||||||
'action3' => ['only' => ['abcd']],
|
'action3' => ['only' => 'abcd'],
|
||||||
'action4' => ['except' => ['abcd']],
|
'action4' => ['except' => 'abcd'],
|
||||||
];
|
];
|
||||||
|
|
||||||
public function action1()
|
public function action1()
|
||||||
@@ -101,4 +105,54 @@ class controllerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$obj = new Baz;
|
$obj = new Baz;
|
||||||
$this->assertEquals(19, $obj->test);
|
$this->assertEquals(19, $obj->test);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getView($controller)
|
||||||
|
{
|
||||||
|
$view = new View();
|
||||||
|
$rc = new ReflectionClass(get_class($controller));
|
||||||
|
$property = $rc->getProperty('view');
|
||||||
|
$property->setAccessible(true);
|
||||||
|
$property->setValue($controller, $view);
|
||||||
|
return $view;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFetch()
|
||||||
|
{
|
||||||
|
$controller = new Foo;
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testShow()
|
||||||
|
{
|
||||||
|
$controller = new Foo;
|
||||||
|
$view = $this->getView($controller);
|
||||||
|
$template = dirname(__FILE__) . '/display.html';
|
||||||
|
$viewFetch = $view->show($template, ['name' => 'ThinkPHP']);
|
||||||
|
$controllerFetch = $controller->show($template, ['name' => 'ThinkPHP']);
|
||||||
|
$this->assertEquals($controllerFetch, $viewFetch);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssign()
|
||||||
|
{
|
||||||
|
$controller = new Foo;
|
||||||
|
$view = $this->getView($controller);
|
||||||
|
$controller->assign('abcd', 'dcba');
|
||||||
|
$controller->assign(['key1' => 'value1', 'key2' => 'value2']);
|
||||||
|
$expect = ['abcd' => 'dcba', 'key1' => 'value1', 'key2' => 'value2'];
|
||||||
|
$this->assertAttributeEquals($expect, 'data', $view);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEngine()
|
||||||
|
{
|
||||||
|
$controller = new Foo;
|
||||||
|
$view = $this->getView($controller);
|
||||||
|
$view->engine = null;
|
||||||
|
$this->assertEquals(null, $view->engine);
|
||||||
|
$controller->engine('php');
|
||||||
|
$this->assertEquals('php', $view->engine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,11 +23,12 @@ class modelTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$config = [
|
$config = [
|
||||||
'connection' => [
|
'connection' => [
|
||||||
'type' => 'mysql',
|
'type' => 'mysql',
|
||||||
'database' => 'test',
|
'database' => 'test',
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => '',
|
'password' => '',
|
||||||
],
|
],
|
||||||
|
'prefix' => 'tp_',
|
||||||
];
|
];
|
||||||
return $config;
|
return $config;
|
||||||
}
|
}
|
||||||
@@ -35,41 +36,43 @@ class modelTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testValidate()
|
public function testValidate()
|
||||||
{
|
{
|
||||||
$model = new Model('', $this->getConfig());
|
$model = new Model('', $this->getConfig());
|
||||||
$data = [
|
$data = [
|
||||||
'username' => 'username',
|
'username' => 'username',
|
||||||
'nickname' => 'nickname',
|
'nickname' => 'nickname',
|
||||||
'password' => '123456',
|
'password' => '123456',
|
||||||
'repassword' => '123456',
|
'repassword' => '123456',
|
||||||
'mobile' => '13800000000',
|
'mobile' => '13800000000',
|
||||||
'email' => 'abc@abc.com',
|
'email' => 'abc@abc.com',
|
||||||
'sex' => '0',
|
'sex' => '0',
|
||||||
'age' => '20',
|
'age' => '20',
|
||||||
'code' => '1234',
|
'code' => '1234',
|
||||||
];
|
];
|
||||||
|
|
||||||
$validate = [
|
$validate = [
|
||||||
'__pattern__' => [
|
'__pattern__' => [
|
||||||
'mobile' => '/^1(?:[358]\d|7[6-8])\d{8}$/',
|
'mobile' => '/^1(?:[358]\d|7[6-8])\d{8}$/',
|
||||||
'require' => '/.+/',
|
'require' => '/.+/',
|
||||||
],
|
],
|
||||||
'__all__' => [
|
'__all__' => [
|
||||||
'code' => function($value, $data) {return '1234' != $value ? 'code error' : true;},
|
'code' => function ($value, $data) {
|
||||||
|
return '1234' != $value ? 'code error' : true;
|
||||||
|
},
|
||||||
],
|
],
|
||||||
'user' => [
|
'user' => [
|
||||||
['username', [&$this, 'checkName'], '用户名长度为5到15个字符', 'callback', 'username'],
|
['username', [&$this, 'checkName'], '用户名长度为5到15个字符', 'callback', 'username'],
|
||||||
['nickname', 'require', '请填昵称'],
|
['nickname', 'require', '请填昵称'],
|
||||||
'password' => ['[\w-]{6,15}', '密码长度为6到15个字符'],
|
'password' => ['[\w-]{6,15}', '密码长度为6到15个字符'],
|
||||||
'repassword' => ['password', '两次密码不一到致', 'confirm'],
|
'repassword' => ['password', '两次密码不一到致', 'confirm'],
|
||||||
'mobile' => ['mobile', '手机号错误'],
|
'mobile' => ['mobile', '手机号错误'],
|
||||||
'email' => ['validate_email', '邮箱格式错误', 'filter'],
|
'email' => ['validate_email', '邮箱格式错误', 'filter'],
|
||||||
'sex' => ['0,1', '性别只能为为男或女', 'in'],
|
'sex' => ['0,1', '性别只能为为男或女', 'in'],
|
||||||
'age' => ['1,80', '年龄只能在10-80之间', 'between'],
|
'age' => ['1,80', '年龄只能在10-80之间', 'between'],
|
||||||
'__option__' => [
|
'__option__' => [
|
||||||
'scene' => [
|
'scene' => [
|
||||||
'add' => 'username,nickname,password,repassword,mobile,email,age,code',
|
'add' => 'username,nickname,password,repassword,mobile,email,age,code',
|
||||||
'edit' => 'nickname,password,repassword,mobile,email,sex,age,code',
|
'edit' => 'nickname,password,repassword,mobile,email,sex,age,code',
|
||||||
],
|
],
|
||||||
'value_validate' => 'email',
|
'value_validate' => 'email',
|
||||||
'exists_validate' => 'password,repassword,code',
|
'exists_validate' => 'password,repassword,code',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
@@ -80,47 +83,50 @@ class modelTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
unset($data['password'], $data['repassword']);
|
unset($data['password'], $data['repassword']);
|
||||||
$data['email'] = '';
|
$data['email'] = '';
|
||||||
$result = $model->validate('user.edit')->create($data);
|
$result = $model->validate('user.edit')->create($data);
|
||||||
$this->assertEquals('', $model->getError());
|
$this->assertEquals('', $model->getError());
|
||||||
|
|
||||||
// 测试带.和*的键名
|
// 测试带.和*的键名
|
||||||
$data = [
|
$data = [
|
||||||
'code' => '',
|
'code' => '',
|
||||||
'name' => ['a' => '', 'b' => ''],
|
'name' => ['a' => '', 'b' => ''],
|
||||||
'sku' => [
|
'sku' => [
|
||||||
0 => [
|
0 => [
|
||||||
0 => [
|
0 => [
|
||||||
'item' => 'item',
|
'item' => 'item',
|
||||||
'price' => '',
|
'price' => '',
|
||||||
],
|
],
|
||||||
1 => [
|
1 => [
|
||||||
'item' => 'item2',
|
'item' => 'item2',
|
||||||
'price' => '',
|
'price' => '',
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
$test = [
|
$test = [
|
||||||
'code' => function($value, $data) {return empty($value) ? ['code' => 'not empty'] : true;},
|
'code' => function ($value, $data) {
|
||||||
'name.*' => ['/.+/', 'not empty'],
|
return empty($value) ? ['code' => 'not empty'] : true;
|
||||||
|
},
|
||||||
|
'name.*' => ['/.+/', 'not empty'],
|
||||||
'sku.*.*.price' => ['/\d+/', 'mast int'],
|
'sku.*.*.price' => ['/\d+/', 'mast int'],
|
||||||
'__option__' => [
|
'__option__' => [
|
||||||
'patch' => true,
|
'patch' => true,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
$result = $model->validate($test)->create($data);
|
$result = $model->validate($test)->create($data);
|
||||||
$msg = [
|
$msg = [
|
||||||
'code' => 'not empty',
|
'code' => 'not empty',
|
||||||
'name.a' => 'not empty',
|
'name.a' => 'not empty',
|
||||||
'name.b' => 'not empty',
|
'name.b' => 'not empty',
|
||||||
'sku.0.0.price' => 'mast int',
|
'sku.0.0.price' => 'mast int',
|
||||||
'sku.0.1.price' => 'mast int',
|
'sku.0.1.price' => 'mast int',
|
||||||
];
|
];
|
||||||
$this->assertEquals($msg, $model->getError());
|
$this->assertEquals($msg, $model->getError());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function checkName($value, $field) {
|
public function checkName($value, $field)
|
||||||
switch($field) {
|
{
|
||||||
|
switch ($field) {
|
||||||
case 'username':
|
case 'username':
|
||||||
return !empty($value);
|
return !empty($value);
|
||||||
case 'mobile':
|
case 'mobile':
|
||||||
@@ -131,71 +137,365 @@ class modelTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testFill()
|
public function testFill()
|
||||||
{
|
{
|
||||||
$model = new Model('', $this->getConfig());
|
$model = new Model('', $this->getConfig());
|
||||||
$data = [
|
$data = [
|
||||||
'username' => '',
|
'username' => '',
|
||||||
'nickname' => 'nickname',
|
'nickname' => 'nickname',
|
||||||
'phone' => ' 123456',
|
'phone' => ' 123456',
|
||||||
'hobby' => ['1', '2'],
|
'hobby' => ['1', '2'],
|
||||||
'cityid' => '1',
|
'cityid' => '1',
|
||||||
];
|
];
|
||||||
$auto = [
|
$auto = [
|
||||||
'user' => [
|
'user' => [
|
||||||
'__option__' => [
|
'__option__' => [
|
||||||
'value_fill' => ['username', 'password', 'phone'],
|
'value_fill' => ['username', 'password', 'phone'],
|
||||||
'exists_fill' => 'nickname',
|
'exists_fill' => 'nickname',
|
||||||
],
|
],
|
||||||
'username' => ['strtolower', 'callback'],
|
'username' => ['strtolower', 'callback'],
|
||||||
'password' => ['md5', 'callback'],
|
'password' => ['md5', 'callback'],
|
||||||
'nickname' => [[&$this, 'fillName'], 'callback', 'cn_'],
|
'nickname' => [[&$this, 'fillName'], 'callback', 'cn_'],
|
||||||
'phone' => function($value, $data) {return trim($value);},
|
'phone' => function ($value, $data) {
|
||||||
'hobby' => ['', 'serialize'],
|
return trim($value);
|
||||||
'cityid' => ['1', 'ignore'] ,
|
},
|
||||||
'address' => ['address'],
|
'hobby' => ['', 'serialize'],
|
||||||
'integral' => 0,
|
'cityid' => ['1', 'ignore'],
|
||||||
|
'address' => ['address'],
|
||||||
|
'integral' => 0,
|
||||||
['reg_time', 'time', 'callback'],
|
['reg_time', 'time', 'callback'],
|
||||||
['login_time', function($value, $data) {return $data['reg_time'];}],
|
['login_time', function ($value, $data) {
|
||||||
|
return $data['reg_time'];
|
||||||
|
}],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
\think\Config::set('auto', $auto);
|
\think\Config::set('auto', $auto);
|
||||||
$result = $model->auto('user')->create($data);
|
$result = $model->auto('user')->create($data);
|
||||||
$data['nickname'] = 'cn_nickname';
|
$data['nickname'] = 'cn_nickname';
|
||||||
$data['phone'] = '123456';
|
$data['phone'] = '123456';
|
||||||
$data['hobby'] = serialize($data['hobby']);
|
$data['hobby'] = serialize($data['hobby']);
|
||||||
$data['address'] = 'address';
|
$data['address'] = 'address';
|
||||||
$data['integral'] = 0;
|
$data['integral'] = 0;
|
||||||
$data['reg_time'] = time();
|
$data['reg_time'] = time();
|
||||||
$data['login_time'] = $data['reg_time'];
|
$data['login_time'] = $data['reg_time'];
|
||||||
unset($data['cityid']);
|
unset($data['cityid']);
|
||||||
$this->assertEquals($data, $result);
|
$this->assertEquals($data, $result);
|
||||||
|
|
||||||
// 测试带.和*的键名
|
// 测试带.和*的键名
|
||||||
$data = [
|
$data = [
|
||||||
'name' => ['a' => 'a', 'b' => 'b'],
|
'name' => ['a' => 'a', 'b' => 'b'],
|
||||||
'goods' => [
|
'goods' => [
|
||||||
0 => [
|
0 => [
|
||||||
0 => [
|
0 => [
|
||||||
'item' => 'item',
|
'item' => 'item',
|
||||||
'price' => '',
|
'price' => '',
|
||||||
],
|
],
|
||||||
1 => [
|
1 => [
|
||||||
'item' => 'item2',
|
'item' => 'item2',
|
||||||
'price' => '',
|
'price' => '',
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
$test = [
|
$test = [
|
||||||
'name.*' => 'name',
|
'name.*' => 'name',
|
||||||
'goods.*.*.price' => 100,
|
'goods.*.*.price' => 100,
|
||||||
];
|
];
|
||||||
$result = $model->auto($test)->create($data);
|
$result = $model->auto($test)->create($data);
|
||||||
$data['name']['a'] = $data['name']['b'] = 'name';
|
$data['name']['a'] = $data['name']['b'] = 'name';
|
||||||
$data['goods'][0][0]['price'] = 100;
|
$data['goods'][0][0]['price'] = 100;
|
||||||
$data['goods'][0][1]['price'] = 100;
|
$data['goods'][0][1]['price'] = 100;
|
||||||
$this->assertEquals($data, $result);
|
$this->assertEquals($data, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fillName($value, $prefix) {
|
public function fillName($value, $prefix)
|
||||||
|
{
|
||||||
return $prefix . trim($value);
|
return $prefix . trim($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testExecute()
|
||||||
|
{
|
||||||
|
$sql = <<<EOF
|
||||||
|
DROP TABLE IF EXISTS `tp_user`;
|
||||||
|
CREATE TABLE `tp_user` (
|
||||||
|
`id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
`username` char(40) NOT NULL DEFAULT '' COMMENT '用户名',
|
||||||
|
`password` char(40) NOT NULL DEFAULT '' COMMENT '密码',
|
||||||
|
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态',
|
||||||
|
`create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间'
|
||||||
|
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='会员表';
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS `tp_order`;
|
||||||
|
CREATE TABLE `tp_order` (
|
||||||
|
`id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
`user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
|
||||||
|
`sn` char(20) NOT NULL DEFAULT '' COMMENT '订单号',
|
||||||
|
`amount` decimal(10,2) unsigned NOT NULL DEFAULT '0' COMMENT '金额',
|
||||||
|
`freight_fee` decimal(10,2) unsigned NOT NULL DEFAULT '0' COMMENT '运费',
|
||||||
|
`address_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '地址id',
|
||||||
|
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态',
|
||||||
|
`create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间'
|
||||||
|
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='订单表';
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS `tp_user_address`;
|
||||||
|
CREATE TABLE `tp_user_address` (
|
||||||
|
`id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
`user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
|
||||||
|
`consignee` varchar(60) NOT NULL DEFAULT '' COMMENT '收货人',
|
||||||
|
`area_info` varchar(50) NOT NULL DEFAULT '' COMMENT '地区信息',
|
||||||
|
`city_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '城市id',
|
||||||
|
`area_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '地区id',
|
||||||
|
`address` varchar(120) NOT NULL DEFAULT '' COMMENT '地址',
|
||||||
|
`tel` varchar(60) NOT NULL DEFAULT '' COMMENT '电话',
|
||||||
|
`mobile` varchar(60) NOT NULL DEFAULT '' COMMENT '手机',
|
||||||
|
`isdefault` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否默认'
|
||||||
|
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='地址表';
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS `tp_role_user`;
|
||||||
|
CREATE TABLE `tp_role_user` (
|
||||||
|
`role_id` smallint(5) unsigned NOT NULL,
|
||||||
|
`user_id` int(10) unsigned NOT NULL,
|
||||||
|
PRIMARY KEY (`role_id`,`user_id`)
|
||||||
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
||||||
|
EOF;
|
||||||
|
|
||||||
|
$model = new Model('', $this->getConfig());
|
||||||
|
$model->execute($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdd()
|
||||||
|
{
|
||||||
|
$config = $this->getConfig();
|
||||||
|
$time = time();
|
||||||
|
|
||||||
|
$user_model = new Model('user', $config);
|
||||||
|
$data = [
|
||||||
|
'username' => 'test',
|
||||||
|
'password' => md5('123456'),
|
||||||
|
'status' => 1,
|
||||||
|
'create_time' => $time,
|
||||||
|
];
|
||||||
|
$user_id = $user_model->add($data);
|
||||||
|
$data = [
|
||||||
|
'username' => 'test2',
|
||||||
|
'password' => md5('000000'),
|
||||||
|
'status' => 1,
|
||||||
|
'create_time' => $time,
|
||||||
|
];
|
||||||
|
$user_model->add($data, true);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
[
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'consignee' => '张三',
|
||||||
|
'area_info' => '广东深圳',
|
||||||
|
'city_id' => '42',
|
||||||
|
'area_id' => '111',
|
||||||
|
'address' => 'xx路xx号',
|
||||||
|
'mobile' => '1380000000000',
|
||||||
|
'isdefault' => '1',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'consignee' => '李四',
|
||||||
|
'area_info' => '广东深圳',
|
||||||
|
'city_id' => '42',
|
||||||
|
'area_id' => '111',
|
||||||
|
'address' => 'xx路xx号',
|
||||||
|
'mobile' => '13999999999',
|
||||||
|
'isdefault' => '0',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$address_model = new Model('user_address', $config);
|
||||||
|
$address_id = $address_model->addAll($data, [], true);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
[
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'sn' => '10001',
|
||||||
|
'amount' => '200',
|
||||||
|
'freight_fee' => '10',
|
||||||
|
'address_id' => $address_id - 1,
|
||||||
|
'status' => '1',
|
||||||
|
'create_time' => $time,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'sn' => '10002',
|
||||||
|
'amount' => '350',
|
||||||
|
'freight_fee' => '10',
|
||||||
|
'address_id' => $address_id,
|
||||||
|
'status' => '0',
|
||||||
|
'create_time' => $time,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$address_model = new Model('order', $config);
|
||||||
|
$address_model->addAll($data, [], true);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'role_id' => 1,
|
||||||
|
];
|
||||||
|
$model = new Model('', $config);
|
||||||
|
$model->table($config['prefix'] . 'role_user')->add($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testQuery()
|
||||||
|
{
|
||||||
|
$user_model = new Model('user', $this->getConfig());
|
||||||
|
|
||||||
|
$sql = "select id,create_time from tp_user where username='test' limit 1";
|
||||||
|
$result = $user_model->query($sql);
|
||||||
|
$id = $result[0]['id'];
|
||||||
|
$time = $result[0]['create_time'];
|
||||||
|
$info = $user_model->where('create_time=' . $time)->where(['status' => 1])->field(true)->find(['cache' => ['key' => true]]);
|
||||||
|
$data = [
|
||||||
|
'id' => $id,
|
||||||
|
'username' => 'test',
|
||||||
|
'password' => md5('123456'),
|
||||||
|
'status' => '1',
|
||||||
|
'create_time' => $time,
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $info);
|
||||||
|
|
||||||
|
$result = $user_model->where(['id' => $id])->field('password,create_time', true)->order('id')->limit('0,10')->select(['cache' => ['key' => true, 'expire' => 0], 'index' => 'username']);
|
||||||
|
$data = [
|
||||||
|
'id' => $id,
|
||||||
|
'username' => 'test',
|
||||||
|
'status' => '1',
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $result['test']);
|
||||||
|
|
||||||
|
$time = $user_model->where(['status'=>1])->getField('create_time');
|
||||||
|
$ids = $user_model->where(['status'=>1])->getField('id', true);
|
||||||
|
$this->assertEquals(2, count($ids));
|
||||||
|
$result = $user_model->getField('username,status,create_time', '|');
|
||||||
|
$data = [
|
||||||
|
'test' => '1|' . $time,
|
||||||
|
'test2' => '1|' . $time,
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testJoin()
|
||||||
|
{
|
||||||
|
$config = $this->getConfig();
|
||||||
|
$user_model = new Model('user', $config);
|
||||||
|
|
||||||
|
$join = [
|
||||||
|
[['order o', 'tp_'], 'u.id=o.user_id'],
|
||||||
|
[['user_address' => 'a'], 'u.id=a.user_id'],
|
||||||
|
];
|
||||||
|
$result = $user_model->alias('u')->join($join)->field('u.username,a.consignee,o.amount')->select();
|
||||||
|
$data = [
|
||||||
|
'username' => 'test',
|
||||||
|
'consignee' => '张三',
|
||||||
|
'amount' => '200',
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $result[0]);
|
||||||
|
|
||||||
|
$result = $user_model->alias('u')->join('__USER_ADDRESS__ a', 'u.id=a.user_id', 'left')->field('u.username,a.consignee')->select();
|
||||||
|
$data = [
|
||||||
|
'username' => 'test',
|
||||||
|
'consignee' => '张三',
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $result[0]);
|
||||||
|
|
||||||
|
$subsql = "(select user_id,amount from {$config['prefix']}order where status=1 limit 1) o";
|
||||||
|
$result = $user_model->alias('u')->join($subsql, 'u.id=o.user_id', 'left')->field('u.username,o.amount')->select();
|
||||||
|
$data = [
|
||||||
|
'username' => 'test',
|
||||||
|
'amount' => '200',
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $result[0]);
|
||||||
|
|
||||||
|
// 兼容_join方法
|
||||||
|
$result = $user_model->alias('u')->join('__USER_ADDRESS__ a on u.id=a.user_id', 'left')->field('u.username,a.consignee')->select();
|
||||||
|
$data = [
|
||||||
|
'username' => 'test',
|
||||||
|
'consignee' => '张三',
|
||||||
|
];
|
||||||
|
$this->assertEquals($data, $result[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnion()
|
||||||
|
{
|
||||||
|
$config = $this->getConfig();
|
||||||
|
$user_model = new Model('user', $config);
|
||||||
|
|
||||||
|
$union = "SELECT consignee FROM __USER_ADDRESS__";
|
||||||
|
$result = $user_model->field('username')->union($union)->select();
|
||||||
|
$this->assertEquals(4, count($result));
|
||||||
|
|
||||||
|
$model = new Model('', $config);
|
||||||
|
$union = ["SELECT create_time FROM __ORDER__"];
|
||||||
|
$result = $model->table([$config['prefix'] . 'user'])->field('create_time')->union($union, true)->select();
|
||||||
|
$this->assertEquals(4, count($result));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSave()
|
||||||
|
{
|
||||||
|
$config = $this->getConfig();
|
||||||
|
$order_model = new Model('order', $config);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'id' => '1',
|
||||||
|
'amount' => '180',
|
||||||
|
'status' => 0,
|
||||||
|
'create_time' => time(),
|
||||||
|
];
|
||||||
|
$flag = $user_id = $order_model->save($data);
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'status' => 1,
|
||||||
|
];
|
||||||
|
$flag = $order_model->where(['id' => 2])->setField($data);
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
$flag = $order_model->where(['amount'=>['lt',200]])->setField('freight_fee', 15);
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
$map = [
|
||||||
|
'amount' => ['gt', 300],
|
||||||
|
'freight_fee' =>['gt', 5],
|
||||||
|
];
|
||||||
|
$flag = $order_model->where($map)->setDec('freight_fee', 5, 1);
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
$flag = $order_model->where($map)->setInc('freight_fee', 5, 1);
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDelete()
|
||||||
|
{
|
||||||
|
$config = $this->getConfig();
|
||||||
|
|
||||||
|
$order_model = new Model('order', $config);
|
||||||
|
$order_model->id = 2;
|
||||||
|
$flag = $order_model->delete();
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
$flag = $order_model->delete('1');
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
$address_model = new Model('user_address', $config);
|
||||||
|
$flag = $address_model->delete(['1','2']);
|
||||||
|
$this->assertEquals(2, $flag);
|
||||||
|
|
||||||
|
$user_model = new Model('user', $config);
|
||||||
|
$flag = $user_model->where('1=1')->delete();
|
||||||
|
$this->assertEquals(2, $flag);
|
||||||
|
|
||||||
|
$ru_model = new Model('role_user', $config);
|
||||||
|
$flag = $ru_model->delete(['1','1']);
|
||||||
|
$this->assertEquals(1, $flag);
|
||||||
|
|
||||||
|
$sql = <<<EOF
|
||||||
|
DROP TABLE IF EXISTS `tp_user`;
|
||||||
|
DROP TABLE IF EXISTS `tp_order`;
|
||||||
|
DROP TABLE IF EXISTS `tp_user_address`;
|
||||||
|
DROP TABLE IF EXISTS `tp_role_user`;
|
||||||
|
EOF;
|
||||||
|
$model = new Model('', $this->getConfig());
|
||||||
|
$model->execute($sql);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user