首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP5.6中,PHPUnit将xml字符串与xml文件进行比较失败,但在PHP7.0中通过

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

Stack Overflow用户
提问于 2016-05-18 04:24:17
回答 1查看 182关注 0票数 0

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

代码语言:javascript
复制
$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的日志文件:

代码语言:javascript
复制
$ 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文件,但我也遇到了同样的问题。

短解决方案不能在PHP5.6中运行测试

EN

回答 1

Stack Overflow用户

发布于 2016-05-18 05:53:03

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

如果你使用phpstorm并在其中运行你的单元测试。你可以看到不同之处,并对它们进行比较。通过单击控制台中的assert错误消息,Phpstorm将显示一个diff窗口。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37285630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档