2011-10-20 10 views
5

के साथ मैंने move_uploaded_file और is_uploaded_file का परीक्षण करने के लिए PHPUnit और vfsStream के साथ परीक्षण करने का प्रयास किया है। वे हमेशा झूठी वापसी करते हैं।टेस्ट move_uploaded_file और is_uploaded_file vfsStream

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')); 
} 

क्या यह संभव है? मैं उसको कैसे करू? क्या मैं एक फॉर्म के बिना vfsStreamFile (या कोई डेटा) पोस्ट कर सकता हूं, बस PHP कोड का उपयोग कर? धन्यवाद।

उत्तर

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); 
    } 
} 
संबंधित मुद्दे