Skip to content

Commit 35545cd

Browse files
committed
Added tests for constraint
1 parent 7c0a2da commit 35545cd

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed

src/Symfony/Cmf/Component/Testing/Unit/Constraint/SchemaAcceptsXml.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class SchemaAcceptsXml extends \PHPUnit_Framework_Constraint
66
{
77
protected $schemaFile;
8+
protected $failingElement;
9+
protected $errors;
810

911
public function __construct($schemaFile)
1012
{
@@ -13,26 +15,46 @@ public function __construct($schemaFile)
1315

1416
public function matches($others)
1517
{
16-
foreach ($others as $other) {
18+
foreach ($others as $id => $other) {
1719
$configElement = $other->getElementsByTagName('config');
1820

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+
if (1 !== $configElement->length) {
22+
throw new \InvalidArgumentException(sprintf('Can only test a file if it contains 1 <config> element, %d given', $configElement->length));
2123
}
2224

2325
$configDom = new \DomDocument();
2426
$configDom->appendChild($configDom->importNode($configElement->item(0), true));
2527

28+
libxml_use_internal_errors(true);
2629
if (!$configDom->schemaValidate($this->schemaFile)) {
30+
$this->errors = libxml_get_errors();
31+
$this->failingElement = $id;
2732
return false;
2833
}
2934
}
3035

3136
return true;
3237
}
3338

34-
public function toString()
39+
public function toString() { }
40+
41+
protected function failureDescription($others)
42+
{
43+
return sprintf(
44+
'"%s" is accepted by the XML schema "%s"',
45+
\PHPUnit_Util_Type::export($others[$this->failingElement]),
46+
$this->schemaFile
47+
);
48+
}
49+
50+
protected function additionalFailureDescription($other)
3551
{
36-
return sprintf('is accepted by the XML schema "%s"', $this->schemaFile);
52+
$str = '';
53+
54+
foreach ($this->errors as $error) {
55+
$str .= $error->message.($error->file ? ' in'.$error->file : '').' on line '.$error->line."\n";
56+
}
57+
58+
return $str;
3759
}
3860
}

src/Symfony/Cmf/Component/Testing/Unit/XmlSchemaTestCase.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ public static function assertSchemaAcceptsXml($xmlDoms, $schemaPath, $message =
1919
return $dom;
2020
}
2121

22-
if (!$dom instanceof DOMDocument) {
23-
throw new \InvalidArgumentException('The first argument of assertSchemaAcceptsXml should be instances of \DOMDocument, "%s" given', get_class($xml));
22+
if (!$dom instanceof \DOMDocument) {
23+
throw new \InvalidArgumentException(sprintf('The first argument of assertSchemaAcceptsXml should be instances of \DOMDocument, "%s" given', get_class($dom)));
2424
}
25+
26+
return $dom;
2527
}, $xmlDoms);
2628

2729
return self::assertThat($xmlDoms, new Constraint\SchemaAcceptsXml($schemaPath), $message);

tests/Fixtures/schema/schema1.xsd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<xsd:schema xmlns="http://cmf.symfony.com/schema/dic/foo"
4+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5+
targetNamespace="http://cmf.symfony.com/schema/dic/foo"
6+
elementFormDefault="qualified">
7+
8+
<xsd:element name="config" type="config" />
9+
10+
<xsd:complexType name="config">
11+
<xsd:attribute name="required" use="required" />
12+
</xsd:complexType>
13+
</xsd:schema>

tests/Unit/XmlSchemaTestCaseTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Symfony\Cmf\Component\Testing\Unit\XmlSchemaTestCase;
6+
7+
class XmlSchemaTestCaseTest extends XmlSchemaTestCase
8+
{
9+
/**
10+
* @dataProvider getAssertingData
11+
*/
12+
public function testAsserting($input, $schemaFile, $result, $message = null)
13+
{
14+
$failed = false;
15+
16+
try {
17+
$this->assertSchemaAcceptsXml($input, $schemaFile);
18+
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
19+
$failed = true;
20+
}
21+
22+
if ($failed) {
23+
$this->assertFalse($result, 'schema should accept xml');
24+
} else {
25+
$this->assertTrue($result, 'schema should not accept xml');
26+
if ($message) {
27+
$this->assertEquals($message, $e->getMessage());
28+
}
29+
}
30+
}
31+
32+
public function getAssertingData()
33+
{
34+
$schema1 = __DIR__.'/../Fixtures/schema/schema1.xsd';
35+
36+
$data = array();
37+
38+
$dom1 = new \DomDocument();
39+
$dom1->loadXML('<container><config xmlns="http://cmf.symfony.com/schema/dic/foo" required="f"/></container>');
40+
$data[] = array($dom1, $schema1, true);
41+
42+
$dom2 = new \DomDocument();
43+
$dom2->loadXML('<container><config xmlns="http://cmf.symfony.com/schema/dic/foo" /></container>');
44+
$data[] = array($dom2, $schema1, false);
45+
46+
$data[] = array(array($dom1, $dom1), $schema1, true);
47+
$data[] = array(array($dom1, $dom2), $schema1, false);
48+
$data[] = array(array($dom2, $dom1), $schema1, false);
49+
50+
return $data;
51+
}
52+
53+
/**
54+
* @expectedException \InvalidArgumentException
55+
*/
56+
public function testFailsIfNoConfigElementIsAvailable()
57+
{
58+
$dom = new \DomDocument();
59+
$dom->loadXML('<container></container>');
60+
61+
$this->assertSchemaAcceptsXml($dom, __DIR__.'/../Fixtures/schema/schema1.xsd');
62+
}
63+
}

0 commit comments

Comments
 (0)