Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6351163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:56:56+00:00 2026-05-24T21:56:56+00:00

I have a command line script that generates some HTML that I am trying

  • 0

I have a command line script that generates some HTML that I am trying to unit test using PHPUnit. Note that this HTML is not seen by a browser, so Selenium is not the right solution for this.

I’m only concerned with comparing the actual HTML structure. I’m using assertEquals() but the actual strings may not be exactly identical because of various whitespace characters.

public function testHtmlIsIdentical()
{
    $expectedReport = file_get_contents('expected.html');
    $this->report->setupSomeData('test data');
    $actualReport = $this->report->generateHtml();
    $this->assertEquals($expectedReport, $actualReport);
}

What can I do in order to compare the structure (the nodes) of the HTML instead of the strings of HTML? Is there a feature of PHPUnit that allows this? Is there a standalone library for comparing HTML?

Solution:

PHPUnit has assertions for comparing XML:

  • assertXmlFileEqualsXmlFile()
  • assertXmlStringEqualsXmlFile()
  • assertXmlStringEqualsXmlString()

The assertXmlStringEqualsXmlFile works perfectly in this scenario:

public function testHtmlIsIdentical()
{
    $this->report->setupSomeData('test data');
    $this->assertXmlStringEqualsXmlFile('expected.html', $this->report->generateHtml());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:56:57+00:00Added an answer on May 24, 2026 at 9:56 pm

    Well there is DomDocument and if you want to check that the order of HTML elements matches you could use that.

    If everything that differes is redundant whitespace maybe try:

    $expectedDom = new DomDocument();
    $expectedDom->loadHTMLFile('expected.html');
    $expectedDom->preserveWhiteSpace = false;
    
    $actualDom = new DomDocument();
    $actualDom->loadHTML($this->report->generateHtml());
    $actualDom->preserveWhiteSpace = false;
    
    $this->assertEquals($expectedDom->saveHTML(), $actualDom->saveHTML());
    

    See preservewhitespace.

    What could also be worth looking into is assertEqualXMLStructure as this can be used to compare HTML as well:

    assertEqualXMLStructure(
        DOMElement $expectedElement,
        DOMElement $actualElement
        [, bool $checkAttributes = false,
        string $message = '']
    )
    

    But you might run into issues with the whitespaces again so maybe you need to strip those before comparing. The benefit of using DOM is that you get much nicer error reporting in case the docs don’t match.

    Another way of testing XML/HTML generation is described in Practical PHPUnit: Testing XML generation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a command-line PHP script that runs a wget request using each member
This is a simple script I have written to test command line argument handling:
I have a Python command line script that connects to a database, generates files
I have a command line script that uses the Django ORM and MySQL backend.
Specifically I have a PHP command-line script that at a certain point requires input
I have a python script that, once executed from command line, performs the needed
Does anyone happen to have some sort of a nice command line script they
I have a command line executable that alters some bits in a file that
I have a perl script which when run from the command line generates a
I have a script that generate me some command that I must execute... Example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.