我有一个运行产品组件测试的测试环境。我发现最近很难测试并成功模拟 php is_uploaded_file()
,move_uploaded_file()
但经过大量搜索和研究后,我发现了 PHPT。这确实对我测试这些方法和文件上传的期望有很大帮助。这不是关于文件上传的问题,而是如何将 phpt 测试用例集成到基本的 phpunit 测试用例中,以便对正在测试的方法也运行代码覆盖率。以下是一些代码摘录:
文件.php
class prFiles
{
// Instance methods here not needed for the purpose of this question
// ......
public function transfer(array $files, $target_directory,
$new_filename, $old_filename = '')
{
if ( (isset($files['file']['tmp_name']) === true)
&& (is_uploaded_file($files['file']['tmp_name']) === true) )
{
// Only check if old filename exists
if ( (file_exists($target_directory . '/' . $old_filename) === true)
&& (empty($old_filename) === false) )
{
unlink($target_directory . $old_filename);
}
$upload = move_uploaded_file(
$files['file']['tmp_name'],
$target_directory . '/' . $new_filename
);
if ( $upload === true )
{
return true;
}
else
{
return false;
}
}
return false;
}
}
文件上传测试.phpt
--TEST--
Test the prFiles::transfer() the actual testing of the file uploading.
--POST_RAW--
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryfywL8UCjFtqUBTQn
------WebKitFormBoundaryfywL8UCjFtqUBTQn
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
This is some test text
------WebKitFormBoundaryfywL8UCjFtqUBTQn
Content-Disposition: form-data; name="submit"
Upload
------WebKitFormBoundaryfywL8UCjFtqUBTQn--
--FILE--
<?php
require_once dirname(__FILE__) . '/../../../src/lib/utilities/files.php';
$prFiles = prFiles::getInstance()->transfer(
$_FILES,
dirname(__FILE__) . '/../_data/',
'test.txt'
);
var_dump($prFiles);
?>
--EXPECT--
bool(true)
实用程序文件TransferTest.php
class UtilitiesFilesTransferTest extends PHPUnit_Extensions_PhptTestCase
{
/**
* Constructs a new UtilitiesFilesTransferTest.
*
*/
public function __construct()
{
parent::__construct(dirname(__FILE__) . '/_phpt/file_upload_test.phpt');
}
}
所以一切正常。但我似乎无法得到任何关于我正在测试的传输方法的报道。请问有人可以帮我吗?
编辑:我的覆盖命令如下所示:
@echo off
echo.
if not "%1"=="" goto location
goto default
:location
set EXEC=phpunit --coverage-html %1 TestSuite
goto execute
:default
set EXEC=phpunit --coverage-html c:\xampp\htdocs\workspace\coverage\project TestSuite
:execute
%EXEC%