Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 14, 2016
1 parent 69fe225 commit 7b2891d
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ Getting started
---------------

```php
use Goetas\XML\XSDReader\SchemaReader;
use GoetasWebservices\XML\XSDReader\SchemaReader;

$reader = new SchemaReader();
$schema = $reader->readFile("http://www.example.com/exaple.xsd");

// $schema is instance of Goetas\XML\XSDReader\Schema\Schema;
// $schema is instance of GoetasWebservices\XML\XSDReader\Schema\Schema;

// Now you can navigate the entire schema structure

foreach ($schema->getSchema() as $innerSchema){
foreach ($schema->getSchemas() as $innerSchema){

}
foreach ($schema->getTypes() as $type){
Expand All @@ -67,5 +67,5 @@ foreach ($schema->getAttributeGroups() as $attrGroup){
Note
----

I'm sorry for the *terrible* english fluency used inside the documentation, I'm trying to improve it.
I'm sorry for the *terrible* english fluency used inside the documentation, I'm trying to improve it.
Pull Requests are welcome.
13 changes: 7 additions & 6 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,10 @@ private function loadRestriction(Type $type, DOMElement $node)
'length',
'minLength',
'maxLength',
'minInclusve',
'maxInclusve',
'minExclusve',
'maxEXclusve'
'minInclusive',
'maxInclusive',
'minExclusive',
'maxExclusive'
], true)) {
$restriction->addCheck($childNode->localName,
[
Expand All @@ -555,7 +555,7 @@ private static function splitParts(DOMElement $node, $typeName)
list ($prefix, $name) = explode(':', $typeName);
}

$namespace = $node->lookupNamespaceURI($prefix);
$namespace = $node->lookupNamespaceURI($prefix ?: null);
return array(
$name,
$namespace,
Expand Down Expand Up @@ -633,7 +633,8 @@ private function fillItem(Item $element, DOMElement $node)

private function loadImport(Schema $schema, DOMElement $node)
{
$file = UrlUtils::resolveRelativeUrl($node->ownerDocument->documentURI, $node->getAttribute("schemaLocation"));
$base = urldecode($node->ownerDocument->documentURI);
$file = UrlUtils::resolveRelativeUrl($base, $node->getAttribute("schemaLocation"));
if ($node->hasAttribute("namespace")
&& isset(self::$globalSchemaInfo[$node->getAttribute("namespace")])
&& isset($this->loadedFiles[self::$globalSchemaInfo[$node->getAttribute("namespace")]])
Expand Down
27 changes: 27 additions & 0 deletions tests/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace GoetasWebservices\XML\XSDReader\Tests;

class FilesystemTest extends BaseTest
{

/**
* Test that a referenced Xsd file is found when the base-path contains " " (space-character).
*
* Covers the issue described in {@link https://github.com/goetas/xsd-reader/pull/10 PR #10}.
*/
public function testReferencedOnFileSystem_1()
{
/*
* Using vfsStream seems ideal, but currently seems to have an issue with directorypaths with a space in
* combination with DOMDocument::load(). For now use actual filesystem to create the testcase.
*/
$schemaXsd = __DIR__ . DIRECTORY_SEPARATOR . 'foo bar' . DIRECTORY_SEPARATOR . 'schema.xsd';

$schema = $this->reader->readFile($schemaXsd);

$this->assertCount(1, $schema->getTypes());
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType', $schema->findType('myType', 'http://www.example.com'));
}

}
244 changes: 244 additions & 0 deletions tests/RestrictionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php
namespace GoetasWebservices\XML\XSDReader\Tests;

/**
* @group Restriction
*/
class RestrictionsTest extends BaseTest
{
/**
* Test the correct detection an Enumeration-restriction.
*/
public function testRestriction_1()
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="enumeration">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="foo"/>
<xs:enumeration value="bar"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>');

$element = $schema->findElement('enumeration', 'http://www.example.com');
$simpleType = $element->getType();
$restriction = $simpleType->getRestriction();
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction', $restriction);

$expectedChecks = array(
'enumeration' => array(
array(
'value' => 'foo',
'doc' => '',
),
array(
'value' => 'bar',
'doc' => '',
),
),
);
$this->assertEquals($expectedChecks, $restriction->getChecks());
}

/**
* Test the correct detection a pattern-restriction.
*/
public function testRestriction_2()
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="pattern">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>');

$element = $schema->findElement('pattern', 'http://www.example.com');
$simpleType = $element->getType();
$restriction = $simpleType->getRestriction();
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction', $restriction);

$expectedChecks = array(
'pattern' => array(
array(
'value' => '[a-zA-Z0-9]',
'doc' => '',
),
),
);
$this->assertEquals($expectedChecks, $restriction->getChecks());
}

/**
* Test the correct detection a length-restriction.
*/
public function testRestriction_3()
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="length">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>');

$element = $schema->findElement('length', 'http://www.example.com');
$simpleType = $element->getType();
$restriction = $simpleType->getRestriction();
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction', $restriction);

$expectedChecks = array(
'length' => array(
array(
'value' => '10',
'doc' => '',
),
),
);
$this->assertEquals($expectedChecks, $restriction->getChecks());
}

/**
* Test the correct detection a minLength- and maxLength-restriction.
*/
public function testRestriction_4()
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="minMaxLength">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>');

$element = $schema->findElement('minMaxLength', 'http://www.example.com');
$simpleType = $element->getType();
$restriction = $simpleType->getRestriction();
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction', $restriction);

$expectedChecks = array(
'minLength' => array(
array(
'value' => '5',
'doc' => '',
),
),
'maxLength' => array(
array(
'value' => '8',
'doc' => '',
),
),
);
$this->assertEquals($expectedChecks, $restriction->getChecks());
}

/**
* Test the correct detection a minInclusive- and maxInclusive-restriction.
*/
public function testRestriction_5()
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="minMaxInclusive">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>');

$element = $schema->findElement('minMaxInclusive', 'http://www.example.com');
$simpleType = $element->getType();
$restriction = $simpleType->getRestriction();
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction', $restriction);

$expectedChecks = array(
'minInclusive' => array(
array(
'value' => '1',
'doc' => '',
),
),
'maxInclusive' => array(
array(
'value' => '10',
'doc' => '',
),
),
);
$this->assertEquals($expectedChecks, $restriction->getChecks());
}

/**
* Test the correct detection a minExclusive- and maxExclusive-restriction.
*/
public function testRestriction_6()
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="minMaxExclusive">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minExclusive value="1"/>
<xs:maxExclusive value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>');

$element = $schema->findElement('minMaxExclusive', 'http://www.example.com');
$simpleType = $element->getType();
$restriction = $simpleType->getRestriction();
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction', $restriction);

$expectedChecks = array(
'minExclusive' => array(
array(
'value' => '1',
'doc' => '',
),
),
'maxExclusive' => array(
array(
'value' => '10',
'doc' => '',
),
),
);
$this->assertEquals($expectedChecks, $restriction->getChecks());
}
}
4 changes: 3 additions & 1 deletion tests/UrlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function testFilePaths()
{
$this->assertEquals('file:///test', UrlUtils::resolveRelativeUrl('file:///', '/test'));
$this->assertEquals('file:///test', UrlUtils::resolveRelativeUrl('file:///', 'test'));
/* Assert that any filenames will be stripped from base */
$this->assertEquals('file:///bar.xsd', UrlUtils::resolveRelativeUrl('file:///foo.xsd', 'bar.xsd'));
}


Expand All @@ -79,4 +81,4 @@ public function testRegularPathsParent()
$this->assertEquals('/testing', UrlUtils::resolveRelativeUrl('/test/child', '../testing'));
}

}
}
10 changes: 10 additions & 0 deletions tests/foo bar/referenced.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="myType"></xs:complexType>
<xs:element name="myElement" type="myType"></xs:element>

<xs:group name="myGroup">
<xs:sequence></xs:sequence>
</xs:group>
<xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
<xs:attributeGroup name="myAttributeGroup"></xs:attributeGroup>
</xs:schema>
3 changes: 3 additions & 0 deletions tests/foo bar/schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="referenced.xsd" />
</xs:schema>
10 changes: 10 additions & 0 deletions tests/fooz%20baz/referenced.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="myType"></xs:complexType>
<xs:element name="myElement" type="myType"></xs:element>

<xs:group name="myGroup">
<xs:sequence></xs:sequence>
</xs:group>
<xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
<xs:attributeGroup name="myAttributeGroup"></xs:attributeGroup>
</xs:schema>
3 changes: 3 additions & 0 deletions tests/fooz%20baz/schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="referenced.xsd" />
</xs:schema>

0 comments on commit 7b2891d

Please sign in to comment.