📜  PHPUnit assertXmlStringEqualsXmlFile()函数

📅  最后修改于: 2022-05-13 01:56:34.848000             🧑  作者: Mango

PHPUnit assertXmlStringEqualsXmlFile()函数

assertXmlStringEqualsXmlFile()函数是 PHPUnit 中的内置函数,用于断言实际的 XML 文件内容是否等于预期的 XML字符串。如果预期的 XML字符串与实际的 XML 文件内容相同,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。

句法:

assertXmlStringEqualsXmlFile(string $expectedFile, 
string $actualFile[, string $message = ''])

参数:此函数接受三个参数,如上述语法所示。参数说明如下:

  • $expectedXmlString:这个参数是代表期望的 XML字符串的任何类型。
  • $actualXmlFile:此参数是表示实际 XML 文件路径的任何类型。
  • $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。

下面的示例说明了 PHPUnit 中的 assertXmlstringEqualsXmlFile()函数:

示例 1:

PHP
'; 
        $actualxmlFilePath    =  '/home/lovely/Documents/php/actual.xml';
        // Assert function to test whether given 
        // expected xml string is equal to actual xml file path
        $this->assertXmlStringEqualsXmlFile(
            $actualxmlFilePath,
            $expectedxmlString,
            "actual xml file path is equal to expected xml string"
        ); 
    } 
} 
    
?>


PHP
'; 
        $actualxmlFilePath    =  '/home/lovely/Documents/php/actual.xml';
        // Assert function to test whether given 
        // expected xml string is equal to actual xml file path
        $this->assertXmlStringEqualsXmlFile(
            $actualxmlFilePath,
            $expectedxmlString,
            "actual xml file path is equal to expected xml string"
        ); 
    } 
} 
    
?>


输出:

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
@@ @@


-  
+  


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

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

示例 2:

PHP

'; 
        $actualxmlFilePath    =  '/home/lovely/Documents/php/actual.xml';
        // Assert function to test whether given 
        // expected xml string is equal to actual xml file path
        $this->assertXmlStringEqualsXmlFile(
            $actualxmlFilePath,
            $expectedxmlString,
            "actual xml file path is equal to expected xml string"
        ); 
    } 
} 
    
?> 

输出:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                                  1 / 1 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

参考c: https://phpunit.readthedocs.io/en/9.2/assertions.html#assertxmlstringequalsxmlfile