How to unit testing HTML output using PHPUnit?

I am new to PHPUnit and I have some problems testing HTML output.

My test is as follows:

/**
* @covers Scrap::removeTags
*
*/
public function testRemoveTags() {

    // Variables
    $simple_parameter        = 'script';
    $array_parameter         = array('script', 'div');
    $html                    = '<div class="pubanunciomrec" style="background:#FFFFFF;"><script type="text/javascript"><!-- google_ad_slot = "9853257829"; google_ad_width = 300; google_ad_height = 250; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div><table></table>';

    // Expected HTML
    $expected_html_whitout_script     = new DOMDocument;
    $expected_html_whitout_script->loadHTML('<div class="pubanunciomrec" style="background:#FFFFFF;"></div><table></table>');
    $expected_html_without_script_div = new DOMDocument;
    $expected_html_without_script_div->loadHTML('<table></table>');

    // Actual HTML
    $actual_whitout_script     = new DOMDocument;
    $actual_whitout_script->loadHTML($this->scrap->removeTags($html, $simple_parameter));
    $actual_without_script_div = new DOMDocument;
    $actual_without_script_div->loadHTML($this->scrap->removeTags($html, $array_parameter));


    // Test
    $this->assertEquals($expected_html_whitout_script, $actual_whitout_script);
    $this->assertEquals($expected_html_without_script_div, $actual_without_script_div);

}

My problem is that the DOMDocument object is generating some HTML code, and I cannot compare. How can I print a DOMDocument object to see the result? Any tips on how to compare HTML?

Sorry, my bad english.

Regards,

+3
source share
5 answers

You can use the saveHtml DOMDocument method and compare the result.

+1
source

2013 HTML PHPUnit. assertTag(), PHPUnit 3.7 3.8.

:

// Matcher that asserts that there is an element with an id="my_id".
$matcher = array('id' => 'my_id');

// Matcher that asserts that there is a "span" tag.
$matcher = array('tag' => 'span');

// Matcher that asserts that there is a "div", with an "ul" ancestor and a "li"
// parent (with class="enum"), and containing a "span" descendant that contains
// an element with id="my_test" and the text "Hello World".
$matcher = array(
    'tag'      => 'div',
    'ancestor' => array('tag' => 'ul'),
    'parent'   => array(
        'tag'        => 'li',
        'attributes' => array('class' => 'enum')
    ),
    'descendant' => array(
        'tag'   => 'span',
        'child' => array(
            'id'      => 'my_test',
            'content' => 'Hello World'
        )
    )
);

// Use assertTag() to apply a $matcher to a piece of $html.
$this->assertTag($matcher, $html);

PHPUnit Website.

+4

HTML . HTML , .

.

+2

, Selenium. -.

, - , , , , , .

IDE, Firefox, .

Selenium, CI, , - HTML.

, .

, PHPUnit ( ), , , , , .

+1

HTML PHPUnit assertXmlStringEqualsXmlString:

$this->assertXmlStringEqualsXmlString($emailMarkup, $html);

$emailMarkup - HTML $html - HTML

! HTML XML-. ,

<br/>

<br>

Also, tag attributes must have values, for example. use

<hr noshade="true">

instead

<hr noshade>

0
source

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


All Articles