【发布时间】:2012-07-18 13:43:16
【问题描述】:
我有一些 PHPT 测试正在 PHPUnit 中运行。这些测试包括一个--CLEAN-- 部分,用于处理测试期间创建的文件的删除。使用pear run-tests 在命令行运行 PHPT 测试工作正常,但是当它们通过 PHPUnit 运行时,我收到以下错误:
BORKED --CLEAN-- 部分!输出:
X-Powered-By:PHP/5.3.10-1ubuntu3.2
内容类型:text/html
这显然看起来像一个 HTTP 标头,但没有进一步的文本,并且干净部分中的操作仍然按计划工作。删除 CLEAN 部分可以解决问题,但我宁愿不在 PHPT 文件之外进行测试清理。关于这些是由什么引起的以及如何摆脱它们的任何想法?他们没有破坏测试,但在输出中看起来很不整洁。
PHPT 测试(POST 文件数据被截断以保护我的工作):
--TEST--
Test file upload
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------168481652011378167832091260413
Content-Length: 3510
-----------------------------168481652011378167832091260413
Content-Disposition: form-data; name="code"
te
-----------------------------168481652011378167832091260413
Content-Disposition: form-data; name="name"
test
-----------------------------168481652011378167832091260413
Content-Disposition: form-data; name="file"; filename="test.xml"
Content-Type: text/xml
<?xml version="1.0" encoding="utf-8"?>
<testElement>
<innerElement />
</testElement>
-----------------------------168481652011378167832091260413--
--FILE--
<?php
// Zend/PHPUnit bootstrap
require_once __DIR__ . '/../../../bootstrap.php';
$upload = new My_File_Upload();
var_dump($upload->handleFile(
'file',
APPLICATION_PATH . '/../datafiles/request/',
'application/xml',
'testname'
));
var_dump(file_exists(APPLICATION_PATH . '/../datafiles/request/testname.xml'))
?>
--CLEAN--
<?php
$file = dirname(__FILE__) . '/../../../../datafiles/request/testname.xml';
if(file_exists($file)) unlink($file);
?>
--EXPECT--
string(12) "testname.xml"
bool(true)
一个 Zend/PHPUnit 测试包装器:
<?php
require_once 'PHPUnit/Extensions/PhptTestCase.php';
/**
* UploadFileHandleTest
*
* PHPUnit wrapper for the phpt test file UploadFileHandleTest.phpt
*
*/
class My_File_UploadFileHandle extends PHPUnit_Extensions_PhptTestCase
{
/**
* Test file uploads
*
* Test the file uploads using associated .phpt test files. These test have to
* be abstracted to the php-cgi environment to permit accurate manipulation of
* the $_FILES superglobal.
*
* @see http://qafoo.com/blog/013_testing_file_uploads_with_php.html
*
* @covers My_File_Upload::handleFile
*
* @return void
*/
public function __construct()
{
parent::__construct(__DIR__ . '/UploadFileHandleTest.phpt');
}
/**
* Implement missing hasOutput method for PHPUnit
*
* @return boolean
*/
public function hasOutput()
{
return false;
}
}
触发包装器的测试套件:
<?php
require_once 'UploadFileExists.php';
require_once 'UploadFileHandle.php';
require_once 'UploadFileHandleNoName.php';
require_once 'UploadFileHandleExceptions.php';
class My_File_UploadTestSuite extends PHPUnit_Framework_TestSuite
{
public static function suite()
{
$suite = new My_File_UploadTestSuite('My_File_UploadFileTests');
$suite->addTest(new My_File_UploadFileExists());
$suite->addTest(new My_File_UploadFileHandle());
$suite->addTest(new My_File_UploadFileHandleNoName());
$suite->addTest(new My_File_UploadFileHandleExceptions());
return $suite;
}
}
【问题讨论】:
-
Content-Length: 3510和请求正文之间应该有一个双 CRLF(即添加一个额外的空行)。不知道这是否会解决它,但这绝对是 a 问题。还要验证3510是消息正文的正确字节长度 - 它看起来不像是基于您发布的数据。 -
感谢您的回答,我会试一试。在此示例中已截断 POST 文件数据以保护 IP,因此长度应正确。它是从 Firefox 中的真正标头中获取的。
-
在内容长度后添加额外的一行并没有影响。测试确实运行正常,它似乎只是在事后才吐出这条消息。
-
我认为这与 --CLEAN-- 部分路径中的所有
/..s 有关。testname.xml真的在清理过程中被删除了吗? -
是的,实际的清理操作执行得很好。