我们知道 Yii 使用C上传文件 http://www.yiiframework.com/doc/api/1.1/CUploadedFile,对于文件上传,我们必须使用它来初始化模型的文件属性。
我们可以使用构造函数来初始化 http://www.yiiframework.com/doc/api/1.1/CUploadedFile#__construct-detail文件属性new CUploadedFile($names, $tmp_names, $types, $sizes, $errors);
因此我们可以这样做:
public ValidatorTest extends CTestCase{
public $testFile = array(
'name'=>'yii-1.1.0-validator-cheatsheet.pdf',
'tmp_name'=>'/private/var/tmp/phpvVRwKT',
'type'=>'application/pdf',
'size'=>100,
'error'=>0
);
public function testValidators()
{
$testUpload = new TestUploadForm;
$testUpload->test = new CUploadedFile($this->testFile['name'],$this->testFile['tmp_name'],$this->testFile['type'],$this->testFile['size'],$this->testFile['error']);
$this->assertTrue($testUpload->validate());
$errors= $testUpload->errors;
$this->assertEmpty($errors);
}
}
The C文件验证器 http://www.yiiframework.com/doc/api/1.1/CFileValidator考虑到用于确定类型的文件扩展名 http://www.yiiframework.com/doc/api/1.1/CFileValidator#types-detail,因此要测试您的验证器,您必须不断更改验证器的名称$testFile
, i.e $testFile['name']='correctname.rar'
.
所以最后我们在任何地方都不需要文件,只需文件的信息就足以进行测试。