繁体   English   中英

GitHub CI - 在 PHPUnit 测试中使用 PHP copy() 移动文件

[英]GitHub CI - move a file using PHP copy() in a PHPUnit test

我有一个用 PHP 8 编写的应用程序。我使用PHPUnit为它添加了一些单元测试。

在其中一项测试中,我使用 PHP 的副本function 将文件从一个位置移动到另一个位置。 这样做是为了测试通过将“虚拟”文件移动到文件在生产应用程序中的“真实”位置来下载文件的端点。

我的测试看起来像这样:

// tests/TestCase/Controller/DocsControllerTest.php
public function testDownload()
{
    $testFile = '75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf';
    copy('/full/path/to/testFiles/' . $testFile, '/webroot/docs/');

    // Download the file from the endpoint
    $id = 9; // File ID to download
    $this->get('/download/' . $id); 
  
    // This should return a HTTP 200 response containing the PDF
    $this->assertResponseCode(200, 'Downloading a valid PDF should produce a 200 response.');
}

解释一下上面的function:

  • 我们有一个名为75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf的测试文件。 这是一个具有适当编码的真实 PDF 文件。
  • 我们使用copy()将文件从我们保存一些测试文件的目录移动到生产 web 应用程序将真正存储文件的完整目录路径 ( /webroot/docs/ )。
  • 其余逻辑处理从端点下载文件。 $this->get向端点 ( /download/ ) 发出 HTTP GET 请求,该端点还传递适当的文件 ID。 文件的位置从 MySQL 数据库中查找,然后流式传输到浏览器,从而生成包含 PDF 的 HTTP 200 响应。

当我通过执行vendor/bin/phpunit --filter testDownload在本地运行 phpunit 时,这都有效:

PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
Time: 00:05.053, Memory: 20.00 MB
OK (1 test, 16 assertions)

它也适用于浏览器,即如果我向/download/9发出请求,我将收到相应的 PDF。

我遇到的问题是 GitHub。当我在那里运行单元测试时,CI 失败并出现以下错误:

Warning Error: copy(/home/runner/work/my-app/webroot/docs/75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf): Failed to open stream: No such file or directory
In [/home/runner/work/my-app/tests/TestCase/Controller/DocsControllerTest.php, line 745]

鉴于这在本地有效,我不明白为什么会发生此错误。 在 GitHub 的 CI 中使用copy()有什么限制吗?

/full/path/to/testFiles/中的目录和文件不是.gitignore 'd,因此它们与 repo 代码的 rest 一起提交。 因此,测试文件75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf存在于 GitHub 的代码库中。

我在运行 macOS Monterey (12.2) 的 Mac 上使用 PHPUnit 9.5.10、PHP 8.0。

它清楚地写着:

无法打开 stream:没有那个文件或目录

也许将.gitkeep添加到目标目录docs中? 也可能是源文件不存在。 人们通常可以从字面上理解这个错误消息。 虽然完全不清楚$this->get()甚至是什么,或者为什么您的 Mac 与运行 GitHub Action 有任何关系?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM