Skip to content

Commit c40dc92

Browse files
committed
Merge pull request #34 from symfony-cmf/xml_schema_test
Refactored constraint to count asserts
2 parents 4d587c3 + 758a948 commit c40dc92

File tree

4 files changed

+92
-62
lines changed

4 files changed

+92
-62
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
class SchemaAcceptsXml extends \PHPUnit_Framework_Constraint
66
{
7-
protected $schemaFile;
7+
protected $xml;
88
protected $failingElement;
99
protected $errors;
1010

11-
public function __construct($schemaFile)
11+
public function __construct($xml)
1212
{
13-
$this->schemaFile = $schemaFile;
13+
$this->xml = $xml;
1414
}
1515

16-
public function matches($others)
16+
public function matches($schemaFile)
1717
{
18-
foreach ($others as $id => $other) {
19-
$configElement = $other->getElementsByTagName('config');
18+
foreach ($this->xml as $id => $dom) {
19+
$configElement = $dom->getElementsByTagName('config');
2020

2121
if (1 !== $configElement->length) {
2222
throw new \InvalidArgumentException(sprintf('Can only test a file if it contains 1 <config> element, %d given', $configElement->length));
@@ -26,28 +26,34 @@ public function matches($others)
2626
$configDom->appendChild($configDom->importNode($configElement->item(0), true));
2727

2828
libxml_use_internal_errors(true);
29-
if (!$configDom->schemaValidate($this->schemaFile)) {
29+
if (!$configDom->schemaValidate($schemaFile)) {
3030
$this->errors = libxml_get_errors();
3131
$this->failingElement = $id;
32+
3233
return false;
3334
}
3435
}
3536

3637
return true;
3738
}
3839

40+
public function count()
41+
{
42+
return count($this->xml);
43+
}
44+
3945
public function toString() { }
4046

41-
protected function failureDescription($others)
47+
protected function failureDescription($schemaFile)
4248
{
4349
return sprintf(
4450
'"%s" is accepted by the XML schema "%s"',
45-
\PHPUnit_Util_Type::export($others[$this->failingElement]),
46-
$this->schemaFile
51+
\PHPUnit_Util_Type::export($this->xml[$this->failingElement]),
52+
$schemaFile
4753
);
4854
}
4955

50-
protected function additionalFailureDescription($other)
56+
protected function additionalFailureDescription($schema)
5157
{
5258
$str = '';
5359

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public static function assertSchemaAcceptsXml($xmlDoms, $schemaPath, $message =
2626
return $dom;
2727
}, $xmlDoms);
2828

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

tests/Unit/XmlSchemaTestCaseTest.php

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,10 @@
66

77
class XmlSchemaTestCaseTest extends XmlSchemaTestCase
88
{
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()
9+
public function testAcceptsSingleDomsWithoutArray()
5710
{
5811
$dom = new \DomDocument();
59-
$dom->loadXML('<container></container>');
60-
12+
$dom->loadXML('<container><config xmlns="http://cmf.symfony.com/schema/dic/foo" required="f"/></container>');
6113
$this->assertSchemaAcceptsXml($dom, __DIR__.'/../Fixtures/schema/schema1.xsd');
6214
}
6315
}

0 commit comments

Comments
 (0)