php - 使用 vfsStream 测试 move_uploaded_file 和 is_uploaded_file

标签 php unit-testing file tdd phpunit

我尝试使用 PHPUnit 和 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(或任何数据)吗? 谢谢。

最佳答案

没有。 move_uploaded_fileis_uploaded_file 专门用于处理上传的文件。它们包括额外的安全检查,以确保文件在上传完成和控制脚本访问文件之间的时间内未被篡改

注意:从脚本中更改文件算作篡改。

关于php - 使用 vfsStream 测试 move_uploaded_file 和 is_uploaded_file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7839855/

相关文章:

c - 从当前目录中删除文件

php - 无法使用 PHP、MySQL 和 Volley 进行 Android 登录

php - 将字典从 Swift 发布到 PHP

c# - 犀牛模拟 : How to verify that a constructor was called

angularjs - Angular 单元测试中scope = {} 和scope = $rootScope.$new() 之间的区别

c - C 中的文件冗余

php - 我如何限制 CakePHP 中的某些内容?

PHP读取文件错误

c# - 数据库集成测试结构

java - 如何通过扫描仪读取文本文件