drv16759 2012-07-18 13:43
浏览 37
已采纳

为什么我在PHPUnit中使用PHPT获得“BORKED --CLEAN- section”?

I have some PHPT tests that are being run in PHPUnit. These tests include a --CLEAN-- section that handles the deletion of files created during the test. Running the PHPT tests at the command line using pear run-tests works fine, but when they are run though PHPUnit I get the following error:

BORKED --CLEAN-- section! output:
X-Powered-By: PHP/5.3.10-1ubuntu3.2
Content-type: text/html

This obviously looks like an HTTP header, but there is no further text, and the actions in the clean section still work as planned. Removing the CLEAN section removes the problem, but I would rather not have the test cleanup done outside of the PHPT file. Any ideas on what these are caused by, and how to get rid of them? They're not breaking the tests, but they look untidy in the output.

A PHPT test (the POST file data is truncated to protect my employment):

--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)

A Zend/PHPUnit test wrapper:

<?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;
    }
}

The test suite to trigger the wrappers:

<?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;
    }
}
  • 写回答

1条回答 默认 最新

  • doucitan2544 2012-07-19 08:42
    关注

    This is caused by using the PHP CGI executable to run your tests instead of the CLI version. The CGI binary will append the two headers in front of the response, and the PEAR module used for testing will scream bork bork and bloody murder if there's output provided from the code run in the --CLEAN-- section. Point the tests to the correct PHP binary and the issue should go away.

    :~$ echo "" | php5-cgi
    X-Powered-By: PHP/5.3.10-1ubuntu3.2
    Content-type: text/html
    

    Update: This is caused by an error in PEAR_RunTest where it doesn't reset which interpreter to use when running the --CLEAN-- section. I've patched PEAR_RunTest to fix the issue and submitted a PR for the issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算