Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让php代码覆盖率在phpt测试用例上运行?_Php_File Upload_Phpunit_Code Coverage_Phpt - Fatal编程技术网

如何让php代码覆盖率在phpt测试用例上运行?

如何让php代码覆盖率在phpt测试用例上运行?,php,file-upload,phpunit,code-coverage,phpt,Php,File Upload,Phpunit,Code Coverage,Phpt,我有一个测试环境,可以运行产品的组件测试。我发现最近很难测试并成功模拟php的is\u uploaded\u file()和move\u uploaded\u file(),但经过大量搜索和研究,我发现了PHPT。这在测试这些方法和文件上传期望方面确实帮助了我很多。这不是一个关于文件上传的问题,而是如何将phpt测试用例集成到基本phpunit测试用例中,以便代码覆盖率也能为正在测试的方法运行。下面是一些代码摘录: files.php class prFiles { // Instanc

我有一个测试环境,可以运行产品的组件测试。我发现最近很难测试并成功模拟php的
is\u uploaded\u file()
move\u uploaded\u file()
,但经过大量搜索和研究,我发现了PHPT。这在测试这些方法和文件上传期望方面确实帮助了我很多。这不是一个关于文件上传的问题,而是如何将phpt测试用例集成到基本phpunit测试用例中,以便代码覆盖率也能为正在测试的方法运行。下面是一些代码摘录:

files.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;

    }
}
class UtilitiesFilesTransferTest extends PHPUnit_Extensions_PhptTestCase
{

    /**
     * Constructs a new UtilitiesFilesTransferTest.
     *
     */
    public function __construct()
    {
        parent::__construct(dirname(__FILE__) . '/_phpt/file_upload_test.phpt');

    }

}
文件上传测试.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)
这样一切都会好起来。但我似乎无法得到我正在测试的传输方法的任何覆盖。有人能帮我吗

编辑:“我的覆盖率”命令如下所示:

@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%

我不知道PHPUnit为什么不为您收集这些保险数据。这可能与它如何使用(或不使用)XDebug有关

您可以通过使用一个测试覆盖率工具来解决这个问题,该工具不依赖于PHPUNit或XDebug的工作方式


我们将收集任何PHP脚本中任何函数的测试覆盖率,不管该函数是如何执行的。提供PHPT调用的函数执行的覆盖率数据应该没有问题。

因为PhpUnit有用于运行单独进程中发生的PHPT文件的自定义实现,所以将代码覆盖率与PhpUnit集成可能确实非常困难

然而,如果您所需要的只是覆盖范围(或者您愿意自己做一些后处理),那么它就变得非常琐碎

在最简单的形式中,您所需要做的就是从PHPT文件调用xDebug。使用(和用于类自动加载的Composer)您的
--文件--
部分可能如下所示:

--FILE--
<?php
/* autoload classes */
require __DIR__ . '/../../../vendor/autoload.php';

/* Setup and start code coverage */
$coverage = new \PHP_CodeCoverage;
$coverage->start('test');

/* run logic */
$prFiles = prFiles::getInstance()->transfer(
    $_FILES,
    __DIR__ . '/../_data/',
    'test.txt'
);
var_dump($prFiles);


/* stop and output coverage data */
$coverage->stop();
$writer = new \PHP_CodeCoverage_Report_PHP;
$writer->process($coverage, __DIR__ . '/../../../build/log/coverage-data.php');

?>
封面
类:

<?php

class cover
{
    private static $coverage;

    /* Setup and start code coverage */
    public static function start()
    {
        self::$coverage = new \PHP_CodeCoverage;

        /* Make sure this file is not added to the coverage data */
        $filter = self::$coverage->filter();
        $filter->addFileToBlacklist(__FILE__);

        self::$coverage->start('test');
    }

    /* stop and output coverage data */
    public static function stop()
    {
        self::$coverage->stop();

        $writer = new \PHP_CodeCoverage_Report_PHP;
        $writer->process(self::$coverage, __DIR__ . '/../build/log/coverage-data.php');
    }
}   

您的环境是什么?(PHP/XDebug/PHPUnit版本和操作系统)您的cgi
PHP.ini
文件中是否启用了XDebug?使用phpt测试上传时使用CGI,而不是普通的CLI。@cweiske有关于这方面的文档吗?这是我第一次听说它,我渴望学习@CM这与我的环境无关,但与thx有关。“我第一次听说它”-什么?xdebug?php.ini?cgi?哈哈,phpt使用cgi。我不熟悉phpt测试,而不熟悉php和单元测试本身。我怀疑它能做到这一点,而且由于它是一个没有任何公共文档的商业产品,因此无法验证您的说法。@oweiske:您的怀疑是基于什么技术事实或具体经验?是的,这是一种商业产品。您可以下载一个评估版本,自己阅读文档,然后自己进行验证。@etbal:如果您测试的metnod M在另一个PHP脚本中,并且PHPT像正常运行一样运行该PHP脚本,我真的相信这会起作用,因为我们的工具对代码进行了测试。这是真的,它不是一个PHPUnit修复,它的感觉是它不是一个答案,你的问题。但是如果你不能得到答案(因为,我认为,你一直在使用的工具之间存在着相互作用),那么你拥有的工具就坏了,你需要另一个工具。我假设你真的想解决更大的问题,“当我运行PHPT测试时收集覆盖率”,而且它确实解决了这个问题,我发现这里的反应很有趣。OP有一个问题,他非常想解决,以便发布悬赏。好几天没有其他人发布任何回复。我发布了一个我认为有效的答案。可以下载并试用。然而,每个人都讨厌它,因为它恰好是一个商业产品。(其他读者的背景:OP最初以+100的悬赏发布了这个问题。悬赏窗口已过期;未授予,请问我是否感到惊讶)。
<?php

class cover
{
    private static $coverage;

    /* Setup and start code coverage */
    public static function start()
    {
        self::$coverage = new \PHP_CodeCoverage;

        /* Make sure this file is not added to the coverage data */
        $filter = self::$coverage->filter();
        $filter->addFileToBlacklist(__FILE__);

        self::$coverage->start('test');
    }

    /* stop and output coverage data */
    public static function stop()
    {
        self::$coverage->stop();

        $writer = new \PHP_CodeCoverage_Report_PHP;
        $writer->process(self::$coverage, __DIR__ . '/../build/log/coverage-data.php');
    }
}