XMLSchemaTest::dataValidateSchemaFiles()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.7998
cc 4
nc 3
nop 0
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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