From 7c0a2da43287b87435422f6dd4af9bfa6a490d15 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sat, 21 Dec 2013 11:56:28 +0100 Subject: [PATCH 1/3] Added XML Schema testing support --- .../Unit/Constraint/SchemaAcceptsXml.php | 38 +++++++++++++++++++ .../Testing/Unit/XmlSchemaTestCase.php | 29 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php create mode 100644 src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php diff --git a/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php b/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php new file mode 100644 index 0000000..6b6c3c8 --- /dev/null +++ b/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php @@ -0,0 +1,38 @@ +schemaFile = $schemaFile; + } + + public function matches($others) + { + foreach ($others as $other) { + $configElement = $other->getElementsByTagName('config'); + + if (1 !== count($configElement)) { + throw new \InvalidArgumentException('Can only test a file if it contains 1 elements, %d given', count($configElement)); + } + + $configDom = new \DomDocument(); + $configDom->appendChild($configDom->importNode($configElement->item(0), true)); + + if (!$configDom->schemaValidate($this->schemaFile)) { + return false; + } + } + + return true; + } + + public function toString() + { + return sprintf('is accepted by the XML schema "%s"', $this->schemaFile); + } +} diff --git a/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php b/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php new file mode 100644 index 0000000..f900dd5 --- /dev/null +++ b/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php @@ -0,0 +1,29 @@ +load($xml); + + return $dom; + } + + if (!$dom instanceof DOMDocument) { + throw new \InvalidArgumentException('The first argument of assertSchemaAcceptsXml should be instances of \DOMDocument, "%s" given', get_class($xml)); + } + }, $xmlDoms); + + return self::assertThat($xmlDoms, new Constraint\SchemaAcceptsXml($schemaPath), $message); + } +} From 35545cddf51e6ee439bd70fd7cd0dd41add70801 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sat, 21 Dec 2013 12:39:26 +0100 Subject: [PATCH 2/3] Added tests for constraint --- .../Unit/Constraint/SchemaAcceptsXml.php | 32 ++++++++-- .../Testing/Unit/XmlSchemaTestCase.php | 6 +- tests/Fixtures/schema/schema1.xsd | 13 ++++ tests/Unit/XmlSchemaTestCaseTest.php | 63 +++++++++++++++++++ 4 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 tests/Fixtures/schema/schema1.xsd create mode 100644 tests/Unit/XmlSchemaTestCaseTest.php diff --git a/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php b/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php index 6b6c3c8..b19c89c 100644 --- a/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php +++ b/src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php @@ -5,6 +5,8 @@ class SchemaAcceptsXml extends \PHPUnit_Framework_Constraint { protected $schemaFile; + protected $failingElement; + protected $errors; public function __construct($schemaFile) { @@ -13,17 +15,20 @@ public function __construct($schemaFile) public function matches($others) { - foreach ($others as $other) { + foreach ($others as $id => $other) { $configElement = $other->getElementsByTagName('config'); - if (1 !== count($configElement)) { - throw new \InvalidArgumentException('Can only test a file if it contains 1 elements, %d given', count($configElement)); + if (1 !== $configElement->length) { + throw new \InvalidArgumentException(sprintf('Can only test a file if it contains 1 element, %d given', $configElement->length)); } $configDom = new \DomDocument(); $configDom->appendChild($configDom->importNode($configElement->item(0), true)); + libxml_use_internal_errors(true); if (!$configDom->schemaValidate($this->schemaFile)) { + $this->errors = libxml_get_errors(); + $this->failingElement = $id; return false; } } @@ -31,8 +36,25 @@ public function matches($others) return true; } - public function toString() + public function toString() { } + + protected function failureDescription($others) + { + return sprintf( + '"%s" is accepted by the XML schema "%s"', + \PHPUnit_Util_Type::export($others[$this->failingElement]), + $this->schemaFile + ); + } + + protected function additionalFailureDescription($other) { - return sprintf('is accepted by the XML schema "%s"', $this->schemaFile); + $str = ''; + + foreach ($this->errors as $error) { + $str .= $error->message.($error->file ? ' in'.$error->file : '').' on line '.$error->line."\n"; + } + + return $str; } } diff --git a/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php b/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php index f900dd5..25cf729 100644 --- a/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php +++ b/src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php @@ -19,9 +19,11 @@ public static function assertSchemaAcceptsXml($xmlDoms, $schemaPath, $message = return $dom; } - if (!$dom instanceof DOMDocument) { - throw new \InvalidArgumentException('The first argument of assertSchemaAcceptsXml should be instances of \DOMDocument, "%s" given', get_class($xml)); + if (!$dom instanceof \DOMDocument) { + throw new \InvalidArgumentException(sprintf('The first argument of assertSchemaAcceptsXml should be instances of \DOMDocument, "%s" given', get_class($dom))); } + + return $dom; }, $xmlDoms); return self::assertThat($xmlDoms, new Constraint\SchemaAcceptsXml($schemaPath), $message); diff --git a/tests/Fixtures/schema/schema1.xsd b/tests/Fixtures/schema/schema1.xsd new file mode 100644 index 0000000..11ea315 --- /dev/null +++ b/tests/Fixtures/schema/schema1.xsd @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/tests/Unit/XmlSchemaTestCaseTest.php b/tests/Unit/XmlSchemaTestCaseTest.php new file mode 100644 index 0000000..f969c3e --- /dev/null +++ b/tests/Unit/XmlSchemaTestCaseTest.php @@ -0,0 +1,63 @@ +assertSchemaAcceptsXml($input, $schemaFile); + } catch (\PHPUnit_Framework_ExpectationFailedException $e) { + $failed = true; + } + + if ($failed) { + $this->assertFalse($result, 'schema should accept xml'); + } else { + $this->assertTrue($result, 'schema should not accept xml'); + if ($message) { + $this->assertEquals($message, $e->getMessage()); + } + } + } + + public function getAssertingData() + { + $schema1 = __DIR__.'/../Fixtures/schema/schema1.xsd'; + + $data = array(); + + $dom1 = new \DomDocument(); + $dom1->loadXML(''); + $data[] = array($dom1, $schema1, true); + + $dom2 = new \DomDocument(); + $dom2->loadXML(''); + $data[] = array($dom2, $schema1, false); + + $data[] = array(array($dom1, $dom1), $schema1, true); + $data[] = array(array($dom1, $dom2), $schema1, false); + $data[] = array(array($dom2, $dom1), $schema1, false); + + return $data; + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testFailsIfNoConfigElementIsAvailable() + { + $dom = new \DomDocument(); + $dom->loadXML(''); + + $this->assertSchemaAcceptsXml($dom, __DIR__.'/../Fixtures/schema/schema1.xsd'); + } +} From 0f8c2c93d83e49314ac383e8b1826a77a65e5487 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 27 Dec 2013 10:21:42 +0100 Subject: [PATCH 3/3] Added to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30e7b64..c979447 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ Changelog ========= +* **2013-12-27**: Added XmlSchemaTestCase to test XML schema's * **2013-11-17**: Added DatabaseTestListener to support database testing