Skip to content

Commit

Permalink
Merge pull request #3 from eclipxe13/next-release
Browse files Browse the repository at this point in the history
Version 2.1.0
  • Loading branch information
eclipxe13 authored Mar 30, 2019
2 parents 8fcbaf2 + 7c8f787 commit 33ae600
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 39 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false

env:
- FULL_BUILD_PHP_VERSION="7.0"
- FULL_BUILD_PHP_VERSION="7.3"

before_script:
- phpenv config-rm xdebug.ini
Expand All @@ -24,11 +25,11 @@ script:
- vendor/bin/php-cs-fixer fix --using-cache=no --dry-run --verbose
- |
if [[ $TRAVIS_PHP_VERSION == $FULL_BUILD_PHP_VERSION ]]; then
php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-text --coverage-clover=build/tests/coverage.xml
php -dzend_extension=xdebug.so vendor/bin/phpunit --testdox --verbose --coverage-text --coverage-clover=build/tests/coverage.xml
vendor/bin/phpstan.phar analyse --no-progress --level max src/ tests/
else
vendor/bin/phpunit
vendor/bin/phpunit --testdox --verbose
fi
- vendor/bin/phpstan.phar --no-progress analyse --level max src/ tests/
after_script:
- |
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Version 2.1.0
- Allow create a `SchemaValidator` instance using `DOMDocument`
- Run PHPUnit 7 on PHP >= 7.1
- Run phpstan 0.10/0.11 on PHP >= 7.1

# Version 2.0.2
- Fix bug when running on PHP >= 7.1 and warning was raised when call `DOMDocument::schemaValidateSource`
making impossible to obtain errors from `libxml_clear_errors` and throw a new `LibXmlException`
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ If any of these do not pass, it will result in a complete build failure.
Before you can run these, be sure to `composer install` or `composer update`.

```shell
vendor/bin/parallel-lint src/ tests/
vendor/bin/phplint
vendor/bin/phpcs -sp src/ tests/
vendor/bin/php-cs-fixer fix -v --dry-run
vendor/bin/phpunit --coverage-text
vendor/bin/phpstan.phar analyse --level max src/ tests/
```

## web server instance while running tests
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 - 2017 Carlos C Soto
Copyright (c) 2016 - 2019 Carlos C Soto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ if (! $validator->validate()) {

```php
<?php
$contents = file_get_contents('example.xml');
$validator = new \XmlSchemaValidator\SchemaValidator($contents);
// create SchemaValidator using a DOMDocument
$document = new \DOMDocument();
$document->load('example.xml');
$validator = new \XmlSchemaValidator\SchemaValidator($document);

// change schemas collection to override the schema location of an specific namespace
$schemas = $validator->buildSchemas();
$schemas->create('http://example.org/schemas/x1', './local-schemas/x1.xsd');
Expand Down
10 changes: 10 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

- [ ] Document usage examples

## Planned breaking changes

- [ ] Move sources to namespace `Eclipxe\SchemaValidator`
- [ ] Move tests to namespace `Eclipxe\SchemaValidator\Tests`
- [ ] `SchemaValidator` should be constructed using a `DOMDocument`
- [ ] `SchemaValidator` should offer a new method `createFromString`
- [ ] PHP Minimal version to 7.2 (or 7.1?)
- [ ] Use strict types
- [ ] Review all docblocks, remove or justify

## Done

- [X] Deprecate PHP 5.6 to PHP 7.0 and phpunit from ^5.7 to ^6.3
Expand Down
36 changes: 34 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"ext-libxml": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.2",
"phpunit/phpunit": "^6.2|^7.3",
"overtrue/phplint": "^1.0",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^2.4",
"phpstan/phpstan-shim": "^0.9.1"
"phpstan/phpstan-shim": "^0.9|^0.10|^0.11"
},
"autoload": {
"psr-4": {
Expand All @@ -31,5 +31,37 @@
"psr-4": {
"XmlSchemaValidatorTests\\": "tests/XmlSchemaValidatorTests/"
}
},
"scripts": {
"dev:clean": [
"rm -rf build/",
"find . -type f -name .phpunit.result.cache -delete",
"mkdir -p build/"
],
"dev:build": ["@dev:fix-style", "@dev:test"],
"dev:check-style": [
"vendor/bin/php-cs-fixer fix --dry-run --verbose",
"vendor/bin/phpcs --colors -sp src/ tests/"
],
"dev:fix-style": [
"vendor/bin/php-cs-fixer fix --verbose",
"vendor/bin/phpcbf --colors -sp src/ tests/"
],
"dev:test": [
"vendor/bin/phplint",
"vendor/bin/phpunit --testdox --verbose --stop-on-failure",
"vendor/bin/phpstan analyse --no-progress --level max src/ tests/"
],
"dev:coverage": [
"@php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-text --coverage-html build/coverage/html/"
]
},
"scripts-descriptions": {
"dev:clean": "DEV: recreate build directory, remove any .phpunit.result.cache files",
"dev:build": "DEV: run dev:fix-style dev:tests and dev:docs, run before pull request",
"dev:check-style": "DEV: search for code style errors using php-cs-fixer and phpcs",
"dev:fix-style": "DEV: fix code style errors using php-cs-fixer and phpcbf",
"dev:test": "DEV: run phplint, phpunit and phpstan",
"dev:coverage": "DEV: run phpunit with xdebug and storage coverage in build/coverage/html/"
}
}
13 changes: 2 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
stopOnFailure="false"
processIsolation="false"
backupGlobals="false"
syntaxCheck="true">
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<testsuites>
<testsuite>
<testsuite name="default">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
44 changes: 27 additions & 17 deletions src/XmlSchemaValidator/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ class SchemaValidator
/**
* SchemaValidator constructor.
*
* @param string $content
* @param DOMDocument|string $content
* @throws \InvalidArgumentException if content is empty
* @throws SchemaValidatorException if malformed xml content
*/
public function __construct(string $content)
public function __construct($content)
{
if ('' === $content) {
throw new \InvalidArgumentException('The content to validate must be a non-empty string');
}
$document = new DOMDocument();
try {
LibXmlException::useInternalErrors(function () use ($content, $document) {
$document->loadXML($content, LIBXML_NOWARNING);
});
} catch (LibXmlException $ex) {
throw new SchemaValidatorException('Malformed XML Document: ' . $ex->getMessage());
if ($content instanceof DOMDocument) {
$document = $content;
} else {
$document = $this->createDocumentFromString($content);
}
$this->document = $document;
}
Expand Down Expand Up @@ -84,9 +78,8 @@ public function validateWithSchemas(Schemas $schemas)
}
// build the unique importing schema
$xsd = $schemas->getImporterXsd();
$document = $this->document;
LibXmlException::useInternalErrors(function () use ($document, $xsd) {
$document->schemaValidateSource($xsd);
LibXmlException::useInternalErrors(function () use ($xsd) {
$this->document->schemaValidateSource($xsd);
});
}

Expand All @@ -110,6 +103,7 @@ public function buildSchemas(): Schemas
}

// get all the xsi:schemaLocation attributes in the document
/** @var \DOMNodeList|false $schemasList */
$schemasList = $xpath->query("//@$xsi:schemaLocation");

// schemaLocation attribute not found, no need to continue
Expand All @@ -118,9 +112,9 @@ public function buildSchemas(): Schemas
}

// process every schemaLocation for even parts
for ($s = 0; $s < $schemasList->length; $s++) {
foreach ($schemasList as $node) {
// get the node content
$content = $schemasList->item($s)->nodeValue;
$content = $node->nodeValue;
// get parts without inner spaces
$parts = array_values(array_filter(explode(' ', $content)));
$partsCount = count($parts);
Expand All @@ -138,4 +132,20 @@ public function buildSchemas(): Schemas

return $schemas;
}

private function createDocumentFromString(string $content): DOMDocument
{
if ('' === $content) {
throw new \InvalidArgumentException('The content to validate must be a non-empty string');
}
$document = new DOMDocument();
try {
LibXmlException::useInternalErrors(function () use ($content, $document) {
$document->loadXML($content);
});
} catch (LibXmlException $ex) {
throw new SchemaValidatorException('Malformed XML Document: ' . $ex->getMessage());
}
return $document;
}
}
11 changes: 10 additions & 1 deletion tests/XmlSchemaValidatorTests/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace XmlSchemaValidatorTests;

use DOMDocument;
use XmlSchemaValidator\Schemas;
use XmlSchemaValidator\SchemaValidator;
use XmlSchemaValidator\SchemaValidatorException;
Expand All @@ -13,10 +14,18 @@ public function utilCreateValidator($file)
if (! file_exists($location)) {
$this->markTestSkipped("The file $location was not found");
}
$content = file_get_contents($location);
$content = (string) file_get_contents($location);
return new SchemaValidator($content);
}

public function testConstructUsingExistingDocument()
{
$document = new DOMDocument();
$document->load($this->utilAssetLocation('books-valid.xml'));
new SchemaValidator($document);
$this->assertTrue(true, 'Expected no exception creating the schema validator using a DOMDocument');
}

public function testConstructorWithEmptyString()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down

0 comments on commit 33ae600

Please sign in to comment.