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

AttributesTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 88
Duplicated Lines 36.36 %

Importance

Changes 0
Metric Value
wmc 2
dl 32
loc 88
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 AttributesTest extends BaseTest
5
{
6
7
8
    public function testBase()
9
    {
10
11
        $schema = $this->reader->readString(
12
            '
13
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
14
                <xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
15
                <xs:attribute name="myAttributeOptions" type="xs:string" use="reuqired" nil="true"></xs:attribute>
16
17
                <xs:attributeGroup name="myAttributeGroup">
18
                    <xs:attribute name="alone" type="xs:string"></xs:attribute>
19
                    <xs:attribute ref="ex:myAttribute"></xs:attribute>
20
                    <xs:attributeGroup ref="ex:myAttributeGroup2"></xs:attributeGroup>
21
                </xs:attributeGroup>
22
23
                <xs:attributeGroup name="myAttributeGroup2">
24
                    <xs:attribute name="alone2" type="xs:string"></xs:attribute>
25
                </xs:attributeGroup>
26
            </xs:schema>');
27
28
        $myAttribute = $schema->findAttribute('myAttribute', 'http://www.example.com');
29
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef', $myAttribute);
30
        //$this->assertEquals('http://www.example.com', $myAttribute->getSchema()->getTargetNamespace());
31
        $this->assertEquals('myAttribute', $myAttribute->getName());
32
        $this->assertEquals("string", $myAttribute->getType()->getName());
33
34
35
        $base1 = $myAttribute->getType();
36
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType', $base1);
37
        $this->assertEquals('http://www.w3.org/2001/XMLSchema', $base1->getSchema()->getTargetNamespace());
38
        $this->assertEquals('string', $base1->getName());
39
40
        $myAttributeGroup = $schema->findAttributeGroup('myAttributeGroup', 'http://www.example.com');
41
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Group', $myAttributeGroup);
42
        //$this->assertEquals('http://www.example.com', $myAttribute->getSchema()->getTargetNamespace());
43
        $this->assertEquals('myAttributeGroup', $myAttributeGroup->getName());
44
        $attributesInGroup = $myAttributeGroup->getAttributes();
45
        $this->assertCount(3, $attributesInGroup);
46
47
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute', $attributesInGroup[0]);
48
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef', $attributesInGroup[1]);
49
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\Group', $attributesInGroup[2]);
50
51
52
        $myAttribute = $schema->findAttribute('myAttributeOptions', 'http://www.example.com');
53
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef', $myAttribute);
54
        //$this->assertEquals('http://www.example.com', $myAttribute->getSchema()->getTargetNamespace());
55
        $this->assertEquals('myAttributeOptions', $myAttribute->getName());
56
        $this->assertEquals("string", $myAttribute->getType()->getName());
57
58
    }
59
60
    public function testAnonym()
61
    {
62
        $schema = $this->reader->readString(
63
            '
64
            <xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
65
66
                <xs:attribute name="myAttributeAnonType">
67
                    <xs:simpleType>
68
                        <xs:restriction base="xs:string"></xs:restriction>
69
                    </xs:simpleType>
70
                </xs:attribute>
71
72
            </xs:schema>');
73
74
75
        $myAttributeAnon = $schema->findAttribute('myAttributeAnonType', 'http://www.example.com');
76
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef', $myAttributeAnon);
77
        //$this->assertEquals('http://www.example.com', $myAttribute->getSchema()->getTargetNamespace());
78
        $this->assertEquals('myAttributeAnonType', $myAttributeAnon->getName());
79
        $this->assertNull($myAttributeAnon->getType()->getName());
80
81
        $base2 = $myAttributeAnon->getType();
82
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType', $base2);
83
        $this->assertEquals('http://www.example.com', $base2->getSchema()->getTargetNamespace());
84
        $this->assertTrue(!$base2->getName());
85
86
87
        $restriction1 = $base2->getRestriction();
88
        $base3 = $restriction1->getBase();
89
        $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType', $base3);
90
        $this->assertEquals('http://www.w3.org/2001/XMLSchema', $base3->getSchema()->getTargetNamespace());
91
        $this->assertEquals('string', $base3->getName());
92
    }
93
}
94