Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds phpunit 6.4 as dependency + some PSR2 cleanup #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"name": "jasny/phpunit-xsdvalidation",
"name": "portrino/phpunit-xsdvalidation",
"description": "XSD schema validation constraint for PHPUnit",
"keywords": ["phpunit", "xml", "xsd"],
"license": "MIT",
"authors": [
{
"name": "Arnold Daniels",
"email": "[email protected]"
},
{
"name": "André Wuttig",
"email": "[email protected]"
}
],
"suggest": {
"phpunit/phpunit": "This package can only be used with PHPUnit"
"phpunit/phpunit": "~6.4"
},
"autoload": {
"psr-0": {
Expand Down
53 changes: 31 additions & 22 deletions src/Jasny/PHPUnit/Constraint/XSDValidation.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
<?php
namespace Jasny\PHPUnit\Constraint;

use PHPUnit\Framework\Constraint\Constraint;

/**
* PHPUnit constraint to validate against XSD
*/
class XSDValidation extends \PHPUnit_Framework_Constraint
class XSDValidation extends Constraint
{
/**
* XSD schema filename or source
* @var string
*/
protected $schema;

/**
* LibXML errors
* @var array
*/
protected $errors = [];


/**
* Class constructor
*
* XSDValidation constructor.
*
* @param string $schema filename or source
* @throws \Exception
*/
public function __construct($schema)
{
if (method_exists(get_parent_class(), '__construct')) parent::__construct();

if (method_exists(get_parent_class(), '__construct')) {
parent::__construct();
}

$this->schema = $schema;
if (!$this->schemaIsXml() && !file_exists($this->schema))
if (!$this->schemaIsXml() && !file_exists($this->schema)) {
throw new \Exception("Schema {$this->schema} doesn't exist");
}
}

/**
* Check if schema contains a '<' character.
*
*
* @return string
*/
protected function schemaIsXml()
{
return strpos($this->schema, '<') !== false;
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
Expand All @@ -55,22 +61,25 @@ protected function schemaIsXml()
protected function matches($other)
{
libxml_use_internal_errors(true);

if ($other instanceof \SimpleXMLElement) {
$dom = new \DOMDocument('1.0');
$dom->appendChild($dom->importNode(dom_import_simplexml($other), true));
} elseif ($other instanceof \DOMDocument) {
$dom = $other;
} else {
$dom = \DOMDocument::load($other);
$dom = new \DOMDocument('1.0');
$dom->load($other);
}

$ret = $this->schemaIsXml() ? $dom->schemaValidateSource($this->schema) : $dom->schemaValidate($this->schema);
if (!$ret) $this->errors = libxml_get_errors();

if (!$ret) {
$this->errors = libxml_get_errors();
}

return $ret;
}

/**
* Return the XML errors as additional failure description
*
Expand All @@ -80,14 +89,14 @@ protected function matches($other)
protected function additionalFailureDescription($other)
{
$desc = '';

foreach ($this->errors as $error) {
$desc .= " - " . trim($error->message) . "\n";
}

return $desc;
}

/**
* Returns the description of the failure
*
Expand All @@ -109,10 +118,10 @@ protected function failureDescription($other)
} else {
$xml = $other;
}

return $xml . ' ' . $this->toString();
}

/**
* Returns a string representation of the constraint.
*
Expand Down