Robust unit testing of HTML in PHP

I am adding unit tests to an old PHP database at work. I will test and rewrite a lot of HTML generation code, and currently I'm just testing if the generated lines are identical to the expected line, for example: (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 method has a disadvantage, which is also checked on the ordering of attributes, spaces and many other irrelevant details. I am wondering if there are any better ways to do this. For example, if there are good and easy ways to compare the generated DOM trees. I found very similar questions for ruby but found nothing for PHP.

+4
source share
2 answers

Take a look at Zend_Test_PHPUnit . Here you can query the DOM using:

assertQuery() or assertXpath() ;

+1
source

I am dealing with the same problems. Lol! What I think I'm going to do is use a DOMDocument at some point. But for now, all I do is write coverage tests, which does. Here is one of my tests. 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); } 
0
source

Source: https://habr.com/ru/post/1308299/


All Articles