我有一些使用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