1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use DOMDocument; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class XMLSchemaTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public static function dataValidateSchemaFiles() |
12
|
|
|
{ |
13
|
|
|
$schemaFiles = []; |
14
|
|
|
$di = new DirectoryIterator(__DIR__ . '/Fixtures/config/xml'); |
15
|
|
|
foreach ($di as $element) { |
16
|
|
|
if (! $element->isFile() || substr($element->getFilename(), -4) !== '.xml') { |
17
|
|
|
continue; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$schemaFiles[] = [$element->getPathname()]; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $schemaFiles; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider dataValidateSchemaFiles |
28
|
|
|
*/ |
29
|
|
|
public function testValidateSchema($file) : void |
30
|
|
|
{ |
31
|
|
|
$found = false; |
32
|
|
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
33
|
|
|
$dom->load($file); |
34
|
|
|
|
35
|
|
|
$xmlns = 'http://symfony.com/schema/dic/doctrine'; |
36
|
|
|
|
37
|
|
|
$dbalElements = $dom->getElementsByTagNameNS($xmlns, 'dbal'); |
38
|
|
View Code Duplication |
if ($dbalElements->length) { |
|
|
|
|
39
|
|
|
$dbalDom = new DOMDocument('1.0', 'UTF-8'); |
40
|
|
|
$dbalNode = $dbalDom->importNode($dbalElements->item(0)); |
41
|
|
|
$configNode = $dbalDom->createElementNS($xmlns, 'config'); |
42
|
|
|
$configNode->appendChild($dbalNode); |
43
|
|
|
$dbalDom->appendChild($configNode); |
44
|
|
|
|
45
|
|
|
$ret = $dbalDom->schemaValidate(__DIR__ . '/../../Resources/config/schema/doctrine-1.0.xsd'); |
46
|
|
|
$this->assertTrue($ret, 'DoctrineBundle Dependency Injection XMLSchema did not validate this XML instance.'); |
47
|
|
|
$found = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$ormElements = $dom->getElementsByTagNameNS($xmlns, 'orm'); |
51
|
|
View Code Duplication |
if ($ormElements->length) { |
|
|
|
|
52
|
|
|
$ormDom = new DOMDocument('1.0', 'UTF-8'); |
53
|
|
|
$ormNode = $ormDom->importNode($ormElements->item(0)); |
54
|
|
|
$configNode = $ormDom->createElementNS($xmlns, 'config'); |
55
|
|
|
$configNode->appendChild($ormNode); |
56
|
|
|
$ormDom->appendChild($configNode); |
57
|
|
|
|
58
|
|
|
$ret = $ormDom->schemaValidate(__DIR__ . '/../../Resources/config/schema/doctrine-1.0.xsd'); |
59
|
|
|
$this->assertTrue($ret, 'DoctrineBundle Dependency Injection XMLSchema did not validate this XML instance.'); |
60
|
|
|
$found = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->assertTrue($found, 'Neither <doctrine:orm> nor <doctrine:dbal> elements found in given XML. Are namespaces configured correctly?'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.