【问题标题】:Why is a warning is thrown when running a piece of code using php-cli, but a warning is not thrown when running the exact same code using PHPUnit?为什么使用 php-cli 运行一段代码时会抛出警告,但使用 PHPUnit 运行完全相同的代码时不会抛出警告?
【发布时间】:2014-01-22 09:20:50
【问题描述】:

我有一些使用php cli 和phpunit 的普通文件运行的代码。

让我感到困惑的是,代码在执行过程中应该会抛出警告,但它从不会在 PHPUnit 中抛出警告。

这是使用php run.php 运行时会引发警告的代码:

<?php
error_reporting(E_ALL);

include 'vendor/autoload.php';

$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style type="text/css">

a:active {
    font-size: 11px;
}

</style>

</head>
<body>
    <a href="google.com">blah</a>
</body>
</html>';

$htmldoc = new InlineStyle\InlineStyle($html);
$htmldoc->applyStylesheet($htmldoc->extractStylesheets()); //Causes a warning

echo($htmldoc->getHTML());

警告:

Warning: array_map(): An error occurred while invoking the map callback in /work/inlinecss-test/vendor/symfony/css-selector/Symfony/Component/CssSelector/XPath/Translator.php

不管我是通过php-fpm还是php运行文件,都会抛出警告。

但是,如果我将代码放入 PHPUnit 测试用例并运行它,则不会引发警告:

<?php
use InlineStyle\InlineStyle;

class InlineStyleTest extends \PHPUnit_Framework_TestCase
{
    public function testActivePseudoSelectors(){
        error_reporting(E_ALL);
        $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">

a:active {
    font-size: 11px;
}

</style>

</head>
<body>
    <a href="google.com">blah</a>
</body>
</html>';

        $htmldoc = new InlineStyle($html);
        $styles = $htmldoc->extractStylesheets();

        $htmldoc->applyStylesheet($styles);


        echo($htmldoc->getHTML());
    }
}

在这种情况下,测试很好,没有抛出任何警告:

OK (1 test, 0 assertions)

这是我的phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
         bootstrap="vendor/autoload.php"
>
    <testsuites>
        <testsuite name="tests">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

造成这种差异的原因可能是什么?

代码在这个仓库中:https://github.com/F21/inlinecss-test

上面运行的结果在travis-ci上:https://travis-ci.org/F21/inlinecss-test/jobs/17397488

【问题讨论】:

  • 我相信 PHPUnit 会强制关闭所有警告,所以它们只在日志中。
  • @StevenScott 在我的配置中,我已将其设置为 convertWarningsToExceptions = true。嗯。
  • 这可能与您尝试运行它的方式有关。 PHPUnit 使用自己的内部逻辑和全局处理,可能会停止 error_reporting(),但我认为您应该根据引导程序看到异常。也许在测试文件中尝试一些简单的代码(需要/包含)?

标签: phpunit warnings


【解决方案1】:

PHPUnit 将错误/警告/通知转换为异常,以便它们可以显示在测试结果中。

PHPUnit Docs

【讨论】:

  • 确实如此。但是,如果抛出警告然后转换为异常,它应该会显示出来,因为我没有捕捉到它。例如,如果我使用 trigger_error() 触发了警告,它会显示为我运行测试。
猜你喜欢
  • 1970-01-01
  • 2021-07-27
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多