-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
130a84a
commit 0800c44
Showing
5 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/.php_cs | ||
/.php_cs.cache | ||
/.phpunit.result.cache | ||
/composer.lock | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ php: | |
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
- nightly | ||
- 7.3 | ||
- 7.4snapshot | ||
|
||
env: | ||
global: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer / PHPUnit Constraint XmlMatchesXsd. | ||
* | ||
* (c) SpacePossum | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
|
||
/** | ||
* @author SpacePossum | ||
* | ||
* @internal | ||
*/ | ||
final class XmlMatchesXsdForV8 extends Constraint | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $xmlConstraintErrors = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $xsd; | ||
|
||
/** | ||
* @param string $xsd | ||
*/ | ||
public function __construct($xsd) | ||
{ | ||
// replace first only | ||
$needle = 'http://www.w3.org/2001/xml.xsd'; | ||
if (false !== $pos = strpos($xsd, $needle)) { | ||
$xsd = substr_replace($xsd, 'file:///'.str_replace('\\', '/', __DIR__).'/xml.xsd', $pos, \strlen($needle)); | ||
} | ||
|
||
$this->xsd = $xsd; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toString(): string | ||
{ | ||
return 'matches XSD'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function failureDescription($other): string | ||
{ | ||
if (\is_string($other)) { | ||
return sprintf("%s %s.\n%s", $other, $this->toString(), implode("\n", $this->xmlConstraintErrors)); | ||
} | ||
|
||
if (\is_object($other)) { | ||
$type = sprintf('%s#%s', \get_class($other), method_exists($other, '__toString') ? $other->__toString() : ''); | ||
} elseif (null === $other) { | ||
$type = 'null'; | ||
} else { | ||
$type = \gettype($other).'#'.$other; | ||
} | ||
|
||
return $type.' '.$this->toString(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function matches($other): bool | ||
{ | ||
return \is_string($other) | ||
? $this->stringMatches($other) | ||
: false | ||
; | ||
} | ||
|
||
/** | ||
* @param string $other | ||
* | ||
* @return bool | ||
*/ | ||
private function stringMatches($other) | ||
{ | ||
$internalErrors = libxml_use_internal_errors(true); | ||
$disableEntities = libxml_disable_entity_loader(true); | ||
libxml_clear_errors(); | ||
|
||
$dom = new \DOMDocument(); | ||
$dom->preserveWhiteSpace = false; | ||
$dom->validateOnParse = true; | ||
|
||
if (!@$dom->loadXML($other, LIBXML_NONET | (\defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) { | ||
libxml_disable_entity_loader($disableEntities); | ||
$this->setXMLConstraintErrors(); | ||
libxml_clear_errors(); | ||
libxml_use_internal_errors($internalErrors); | ||
|
||
return false; | ||
} | ||
|
||
$dom->normalizeDocument(); | ||
|
||
libxml_disable_entity_loader($disableEntities); | ||
libxml_clear_errors(); | ||
|
||
if (false === $result = @$dom->schemaValidateSource($this->xsd)) { | ||
$this->setXMLConstraintErrors(); | ||
} | ||
|
||
libxml_clear_errors(); | ||
libxml_use_internal_errors($internalErrors); | ||
|
||
return $result; | ||
} | ||
|
||
private function setXMLConstraintErrors() | ||
{ | ||
foreach (libxml_get_errors() as $error) { | ||
if (LIBXML_ERR_WARNING === $error->level) { | ||
$level = 'warning '; | ||
} elseif (LIBXML_ERR_ERROR === $error->level) { | ||
$level = 'error '; | ||
} elseif (LIBXML_ERR_FATAL === $error->level) { | ||
$level = 'fatal '; | ||
} else { | ||
$level = ''; | ||
} | ||
|
||
$this->xmlConstraintErrors[] = sprintf('[%s%s] %s (line %d, column %d).', $level, $error->code, trim($error->message), $error->line, $error->column); | ||
} | ||
} | ||
} |