views:

58

answers:

2

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.

A: 

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);
}
Gutzofter
+1  A: 

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

assertQuery() or assertXpath();

takeshin