Skip to content

Commit e75b22c

Browse files
committed
Merge pull request #35 from symfony-cmf/xml_schema_test
More Xml schema test improvements
2 parents c40dc92 + 1474ebb commit e75b22c

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,22 @@ public function toString() { }
4747
protected function failureDescription($schemaFile)
4848
{
4949
return sprintf(
50-
'"%s" is accepted by the XML schema "%s"',
51-
\PHPUnit_Util_Type::export($this->xml[$this->failingElement]),
50+
"Xml is accepted by the XML schema \"%s\"",
5251
$schemaFile
5352
);
5453
}
5554

5655
protected function additionalFailureDescription($schema)
5756
{
58-
$str = '';
57+
$str = "\n".$this->xml[$this->failingElement]->saveXml()."\n\n";
5958

6059
foreach ($this->errors as $error) {
61-
$str .= $error->message.($error->file ? ' in'.$error->file : '').' on line '.$error->line."\n";
60+
$error = trim($error->message).($error->file ? ' in'.$error->file : '').' on line '.$error->line."\n";
61+
62+
// avoid repeating same error
63+
if (false === strpos($str, $error)) {
64+
$str .= $error;
65+
}
6266
}
6367

6468
return $str;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
class XmlSchemaTestCase extends \PHPUnit_Framework_TestCase
66
{
77
public static function assertSchemaAcceptsXml($xmlDoms, $schemaPath, $message = '')
8+
{
9+
return self::assertThat($schemaPath, self::getSchemaAcceptsXmlConstraint($xmlDoms), $message);
10+
}
11+
12+
public static function assertSchemaRefusesXml($xmlDoms, $schemaPath, $message = '')
13+
{
14+
return self::assertThat(
15+
$schemaPath,
16+
new \PHPUnit_Framework_Constraint_Not(self::getSchemaAcceptsXmlConstraint($xmlDoms)),
17+
$message
18+
);
19+
}
20+
21+
private static function getSchemaAcceptsXmlConstraint($xmlDoms)
822
{
923
if (!is_array($xmlDoms)) {
1024
$xmlDoms = array($xmlDoms);
@@ -26,6 +40,6 @@ public static function assertSchemaAcceptsXml($xmlDoms, $schemaPath, $message =
2640
return $dom;
2741
}, $xmlDoms);
2842

29-
return self::assertThat($schemaPath, new Constraint\SchemaAcceptsXml($xmlDoms), $message);
43+
return new Constraint\SchemaAcceptsXml($xmlDoms);
3044
}
3145
}

tests/Unit/XmlSchemaTestCaseTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ public function testAcceptsSingleDomsWithoutArray()
1212
$dom->loadXML('<container><config xmlns="http://cmf.symfony.com/schema/dic/foo" required="f"/></container>');
1313
$this->assertSchemaAcceptsXml($dom, __DIR__.'/../Fixtures/schema/schema1.xsd');
1414
}
15+
16+
public function testNegativeAssertion()
17+
{
18+
$dom = new \DomDocument();
19+
$dom->loadXML('<container><config xmlns="http://cmf.symfony.com/schema/dic/foo" /></container>');
20+
21+
$this->assertSchemaRefusesXml($dom, __DIR__.'/../Fixtures/schema/schema1.xsd');
22+
}
1523
}

0 commit comments

Comments
 (0)