Skip to content

Commit

Permalink
Allow PHPUnit 8 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and keradus committed Mar 21, 2019
1 parent 130a84a commit 0800c44
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
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/
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ php:
- 7.0
- 7.1
- 7.2
- nightly
- 7.3
- 7.4snapshot

env:
global:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": "^5.5 || ^7.0",
"phpunit/phpunit": "^5.7.23 || ^6.4.3 || ^7.0",
"phpunit/phpunit": "^5.7.23 || ^6.4.3 || ^7.0 || ^8.0",
"phpunitgoodpractices/polyfill": "^1.1"
},
"conflict": {
Expand Down
4 changes: 3 additions & 1 deletion src/Constraint/XmlMatchesXsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0') < 0) {
class_alias(XmlMatchesXsdForV5::class, XmlMatchesXsd::class);
} else {
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '8.0.0') < 0) {
class_alias(XmlMatchesXsdForV7::class, XmlMatchesXsd::class);
} else {
class_alias(XmlMatchesXsdForV8::class, XmlMatchesXsd::class);
}
142 changes: 142 additions & 0 deletions src/Constraint/XmlMatchesXsdForV8.php
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);
}
}
}

0 comments on commit 0800c44

Please sign in to comment.