问题描述:

I am developing a PHP-powered application component which exports some data to an XML file which must follow a scheme defined by XSD file. I know how to validate the file manually, but it would be very handy if this could be done within unit tests.

Is there any library or framework available which can me help doing that?

网友答案:

One possibility would be to use DOMDocument::schemaValidate or DOMDocument::schemaValidateSource. Since either of those two methods returns a boolean value, you could then assert that the returned value is true.

网友答案:

This PHPUnit XSD validation lib defines a custom PHPUnit_Framework_Constraint uses DOMDocument::schemaValidate.

The advantage is that it gives a useful error when the validation fails, rather than just saying that 'false is not true'.

Usage

$constraint = new Jasny\PHPUnit\Constraint\XSDValidation("myschema.xsd");

$xml = $this->object->doSomething();
$this->assertThat($xml, $constraint);

Note: This is a shameless plug of my own open source library.

相关阅读:
Top