Passed
Push — static-analysis ( 536d5c...894217 )
by SignpostMarv
03:17
created

SchemaTest::testMultipleSchemasInSameFile()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 8.8571
c 2
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Tests;
3
4
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
5
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
6
7
class SchemaTest extends BaseTest
8
{
9
    public function testBaseEmpty()
10
    {
11
        $schema = $this->reader->readString('<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>');
12
13
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Schema', $schema);
14
        $this->assertEquals('http://www.example.com', $schema->getTargetNamespace());
15
16
        $schemas = $schema->getSchemas();
17
18
        $this->assertFalse(empty($schemas));
19
    }
20
21
    public function getTypesToSearch()
22
    {
23
        return [
24
            ['findType'],
25
            ['findElement'],
26
            ['findAttribute'],
27
            ['findAttributeGroup'],
28
            ['findGroup'],
29
        ];
30
    }
31
32
    /**
33
     * @dataProvider getTypesToSearch
34
     */
35
    public function testNotFoundType($find)
36
    {
37
        $schema = $this->reader->readString('<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>');
38
39
        $this->setExpectedException('GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException');
40
        $schema->$find('foo');
41
    }
42
43
    public function testBase()
44
    {
45
        $schema = $this->reader->readString('
46
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
47
                <xs:complexType name="myType"></xs:complexType>
48
                <xs:element name="myElement" type="myType"></xs:element>
49
50
                <xs:group name="myGroup">
51
                    <xs:sequence></xs:sequence>
52
                </xs:group>
53
                <xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
54
                <xs:attributeGroup name="myAttributeGroup"></xs:attributeGroup>
55
            </xs:schema>');
56
57
        $this->assertCount(1, $schema->getTypes());
58
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType', $schema->findType('myType', 'http://www.example.com'));
59
        //$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType', $schema->findType('myType'));
60
61
62
        $this->assertCount(1, $schema->getElements());
63
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef', $schema->findElement('myElement', 'http://www.example.com'));
64
65
        $this->assertCount(1, $schema->getGroups());
66
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\Group', $schema->findGroup('myGroup', 'http://www.example.com'));
67
68
        $this->assertCount(1, $schema->getAttributeGroups());
69
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Group', $schema->findAttributeGroup('myAttributeGroup', 'http://www.example.com'));
70
71
        $this->assertCount(1, $schema->getAttributes());
72
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef', $schema->findAttribute('myAttribute', 'http://www.example.com'));
73
74
    }
75
76
    public function testMultipleSchemasInSameFile()
77
    {
78
        $file = 'schema.xsd';
79
        $schema1 = $this->reader->readString('
80
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
81
                <xs:complexType name="myType"></xs:complexType>
82
                <xs:element name="myElement" type="myType"></xs:element>
83
84
                <xs:group name="myGroup">
85
                    <xs:sequence></xs:sequence>
86
                </xs:group>
87
                <xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
88
                <xs:attributeGroup name="myAttributeGroup"></xs:attributeGroup>
89
            </xs:schema>',
90
            $file
91
        );
92
93
        $this->assertCount(1, $schema1->getTypes());
94
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType', $schema1->findType('myType', 'http://www.example.com'));
95
96
        $this->assertCount(1, $schema1->getElements());
97
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef', $schema1->findElement('myElement', 'http://www.example.com'));
98
99
        //Now use a second schema which imports from the first one, and is in the SAME file
100
        $schema2 = $this->reader->readString('
101
            <xs:schema targetNamespace="http://www.example2.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://www.example.com">
102
                <xs:import namespace="http://www.example.com"/>
103
                <xs:element name="myElement2" type="ns1:myType"></xs:element>
104
            </xs:schema>',
105
            $file
106
        );
107
108
        $this->assertCount(0, $schema2->getTypes());
109
110
        $this->assertCount(1, $schema2->getElements());
111
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef', $schema2->findElement('myElement2', 'http://www.example2.com'));
112
113
    }
114
115
    public function testGroupRefInType()
116
    {
117
        $schema1 = $this->reader->readString('
118
        <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
119
            <xs:element name="myElement">
120
                <xs:complexType>
121
                    <xs:group ref="myGroup"/>
122
                </xs:complexType>
123
            </xs:element>
124
            <xs:group name="myGroup">
125
                <xs:choice>
126
                    <xs:element name="groupElement" type="xs:string"/>
127
                </xs:choice>
128
            </xs:group>
129
        </xs:schema>');
130
131
        $this->assertCount(1, $schema1->getGroups());
132
        $group = $schema1->findGroup('myGroup', 'http://www.example.com');
133
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\Group', $group);
134
135
        $this->assertCount(1, $schema1->getElements());
136
137
        /**
138
         * @var $element ElementDef
139
         * @var $type ComplexType
140
         */
141
        $element = $schema1->findElement('myElement', 'http://www.example.com');
142
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef', $element);
143
144
        $type = $element->getType();
145
        $this->assertCount(1, $type->getElements());
146
147
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\GroupRef', $type->getElements()[0]);
148
        $this->assertEquals($group->getElements(), $type->getElements()[0]->getElements());
149
    }
150
}
151