File类改进支持单元测试

This commit is contained in:
thinkphp
2016-06-14 12:53:45 +08:00
parent c5bde96086
commit fb188291d5
2 changed files with 21 additions and 5 deletions

View File

@@ -22,14 +22,17 @@ class File extends SplFileObject
private $error = ''; private $error = '';
// 文件上传命名规则 // 文件上传命名规则
protected $rule = 'date'; protected $rule = 'date';
// 单元测试
protected $isTest;
// 上传文件信息 // 上传文件信息
protected $info; protected $info;
public function __construct($filename, $info = []) public function __construct($filename, $info = [],$isTest=false)
{ {
parent::__construct($filename); parent::__construct($filename);
$this->info = $info; $this->info = $info;
$this->isTest = $isTest;
} }
/** /**
@@ -88,6 +91,9 @@ class File extends SplFileObject
*/ */
public function isValid() public function isValid()
{ {
if($this->isTest){
return is_file($this->getRealPath());
}
return is_uploaded_file($this->getRealPath()); return is_uploaded_file($this->getRealPath());
} }
@@ -122,7 +128,9 @@ class File extends SplFileObject
} }
/* 移动文件 */ /* 移动文件 */
if (!move_uploaded_file($this->getRealPath(), $path . $savename)) { if($this->isTest){
rename($this->getRealPath(),$path.$savename);
} elseif (!move_uploaded_file($this->getRealPath(), $path . $savename)) {
$this->error = '文件上传保存错误!'; $this->error = '文件上传保存错误!';
return false; return false;
} }

View File

@@ -241,11 +241,19 @@ class Input
if (empty($val['tmp_name'])) { if (empty($val['tmp_name'])) {
continue; continue;
} }
$item[$key] = new File($val['tmp_name'], $val); if($val instanceof File){
$item[$key] = $val;
}else{
$item[$key] = new File($val['tmp_name'], $val);
}
} }
return $item; return $item;
} elseif (isset($array[$name])) { } elseif (isset($array[$name])) {
return new File($array[$name]['tmp_name'], $array[$name]); if($array[$name] instanceof File){
return $array[$name];
}else{
return new File($array[$name]['tmp_name'], $array[$name]);
}
} }
} }
return null; return null;