Test Setup Failed
Push — static-analysis ( af1336...1f5c7e )
by SignpostMarv
06:23
created

SchemaTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
dl 0
loc 142
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testNotFoundType() 0 6 1
A testBaseEmpty() 0 10 1
B testBase() 0 30 1
A getTypesToSearch() 0 8 1
B testMultipleSchemasInSameFile() 0 36 1
B testGroupRefInType() 0 34 1
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');
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::setExpectedException() has been deprecated: Method deprecated since Release 5.2.0; use expectException() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
        /** @scrutinizer ignore-deprecated */ $this->setExpectedException('GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getElements() does not exist on GoetasWebservices\XML\XSDReader\Schema\Type\Type. It seems like you code against a sub-type of GoetasWebservices\XML\XSDReader\Schema\Type\Type such as GoetasWebservices\XML\XS...Schema\Type\ComplexType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        $this->assertCount(1, $type->/** @scrutinizer ignore-call */ getElements());
Loading history...
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