PHPUnit声明相同的HTML结构,而不管空白 [英] PHPUnit asserting identical HTML structure regardless of whitespace

查看:91
本文介绍了PHPUnit声明相同的HTML结构,而不管空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令行脚本,用于生成一些HTML,我尝试使用PHPUnit进行单元测试。请注意,此HTML 浏览器不可见,因此Selenium不适合用于此目的。



我只关注比较实际的HTML结构。我使用 assertEquals(),但实际的字符串可能由于各种空格字符而不完全相同。 ()

  public function testHtmlIsIdentical()
{
$ expectedReport = file_get_contents('expected.html');
$ this-> report-> setupSomeData('test data');
$ actualReport = $ this-> report-> generateHtml();
$ this-> assertEquals($ expectedReport,$ actualReport);

$ / code>

我能做些什么来比较结构(节点) HTML而不是HTML的字符串?有没有PHPUnit的功能允许这个?是否有独立的库用于比较HTML?



解决方案:



PHPUnit有用于比较XML的断言:





在这种情况下, assertXmlStringEqualsXmlFile 完美适用:

  public function testHtmlIsIdentical()
{
$ this-> report-> setupSomeData('test data');
$ this-> assertXmlStringEqualsXmlFile('expected.html',$ this-> report-> generateHtml());
}


解决方案



如果所有不同的东西都是多余的空格,试试看:

  $ expectedDom = new DomDocument(); 
$ expectedDom-> loadHTMLFile('expected.html');
$ expectedDom-> preserveWhiteSpace = false;

$ actualDom = new DomDocument();
$ actualDom-> loadHTML($ this-> report-> generateHtml());
$ actualDom-> preserveWhiteSpace = false;

$ this-> assertEquals($ expectedDom-> saveHTML(),$ actualDom-> saveHTML());

请参阅: preservewhitespace



值得关注的是 assertEqualXMLStructure

  assertEqualXMLStructure(
DOMElement $ expectedElement,
DOMElement $ actualElement
[,boolean $ checkAttributes = FALSE,
string $ message ='']

as这可以用来比较html。



但是,你可能会遇到空白问题,所以也许你需要在比较之前去掉这些问题。使用DOM的好处是,如果文档不匹配,您可以获得更好的错误报告。



另一种测试xml / html代的方法如下所示: / p>

实用PHPUnit :测试XML生成


I have a command line script that generates some HTML that I am trying to unit test using PHPUnit. Note that this HTML is not seen by a browser, so Selenium is not the right solution for this.

I'm only concerned with comparing the actual HTML structure. I'm using assertEquals() but the actual strings may not be exactly identical because of various whitespace characters.

public function testHtmlIsIdentical()
{
    $expectedReport = file_get_contents('expected.html');
    $this->report->setupSomeData('test data');
    $actualReport = $this->report->generateHtml();
    $this->assertEquals($expectedReport, $actualReport);
}

What can I do in order to compare the structure (the nodes) of the HTML instead of the strings of HTML? Is there a feature of PHPUnit that allows this? Is there a standalone library for comparing HTML?

Solution:

PHPUnit has assertions for comparing XML:

The assertXmlStringEqualsXmlFile works perfectly in this scenario:

public function testHtmlIsIdentical()
{
    $this->report->setupSomeData('test data');
    $this->assertXmlStringEqualsXmlFile('expected.html', $this->report->generateHtml());
}

解决方案

Well there is DomDocument and if you want to check that the order of html elements matches you could use that.

If everything that differes is redundant whitespace maybe try:

$expectedDom = new DomDocument();
$expectedDom->loadHTMLFile('expected.html');
$expectedDom->preserveWhiteSpace = false;

$actualDom = new DomDocument();
$actualDom->loadHTML($this->report->generateHtml());
$actualDom->preserveWhiteSpace = false;

$this->assertEquals($expectedDom->saveHTML(), $actualDom->saveHTML());

See: preservewhitespace

What could also be worth looking into is assertEqualXMLStructure:

assertEqualXMLStructure(
    DOMElement $expectedElement, 
    DOMElement $actualElement
    [, boolean $checkAttributes = FALSE, 
       string $message = '']
)

as this can be used to compare html as well.

But you might run into issues with the whitespaces again so maybe you need to strip those before comparing. The benefit of using DOM is that you get much nicer error reporting in case the docs don't match.

Another way of testing xml/html generation is described in:

Practical PHPUnit: Testing XML generation

这篇关于PHPUnit声明相同的HTML结构,而不管空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆