开发者

Robust unit-testing of HTML in PHP

I'm adding unit-tests to an older PHP codebase at work. I will be testing开发者_开发百科 and then rewriting a lot of HTML generation code and currently I'm just testing if the generated strings are identical to the expected string, like so: (using PHPUnit)

public function testConntype_select() {
    $this->assertEquals(
        '<select><option value="blabla">Some text</option></select>',
        conntype_select(1); // A value from the test dataset.
    );
}

This way has the downside that attribute ordering, whitespace and a lot of other irrelevant details are tested as well. I'm wondering if there are any better ways to do this. For example if there are any good and easy ways to compare the generated DOM trees. I found very similar questions for ruby, but couldn't find anything for PHP.


Look at Zend_Test_PHPUnit. Here you may query the DOM using:

assertQuery() or assertXpath();


I'm dealing with the same issues. LOL! What I think I'm going to do is use the DOMDocument at some point. But for now all I'm doing is writing coverage tests, which is what your doing. Here is one of my tests. The same as yours:

public function testUpdateSkuTable() {
    $formName = "sku_id";
    $key = $formName;
    $sku = array('sku_id' => 'sku id', 'description' => 'generic description');

    $expected = "<div class='sku_editor_container'><form id='sku_edit_form'><div class='section'><div>SKU Edit Information For: <div id='sku_id' style='color:blue;'>sku_id</div></div></div><div class='blank'></div><div class='section'>SKU Data Entry<table class='sku_table'><tr><td>sku_id:</td><td><input type='text' name='sku_id' id='sku_id' value='sku id' size='50'/></td></tr><tr><td>description:</td><td><input type='text' name='description' id='description' value='generic description' size='50'/></td></tr></table></div><div class='blank'></div><input type='submit' name='sku_submit' value='Save SKU Edit' class='sku_submit'></form></div>";
    $actual = $this->view->editorUpdateSku($formName, $sku, $key);

    $this->assertEquals($expected, $actual);
}


The bigger issue here is you want your unit test code to be more flexible than an equality test. The one thing you can do is to test to make sure the output HTML is actually valid. That is the first step. Once you know that, you may be able to test for specific output in the HTML, but it is not a good idea to test for exact HTML structure, as that may change on the whims of your product visionary.

I wrote PHPFUI/HtmlUnitTester for exactly this reason. It will insure your generated HTML and CSS is valid. It does not check for content, just that the HTML conforms to the current standard.

It is pretty easy to use, just extend your test from \PHPFUI\HTMLUnitTester\Extensions and use any of the following asserts:

  • assertNotWarningCss
  • assertNotWarningCssFile
  • assertNotWarningCssUrl
  • assertNotWarningFile
  • assertNotWarningHtml
  • assertNotWarningUrl
  • assertValidCss
  • assertValidCssFile
  • assertValidCssUrl
  • assertValidFile
  • assertValidHtml
  • assertValidUrl
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜