【问题标题】:PHPUnit compare xml string with xml file failed in PHP 5.6, but pass in PHP 7.0PHPUnit 比较 xml 字符串和 xml 文件在 PHP 5.6 中失败,但在 PHP 7.0 中通过
【发布时间】:2016-05-17 20:24:17
【问题描述】:

我使用 travis-ci 运行我的测试。在我的本地计算机上我检查了两个 PHP 版本,它很好。但是在 Travis 5.6 PHP 版本下的测试失败了。但测试在 PHP 7.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()
);

我有一个包含正确 XML 结构和值的 sample.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.

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

短解决方案无法在 PHP 5.6 中运行测试

【问题讨论】:

    标签: php xml symfony phpunit


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,但是如果我使用DomDocument,那么保留空格=false,也没有用。我没有任何想法。这很棘手。
    • 您确实必须将 xml 概述为完全相同。没有多余的空格或制表符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2013-04-19
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多