The assertXmlStringEqualsXmlFile() function is a builtin function in PHPUnit and is used to assert whether the actual XML file Content is 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 file content else returns false. In case of true the asserted test case got passed else test case got failed.
Syntax:
assertXmlStringEqualsXmlFile(string $expectedFile,
string $actualFile[, string $message = ''])
Parameters: This function accepts three parameters as shown in the above syntax. The parameters are described below:
- $expectedXmlString: This parameter is of any type which represents the expected XML string.
- $actualXmlFile: This parameter is of any type which represents the actual XML File Path.
- $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 assertXmlstringEqualsXmlFile() function in PHPUnit:
Example 1:
PHP
<?php
use PHPUnit\Framework\TestCase;
class GeeksPhpunitTestCase extends TestCase
{
public function testNegativeTestcaseForassertXmlStringEqualsXmlFile()
{
$expectedxmlString = '<foo><baza></baza></foo>' ;
$actualxmlFilePath = '/home/lovely/Documents/php/actual.xml' ;
$this ->assertXmlStringEqualsXmlFile(
$actualxmlFilePath ,
$expectedxmlString ,
"actual xml file path is equal to expected xml string"
);
}
}
?>
|
Output:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 89 ms, Memory: 10.00 MB
There was 1 failure:
1) GeeksPhpunitTestCase::testPositiveTestcaseForassertXmlFileNotEqualsXmlFile
actual xml file path is equal to expected xml string
Failed asserting that two DOM documents are equal.
--- Expected
+++ Actual
@@ @@
<?xml version="1.0"?>
<foo>
- <baz/>
+ <baza/>
</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 testPositiveTestcaseForassertXmlStringEqualsXmlFile()
{
$expectedxmlString = '<foo><baz></baz></foo>' ;
$actualxmlFilePath = '/home/lovely/Documents/php/actual.xml' ;
$this ->assertXmlStringEqualsXmlFile(
$actualxmlFilePath ,
$expectedxmlString ,
"actual xml file path 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)
Referencec:https://phpunit.readthedocs.io/en/9.2/assertions.html#assertxmlstringequalsxmlfile
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!