PHPUnit将xml字符串与xml文件进行比较在PHP5.6中失败,但在PHP7.0中通过


PHPUnit compare xml string with xml file failed in PHP 5.6, but pass in PHP 7.0

我使用travis-ci来运行我的测试。在我的本地计算机上,我检查了两个PHP版本,它很好。但在Travis中,5.6 PHP版本下的测试失败了。但是测试是在PHP7.0版本中通过的。我不知道真正的问题是什么。

$client = static::createClient();
$crawler = $client->request('GET', '/sitemap.xml');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertXmlStringEqualsXmlFile(
    __DIR__ . '/../Resources/sample.xml',
    $client->getResponse()->getContent()
);

我有一个sample.xml,它包含正确的xml结构和值。我通过/sitemap.xml链接生成sitemap.xml。

我总是犯错误:两个DOM不相等。

我电脑上的PHPUnit版本是5.1.3。

这里是Travis ci的日志文件:

$ php --version
PHP 5.6.5 (cli) (built: Feb 12 2015 01:41:10) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.7, Copyright (c) 2002-2015, by Derick Rethans
$ composer --version
You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
Composer version 1.2-dev (32a8a60695b9b9c9e2da01406ac3713903182669) 2016-05-17 13:08:17
before_install.1
0.00s$ if [[ ! $PHP = ${MIN_PHP%.*} && $TRAVIS_PULL_REQUEST != false ]]; then skip=1; fi
before_install.2
2.11s$ composer self-update --stable;
You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
Updating to version 1.1.1 (stable channel).
    Downloading: Connecting...    Downloading: 100%
Use composer self-update --rollback to return to version 32a8a60695b9b9c9e2da01406ac3713903182669
before_install.3
0.00s$ if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/symfony:${SYMFONY_VERSION}; fi
install
60.18s$ if [ ! $skip ]; then composer update --prefer-dist; fi
You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing psr/log (1.0.0)
    Loading from cache
  - Installing doctrine/lexer (v1.0.1)
    Loading from cache
  - Installing doctrine/annotations (v1.2.7)
    Loading from cache
  - Installing doctrine/collections (v1.3.0)
    Loading from cache
  - Installing doctrine/cache (v1.6.0)
    Loading from cache
  - Installing doctrine/inflector (v1.1.0)
    Loading from cache
  - Installing doctrine/common (v2.6.1)
    Loading from cache
  - Installing symfony/polyfill-util (v1.1.1)
    Loading from cache
  - Installing paragonie/random_compat (v1.4.1)
    Loading from cache
  - Installing symfony/polyfill-php70 (v1.1.1)
    Loading from cache
  - Installing symfony/polyfill-php56 (v1.1.1)
    Loading from cache
  - Installing symfony/polyfill-mbstring (v1.1.1)
    Loading from cache
  - Installing symfony/symfony (v3.0.6)
    Loading from cache
  - Installing symfony/polyfill-intl-icu (v1.1.1)
    Loading from cache
  - Installing twig/twig (v1.24.0)
    Loading from cache
  - Installing sensio/framework-extra-bundle (v3.0.16)
    Loading from cache
  - Installing doctrine/instantiator (1.0.5)
    Loading from cache
  - Installing doctrine/dbal (v2.5.4)
    Loading from cache
  - Installing doctrine/orm (v2.5.4)
    Loading from cache
  - Installing doctrine/doctrine-cache-bundle (1.3.0)
    Loading from cache
  - Installing jdorn/sql-formatter (v1.2.17)
    Loading from cache
  - Installing doctrine/doctrine-bundle (1.6.2)
    Loading from cache
  - Installing myclabs/deep-copy (1.5.1)
    Loading from cache
  - Installing sebastian/version (1.0.6)
    Loading from cache
  - Installing sebastian/resource-operations (1.0.0)
    Loading from cache
  - Installing sebastian/global-state (1.1.1)
    Loading from cache
  - Installing sebastian/recursion-context (1.0.2)
    Loading from cache
  - Installing sebastian/exporter (1.2.1)
    Loading from cache
  - Installing sebastian/environment (1.3.7)
    Loading from cache
  - Installing sebastian/diff (1.4.1)
    Loading from cache
  - Installing sebastian/comparator (1.2.0)
    Loading from cache
  - Installing phpdocumentor/reflection-docblock (2.0.4)
    Loading from cache
  - Installing phpspec/prophecy (v1.6.0)
    Loading from cache
  - Installing phpunit/php-text-template (1.2.1)
    Loading from cache
  - Installing phpunit/phpunit-mock-objects (3.1.3)
    Loading from cache
  - Installing phpunit/php-timer (1.0.8)
    Loading from cache
  - Installing sebastian/code-unit-reverse-lookup (1.0.0)
    Loading from cache
  - Installing phpunit/php-token-stream (1.4.8)
    Loading from cache
  - Installing phpunit/php-file-iterator (1.4.1)
    Loading from cache
  - Installing phpunit/php-code-coverage (3.3.1)
    Loading from cache
  - Installing phpunit/phpunit (5.1.7)
    Loading from cache
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
sensio/framework-extra-bundle suggests installing symfony/psr-http-message-bridge (To use the PSR-7 converters)
doctrine/doctrine-cache-bundle suggests installing symfony/security-acl (For using this bundle to cache ACLs)
sebastian/global-state suggests installing ext-uopz (*)
phpdocumentor/reflection-docblock suggests installing dflydev/markdown (~1.0)
phpdocumentor/reflection-docblock suggests installing erusev/parsedown (~1.0)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Generating autoload files
1.10s$ phpunit Tests/;
PHPUnit 5.1.7 by Sebastian Bergmann and contributors.
F....                                                               5 / 5 (100%)
Time: 973 ms, Memory: 28.25MB
There was 1 failure:
1) RepoSitemapBundle'Tests'Controller'DefaultControllerTest::testSitemapAction
Failed asserting that two DOM documents are equal.
--- Expected
+++ Actual
@@ @@
 <?xml version="1.0"?>
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9">
-    <url>
-        <loc>http://www.localhosttest/1</loc>
-        <lastmod>2010-01-01</lastmod>
-        <changefreq>daily</changefreq>
-        <priority>0.9</priority>
-    </url>
-</urlset>
+                        <url>
+                <loc>http://www.localhosttest/1</loc>
+                <lastmod>2010-01-01</lastmod>
+                <changefreq>daily</changefreq>
+                <priority>0.9</priority>
+            </url>
+            </urlset>
/home/travis/build/botalaszlo/tmp-sitemap-bundle/Tests/Controller/DefaultControllerTest.php:37
phar:///home/travis/.phpenv/versions/5.6.5/bin/phpunit/phpunit/TextUI/Command.php:152
phar:///home/travis/.phpenv/versions/5.6.5/bin/phpunit/phpunit/TextUI/Command.php:104
FAILURES!
Tests: 5, Assertions: 10, Failures: 1.
The command "phpunit Tests/;" exited with 1.

但在PHP7.0中,这并没有失败。我曾尝试用SimpleXml加载sample.xml文件,但遇到了同样的问题。

短期解决方案不在PHP 5.6 中运行测试

这是因为输出中的字符串和文件中的字符串包含彼此不相等的空格和新行。

如果您使用phpstorm并在其中运行单元测试。你可以看到差异并进行比较。Phpstorm将通过单击控制台中的断言错误消息向您显示一个diff窗口。