2011-10-20 3 views
5

PHPUnit 및 vfsStream을 사용하여 move_uploaded_file 및 is_uploaded_file을 테스트 해 보았습니다. 그들은 항상 거짓을 반환합니다.vfsStream을 사용하여 move_uploaded_file 및 is_uploaded_file을 테스트하십시오.

public function testShouldUploadAZipFileAndMoveIt() 
{ 
    $_FILES = array('fieldName' => array(
     'name'  => 'file.zip', 
     'type'  => 'application/zip', 
     'tmp_name' => 'vfs://root/file.zip', 
     'error' => 0, 
     'size'  => 0, 
    )); 

    vfsStream::setup(); 
    $vfsStreamFile = vfsStream::newFile('file.zip'); 
    vfsStreamWrapper::getRoot() 
     ->addChild($vfsStreamFile); 

    $vfsStreamDirectory = vfsStream::newDirectory('/destination'); 
    vfsStreamWrapper::getRoot() 
     ->addChild($vfsStreamDirectory); 

    $fileUpload = new File_Upload(); 
    $fileUpload->upload(
     vfsStream::url('root/file.zip'), 
     vfsStream::url('root/destination/file.zip') 
    ); 

    $this->assertFileExists(vfsStream::url('root/destination/file.zip')); 
} 

가능합니까? 어떻게해야합니까? PHP 코드 만 사용하여 양식없이 vfsStreamFile (또는 모든 데이터)을 게시 할 수 있습니까? 감사합니다.

답변

2

번호 move_uploaded_file 및 is_uploaded_file은 업로드 된 파일을 처리하도록 특별히 설계되었습니다. 여기에는 업로드 완료와 파일에 액세스하는 제어 스크립트 사이의 시간에 파일이 변조되지 않았는지 확인하는 추가 보안 검사가 포함됩니다.

스크립트 내에서 파일을 변경하면 조작이 잘못 계산됩니다.

+1

어떻게 이러한 단위 테스트를 사용합니까? 감사. – user972959

+0

실례지만. 나는 phpunit을 사용한 적이 없다. 여기에 몇 가지 것들이 있습니다 : http://stackoverflow.com/questions/3402765/how-can-i-write-tests-for-file-upload-in-php phpunit을 위해 특별히 아닙니다. –

1

클래스를 사용 중이라고 가정하면 상위 클래스를 만들 수 있습니다.

// this is the class you want to test 
class File { 
    public function verify($file) { 
    return $this->isUploadedFile($file); 
    } 
    public function isUploadedFile($file) { 
    return is_uploaded_file($file); 
    } 
} 

// for the unit test create a wrapper that overrides the isUploadedFile method 
class FileWrapper extends File { 
    public function isUploadedFile($file) { 
    return true; 
    } 
} 

// write your unit test using the wrapper class 
class FileTest extends PHPUnit_Framework_TestCase { 
    public function setup() { 
    $this->fileObj = new FileWrapper; 
    } 

    public function testFile() { 
    $result = $this->fileObj->verify('/some/random/path/to/file'); 
    $this->assertTrue($result); 
    } 
} 
관련 문제