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

TypesTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 302
Duplicated Lines 24.17 %

Importance

Changes 0
Metric Value
wmc 13
dl 73
loc 302
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Tests;
3
4
class TypesTest extends BaseTest
5
{
6
    public function getXsdBaseTypes()
7
    {
8
        return [['xs:dateTime'], ['xs:date'], ['xs:int']];
9
    }
10
11
    /**
12
     * @dataProvider getXsdBaseTypes
13
     */
14
    public function testPrimitiveTypes($type)
15
    {
16
        $schema = $this->reader->readString(
17
            '
18
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
19
                <xs:complexType name="complexType">
20
                    <xs:sequence>
21
                        <xs:element name="el1" type="' . $type . '"></xs:element>
22
                    </xs:sequence>
23
                </xs:complexType>
24
            </xs:schema>');
25
26
27
        $complex = $schema->findType('complexType', 'http://www.example.com');
28
29
        $elements = $complex->getElements();
30
        $this->assertNotNull($elements[0]->getType()->getName());
31
        $this->assertEquals($type, "xs:" . $elements[0]->getType()->getName());
32
    }
33
34
    public function testAnonymousTypes()
35
    {
36
        $schema = $this->reader->readString(
37
            '
38
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
39
                <xs:complexType name="complexType">
40
                    <xs:sequence>
41
                        <xs:element name="el1"></xs:element>
42
                    </xs:sequence>
43
                    <xs:attribute name="att1"></xs:attribute>
44
                </xs:complexType>
45
            </xs:schema>');
46
47
48
        $complex = $schema->findType('complexType', 'http://www.example.com');
49
        $attrs = $complex->getAttributes();
50
        $elements = $complex->getElements();
51
52
        $this->assertEquals("anyType", $attrs[0]->getType()->getName());
53
        $this->assertEquals("anyType", $elements[0]->getType()->getName());
54
55
    }
56
57
    public function testAttrAttr()
58
    {
59
        $schema = $this->reader->readString(
60
            '
61
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
62
                <xs:complexType name="complexType">
63
                    <xs:sequence>
64
                        <xs:element name="el1" nillable="true" type="xs:string" form="qualified"></xs:element>
65
                    </xs:sequence>
66
                    <xs:attribute name="att1" nillable="true" form="qualified" use="required" type="xs:string"></xs:attribute>
67
                </xs:complexType>
68
            </xs:schema>');
69
70
        $complex = $schema->findType('complexType', 'http://www.example.com');
71
        $attrs = $complex->getAttributes();
72
        $this->assertTrue($attrs[0]->isNil());
73
        $this->assertTrue($attrs[0]->isQualified());
74
        $this->assertEquals('required', $attrs[0]->getUse());
75
    }
76
77
    public function testSequenceAll()
78
    {
79
        $schema = $this->reader->readString(
80
            '
81
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
82
                <xs:complexType name="complexType">
83
                    <xs:all>
84
                        <xs:element name="el1" nillable="true" type="xs:string" form="qualified"></xs:element>
85
                        <xs:element name="el2" nillable="true" type="xs:string" form="qualified"></xs:element>
86
                    </xs:all>
87
                </xs:complexType>
88
            </xs:schema>');
89
90
        $complex = $schema->findType('complexType', 'http://www.example.com');
91
        $elements = $complex->getElements();
92
        $this->assertCount(2, $elements);
93
    }
94
95
    public function testElementAttr()
96
    {
97
        $schema = $this->reader->readString(
98
            '
99
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
100
                <xs:complexType name="complexType">
101
                    <xs:sequence>
102
                        <xs:element name="el1" nillable="true" type="xs:string" form="qualified"></xs:element>
103
                    </xs:sequence>
104
                </xs:complexType>
105
            </xs:schema>');
106
107
        $complex = $schema->findType('complexType', 'http://www.example.com');
108
        $elements = $complex->getElements();
109
        $this->assertTrue($elements[0]->isNil());
110
        $this->assertTrue($elements[0]->isQualified());
111
    }
112
113
    public function getMaxOccurences()
114
    {
115
        return [
116
            ['1', 1],
117
            ['0', 0],
118
            ['10', 10],
119
            ['unbounded', -1],
120
        ];
121
    }
122
123
    /**
124
     * @dataProvider getMaxOccurences
125
     */
126
    public function testElementMaxOccurences($xml, $expected)
127
    {
128
        $schema = $this->reader->readString(
129
            '
130
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
131
                <xs:complexType name="complexType">
132
                    <xs:sequence>
133
                        <xs:element name="el1" maxOccurs="' . $xml . '" type="xs:string"></xs:element>
134
                    </xs:sequence>
135
                </xs:complexType>
136
            </xs:schema>');
137
138
        $complex = $schema->findType('complexType', 'http://www.example.com');
139
        $elements = $complex->getElements();
140
        $this->assertEquals($expected, $elements[0]->getMax());
141
    }
142
143
    public function getMinOccurences()
144
    {
145
        return [
146
            ['1', 1],
147
            ['0', 0],
148
        ];
149
    }
150
151
    /**
152
     * @dataProvider getMinOccurences
153
     */
154
    public function testElementMinOccurences($xml, $expected)
155
    {
156
        $schema = $this->reader->readString(
157
            '
158
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
159
                <xs:complexType name="complexType">
160
                    <xs:sequence>
161
                        <xs:element name="el1" minOccurs="' . $xml . '" type="xs:string"></xs:element>
162
                    </xs:sequence>
163
                </xs:complexType>
164
            </xs:schema>');
165
166
        $complex = $schema->findType('complexType', 'http://www.example.com');
167
        $elements = $complex->getElements();
168
        $this->assertEquals($expected, $elements[0]->getMin());
169
    }
170
171
    public function testComplex()
172
    {
173
        $schema = $this->reader->readString(
174
            '
175
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
176
                <xs:complexType name="complexType">
177
                    <xs:sequence>
178
                        <xs:element name="el1" type="xs:string"></xs:element>
179
                        <xs:element name="el2">
180
                        </xs:element>
181
                        <xs:element ref="ex:el3"></xs:element>
182
                        <xs:group ref="g1"></xs:group>
183
                    </xs:sequence>
184
                    <xs:attribute name="att1" type="xs:string"></xs:attribute>
185
                    <xs:attribute name="att2"></xs:attribute>
186
                    <xs:attribute ref="ex:att"></xs:attribute>
187
                    <xs:attributeGroup ref="ex:attGroup"></xs:attributeGroup>
188
                </xs:complexType>
189
190
                <xs:attribute name="att" type="xs:string"></xs:attribute>
191
                <xs:attributeGroup name="attGroup">
192
                    <xs:attribute name="alone" type="xs:string"></xs:attribute>
193
                </xs:attributeGroup>
194
195
                <xs:element name="el3" type="xs:string"></xs:element>
196
                <xs:group name="g1">
197
                    <xs:sequence>
198
                        <xs:element name="aaa" type="xs:string"></xs:element>
199
                    </xs:sequence>
200
                </xs:group>
201
202
            </xs:schema>');
203
204
        $complex = $schema->findType('complexType', 'http://www.example.com');
205
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType', $complex);
206
        $this->assertEquals('http://www.example.com', $complex->getSchema()->getTargetNamespace());
207
        $this->assertEquals('complexType', $complex->getName());
208
209
        $elements = $complex->getElements();
210
        $this->assertCount(4, $elements);
211
212
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\Element', $elements[0]);
213
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\Element', $elements[1]);
214
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem', $elements[2]);
215
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\Group', $elements[3]);
216
217
        $this->assertEquals('el1', $elements[0]->getName());
218
        $this->assertEquals('el2', $elements[1]->getName());
219
        $this->assertEquals('el3', $elements[2]->getName());
220
        $this->assertEquals('g1', $elements[3]->getName());
221
222
        $this->assertEquals("anyType", $elements[1]->getType()->getName());
223
224
        $attributes = $complex->getAttributes();
225
        $this->assertCount(4, $attributes);
226
227
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute', $attributes[0]);
228
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute', $attributes[1]);
229
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem', $attributes[2]);
230
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Group', $attributes[3]);
231
232
        $this->assertEquals('att1', $attributes[0]->getName());
233
        $this->assertEquals('att2', $attributes[1]->getName());
234
        $this->assertEquals('att', $attributes[2]->getName());
235
        $this->assertEquals('attGroup', $attributes[3]->getName());
236
237
        $this->assertEquals("anyType", $attributes[1]->getType()->getName());
238
239
    }
240
241
    public function testSimple()
242
    {
243
        $schema = $this->reader->readString(
244
            '
245
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
246
                <xs:simpleType name="simpleType">
247
248
                </xs:simpleType>
249
            </xs:schema>');
250
251
        $simple = $schema->findType('simpleType', 'http://www.example.com');
252
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType', $simple);
253
        $this->assertEquals('http://www.example.com', $simple->getSchema()->getTargetNamespace());
254
        $this->assertEquals('simpleType', $simple->getName());
255
256
    }
257
258
    public function testComplexSimpleContent()
259
    {
260
        $schema = $this->reader->readString(
261
            '
262
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
263
                <xs:complexType name="complexType">
264
                    <xs:simpleContent>
265
                        <xs:extension base="xs:string"></xs:extension>
266
                    </xs:simpleContent>
267
                    <xs:attribute name="att1" type="xs:string"></xs:attribute>
268
                    <xs:attribute name="att2"></xs:attribute>
269
                    <xs:attribute ref="ex:att"></xs:attribute>
270
                    <xs:attributeGroup ref="ex:attGroup"></xs:attributeGroup>
271
                </xs:complexType>
272
273
                <xs:attribute name="att" type="xs:string"></xs:attribute>
274
                <xs:attributeGroup name="attGroup">
275
                    <xs:attribute name="alone" type="xs:string"></xs:attribute>
276
                </xs:attributeGroup>
277
278
            </xs:schema>');
279
280
        $complex = $schema->findType('complexType', 'http://www.example.com');
281
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\ComplexTypeSimpleContent', $complex);
282
        $this->assertEquals('http://www.example.com', $complex->getSchema()->getTargetNamespace());
283
        $this->assertEquals('complexType', $complex->getName());
284
285
        $extension = $complex->getExtension();
286
        $base1 = $extension->getBase();
287
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType', $base1);
288
        $this->assertEquals('http://www.w3.org/2001/XMLSchema', $base1->getSchema()->getTargetNamespace());
289
        $this->assertEquals('string', $base1->getName());
290
291
292
        $attributes = $complex->getAttributes();
293
        $this->assertCount(4, $attributes);
294
295
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute', $attributes[0]);
296
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute', $attributes[1]);
297
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem', $attributes[2]);
298
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Group', $attributes[3]);
299
300
        $this->assertEquals('att1', $attributes[0]->getName());
301
        $this->assertEquals('att2', $attributes[1]->getName());
302
        $this->assertEquals('att', $attributes[2]->getName());
303
        $this->assertEquals('attGroup', $attributes[3]->getName());
304
305
        $this->assertEquals("anyType", $attributes[1]->getType()->getName());
306
307
    }
308
}
309