miho
miho

Reputation: 12075

XSD schema based XML validation in PHP unit test

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?

Upvotes: 5

Views: 1411

Answers (2)

Arnold Daniels
Arnold Daniels

Reputation: 16553

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.

Upvotes: 1

Havelock
Havelock

Reputation: 6968

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.

Upvotes: 3

Related Questions