如何为文件下载创建 PHP- 单元测试?
How to create a PHP-Unit test for a file download?
这个话题想了半天没有结果,希望有大佬指教下phpUnit-Test下载。我如何为这个函数创建一个 phpUnit 测试,它执行一个 zip 文件的下载?或者是否有其他通常的做法来测试这样的逻辑?
public function createOutput($zipFolderName) {
$zipFolderName = $zipFolderName."/imageResizer.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipFolderName);
header('Content-Length: ' . filesize($zipFolderName));
readfile($zipFolderName);
}
我觉得这个功能有几个组成部分,应该测试每个部分以确保它正确完成:
- 建立正确的
$zipFolderName
路径
- 设置正确的headers
- 计算正确的文件大小
- 读取文件
看起来某种文件处理程序抽象可以帮助您轻松地对该方法进行单元测试。
class FileUtils {
public getDownloadFileName($folderName) {}
public getFileSize($file) {}
public readFile($file) {}
}
现在您可以将文件实用程序实例注入到您的函数中。对于您的生产代码,此 class 会将文件操作委托给实际文件系统,但您的测试会提供一个 stub/mock 允许您断言您的单元正在执行预期的操作。
public function createOutput($fileUtilsInstance, $zipFolderName) {
$zipFolderName = $fileUtilsInstance->getDownloadFileName($zipFolderName);
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipFolderName);
header('Content-Length: ' . $fileUtilsInstance->getFilesize($zipFolderName));
$fileUtilsInstance->readFile($zipFolderName);
}
可以将相同的策略应用于 header
方法。
我建议你vfsStream图书馆:
vfsStream is a PHP stream wrapper for a virtual file system that may
be helpful in unit tests to mock the real file system. It can be used
with any unit test framework, like PHPUnit or SimpleTest.
同样有用的信息:
- PHPUNIT DOC - Mocking the Filesystem section
- 本文中真正有用的代码示例 - MOCKING THE FILE SYSTEM USING PHPUNIT AND VFSSTREAM
希望对您有所帮助。如果您需要一些示例,请告诉我
这个话题想了半天没有结果,希望有大佬指教下phpUnit-Test下载。我如何为这个函数创建一个 phpUnit 测试,它执行一个 zip 文件的下载?或者是否有其他通常的做法来测试这样的逻辑?
public function createOutput($zipFolderName) {
$zipFolderName = $zipFolderName."/imageResizer.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipFolderName);
header('Content-Length: ' . filesize($zipFolderName));
readfile($zipFolderName);
}
我觉得这个功能有几个组成部分,应该测试每个部分以确保它正确完成:
- 建立正确的
$zipFolderName
路径 - 设置正确的headers
- 计算正确的文件大小
- 读取文件
看起来某种文件处理程序抽象可以帮助您轻松地对该方法进行单元测试。
class FileUtils {
public getDownloadFileName($folderName) {}
public getFileSize($file) {}
public readFile($file) {}
}
现在您可以将文件实用程序实例注入到您的函数中。对于您的生产代码,此 class 会将文件操作委托给实际文件系统,但您的测试会提供一个 stub/mock 允许您断言您的单元正在执行预期的操作。
public function createOutput($fileUtilsInstance, $zipFolderName) {
$zipFolderName = $fileUtilsInstance->getDownloadFileName($zipFolderName);
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipFolderName);
header('Content-Length: ' . $fileUtilsInstance->getFilesize($zipFolderName));
$fileUtilsInstance->readFile($zipFolderName);
}
可以将相同的策略应用于 header
方法。
我建议你vfsStream图书馆:
vfsStream is a PHP stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.
同样有用的信息:
- PHPUNIT DOC - Mocking the Filesystem section
- 本文中真正有用的代码示例 - MOCKING THE FILE SYSTEM USING PHPUNIT AND VFSSTREAM
希望对您有所帮助。如果您需要一些示例,请告诉我