使用 vfsStream 测试 move_uploaded_file 和 is_uploaded_file

发布于 2024-12-11 10:48:38 字数 1018 浏览 5 评论 0原文

我尝试使用 PHPUnit 和 vfsStream 测试 move_uploaded_file 和 is_uploaded_file 。它们总是返回 false。

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 (或任何数据)吗? 谢谢。

I've tried to test move_uploaded_file and is_uploaded_file with PHPUnit and vfsStream. They always return false.

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

Is it possible? How do I do that?
Can I post a vfsStreamFile (or any data) without a form, just using PHP code?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迎风吟唱 2024-12-18 10:48:38

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

注意:从脚本内更改文件被视为篡改。

No. move_uploaded_file and is_uploaded_file are specifically designed to handle uploaded files. They include extra security checks to ensure that the file's not been tampered with in the time between the upload completing and the controlling script accessing the file.

Note: changing the file from within the script counts as tampering.

满身野味 2024-12-18 10:48:38

假设您正在使用类,您可以创建一个父类。

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

Assuming you're using classes you can create a parent class.

// 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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文