Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test HTML output with PHPUnit?

I'm new to PHPUnit, and I'm having some trouble with unit testing HTML output.

My test 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 generates some HTML code and I can't compare it. How can I print the DOMDocument object to see the output? Any clues on how to compare the HTML?

Sorry for my bad english.

Best Regards,

like image 595
André Avatar asked Jan 11 '11 10:01

André


People also ask

How do I run a test in PHPUnit?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is unit testing in HTML?

Unit testing is performed during the coding stage of a software development project to test individual units of the application. It's designed to test that each unit of the software code performs as required.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.


2 Answers

You may want to consider looking at Selenium. It is a browser-based testing tool for doing functional tests for a web site.

You write scripts which involve loading a web browser and simulating clicks and other actions, and then doing asserts to check that, for example, specific page elements are present, in the correct place or contain the expected values.

The tests can be written using an IDE that runs as a plug-in for Firefox, but they can be run against all the major browsers.

We have a suite of Selenium tests that run as part of our CI process, allowing us to see very quickly if something has gone wrong with our HTML output.

All in all, its a very powerful testing tool.

Also, it integrates with PHPUnit (and other language-specific tools), so it does answer your question, although probably not in the way you were thinking of.

like image 36
Spudley Avatar answered Oct 13 '22 05:10

Spudley


Since 2013, there is another way to test HTML Output using PHPUnit. It is using assertTag() method that can be found in PHPUnit 3.7 and 3.8.

For example :

// 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);

Read more in official PHPUnit Website.

like image 176
Rudy Avatar answered Oct 13 '22 04:10

Rudy