Friday, December 12, 2025
HomeLanguagesPHPUnit assertXmlStringEqualsXmlString() Function

PHPUnit assertXmlStringEqualsXmlString() Function

The assertXmlStringEqualsXmlString() function is a builtin function in PHPUnit and is used to assert whether the actual XML string equals to expected XML string or not. This assertion will return true in the case if the expected XML string is the same as the actual XML string else returns false. In case of true the asserted test case got passed else test case got failed.

Syntax:

assertXmlStringEqualsXmlString(string $expectedXml, 
string $actualXml[, string $message = ''])

Parameters: This function accepts three parameters as mentioned above and described below:

  •  $expectedXmlString: This parameter is of any type which represents the expected XML string.
  •  $actualXmlString: This parameter is of any type which represents the actual XML String.
  •  $message: This parameter takes a string value. When the test case got failed this string message got displayed as an error message.

Below examples illustrate the assertXmlStringEqualsXmlString() function in PHPUnit:

Example 1:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
{ 
    public function testNegativeTestcaseForassertXmlStringEqualsXmlString()
    { 
        $expectedxmlString  =  '<foo><bazx></bazx></foo>'; 
        $actualxmlString    =  '<foo><baz></baz></foo>';
        // Assert function to test whether given 
        // expected xml string is equal to actual xml string
        $this->assertXmlStringEqualsXmlString(
            $actualxmlString,
            $expectedxmlString,
            "actual xml string is equal to expected xml string"
        ); 
    } 
} 
    
?> 


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

F                                                                1 / 1 (100%)

Time: 87 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testPositiveTestcaseForassertXmlFileNotEqualsXmlFile
actual xml string is equal to expected xml string
Failed asserting that two DOM documents are equal.
--- Expected
+++ Actual
@@ @@
<?xml version="1.0"?>
<foo>
-  <baz/>
+  <bazx/>
</foo>

/home/lovely/Documents/php/test.php:16

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Example 2:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
{ 
    public function testPositiveTestcaseForassertXmlStringEqualsXmlString()
    { 
        $expectedxmlString  =  '<foo><baz></baz></foo>'; 
        $actualxmlString    =  '<foo><baz></baz></foo>';
        // Assert function to test whether given 
        // expected xml string is equal to actual xml string
        $this->assertXmlStringEqualsXmlString(
            $actualxmlString,
            $expectedxmlString,
            "actual xml string is equal to expected xml string"
        ); 
    } 
} 
    
?> 


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                                 1 / 1 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

Reference:https://phpunit.readthedocs.io/en/9.2/assertions.html#assertxmlstringequalsxmlstring

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS