Skip to content

Commit 7c0a2da

Browse files
committed
Added XML Schema testing support
1 parent d91bc86 commit 7c0a2da

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\Unit\Constraint;
4+
5+
class SchemaAcceptsXml extends \PHPUnit_Framework_Constraint
6+
{
7+
protected $schemaFile;
8+
9+
public function __construct($schemaFile)
10+
{
11+
$this->schemaFile = $schemaFile;
12+
}
13+
14+
public function matches($others)
15+
{
16+
foreach ($others as $other) {
17+
$configElement = $other->getElementsByTagName('config');
18+
19+
if (1 !== count($configElement)) {
20+
throw new \InvalidArgumentException('Can only test a file if it contains 1 <config> elements, %d given', count($configElement));
21+
}
22+
23+
$configDom = new \DomDocument();
24+
$configDom->appendChild($configDom->importNode($configElement->item(0), true));
25+
26+
if (!$configDom->schemaValidate($this->schemaFile)) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
public function toString()
35+
{
36+
return sprintf('is accepted by the XML schema "%s"', $this->schemaFile);
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\Unit;
4+
5+
class XmlSchemaTestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
public static function assertSchemaAcceptsXml($xmlDoms, $schemaPath, $message = '')
8+
{
9+
if (!is_array($xmlDoms)) {
10+
$xmlDoms = array($xmlDoms);
11+
}
12+
13+
$xmlDoms = array_map(function ($dom) {
14+
if (is_string($dom)) {
15+
$xml = $dom;
16+
$dom = new \DOMDocument();
17+
$dom->load($xml);
18+
19+
return $dom;
20+
}
21+
22+
if (!$dom instanceof DOMDocument) {
23+
throw new \InvalidArgumentException('The first argument of assertSchemaAcceptsXml should be instances of \DOMDocument, "%s" given', get_class($xml));
24+
}
25+
}, $xmlDoms);
26+
27+
return self::assertThat($xmlDoms, new Constraint\SchemaAcceptsXml($schemaPath), $message);
28+
}
29+
}

0 commit comments

Comments
 (0)