Skip to content

Commit

Permalink
Merge 6f41dd2 into 5a9b465
Browse files Browse the repository at this point in the history
  • Loading branch information
thiemowmde committed Apr 16, 2018
2 parents 5a9b465 + 6f41dd2 commit dce4aa5
Show file tree
Hide file tree
Showing 20 changed files with 1,137 additions and 1,210 deletions.
19 changes: 19 additions & 0 deletions .phpcs.xml
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<ruleset>
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
<exclude name="Generic.PHP.NoSilencedErrors" />
<exclude name="MediaWiki.Commenting.FunctionComment" />
<exclude name="MediaWiki.Commenting.PhpunitAnnotations" />
<exclude name="MediaWiki.NamingConventions.LowerCamelFunctionsName" />
</rule>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="116" />
</properties>
</rule>

<file>.</file>
<arg name="extensions" value="php" />
<arg name="encoding" value="UTF-8" />
</ruleset>
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -14,7 +14,7 @@
"hamcrest/hamcrest-php": "^1.1.1||^2.0"
},
"require-dev": {
"wikibase/wikibase-codesniffer": "^0.1.0",
"mediawiki/mediawiki-codesniffer": "18.0.0",
"phpunit/phpunit": "^4.8"
},
"autoload": {
Expand Down
21 changes: 0 additions & 21 deletions phpcs.xml

This file was deleted.

129 changes: 62 additions & 67 deletions src/AttributeMatcher.php
Expand Up @@ -6,82 +6,77 @@
use Hamcrest\Matcher;
use Hamcrest\Util;

class AttributeMatcher extends TagMatcher
{
class AttributeMatcher extends TagMatcher {

/**
* @var Matcher
*/
private $attributeNameMatcher;
/**
* @var Matcher
*/
private $attributeNameMatcher;

/**
* @var Matcher|null
*/
private $valueMatcher;
/**
* @var Matcher|null
*/
private $valueMatcher;

public static function withAttribute($attributeName) {
return new static(Util::wrapValueWithIsEqual($attributeName));
}
public static function withAttribute( $attributeName ) {
return new static( Util::wrapValueWithIsEqual( $attributeName ) );
}

/**
* AttributeMatcher constructor.
* @param \Hamcrest\Matcher $attributeNameMatcher
*/
public function __construct(Matcher $attributeNameMatcher)
{
parent::__construct();
/**
* AttributeMatcher constructor.
* @param \Hamcrest\Matcher $attributeNameMatcher
*/
public function __construct( Matcher $attributeNameMatcher ) {
parent::__construct();

$this->attributeNameMatcher = $attributeNameMatcher;
}
$this->attributeNameMatcher = $attributeNameMatcher;
}

/**
* @param Matcher|mixed $value
* @return AttributeMatcher
*/
public function havingValue($value)
{
//TODO: Throw exception if value is set
$result = clone $this;
$result->valueMatcher = Util::wrapValueWithIsEqual($value);
/**
* @param Matcher|mixed $value
* @return AttributeMatcher
*/
public function havingValue( $value ) {
// TODO: Throw exception if value is set
$result = clone $this;
$result->valueMatcher = Util::wrapValueWithIsEqual( $value );

return $result;
}
return $result;
}

public function describeTo(Description $description)
{
$description->appendText('with attribute ')
->appendDescriptionOf($this->attributeNameMatcher);
if ($this->valueMatcher) {
$description->appendText(' having value ')
->appendDescriptionOf($this->valueMatcher);
}
}
public function describeTo( Description $description ) {
$description->appendText( 'with attribute ' )
->appendDescriptionOf( $this->attributeNameMatcher );
if ( $this->valueMatcher ) {
$description->appendText( ' having value ' )
->appendDescriptionOf( $this->valueMatcher );
}
}

/**
* @param \DOMElement $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription)
{
/** @var \DOMAttr $attribute */
foreach ($item->attributes as $attribute) {
if ($this->valueMatcher) {
if (
$this->attributeNameMatcher->matches($attribute->name)
&& $this->valueMatcher->matches($attribute->value)
) {
return true;
}
} else {
if ($this->attributeNameMatcher->matches($attribute->name)) {
return true;
}
}
}
/**
* @param \DOMElement $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
/** @var \DOMAttr $attribute */
foreach ( $item->attributes as $attribute ) {
if ( $this->valueMatcher ) {
if (
$this->attributeNameMatcher->matches( $attribute->name )
&& $this->valueMatcher->matches( $attribute->value )
) {
return true;
}
} else {
if ( $this->attributeNameMatcher->matches( $attribute->name ) ) {
return true;
}
}
}

return false;
}
return false;
}

}
98 changes: 47 additions & 51 deletions src/ChildElementMatcher.php
Expand Up @@ -6,66 +6,62 @@
use Hamcrest\Matcher;
use Hamcrest\TypeSafeDiagnosingMatcher;

class ChildElementMatcher extends TypeSafeDiagnosingMatcher
{
class ChildElementMatcher extends TypeSafeDiagnosingMatcher {

/**
* @var Matcher|null
*/
private $matcher;
/**
* @var Matcher|null
*/
private $matcher;

public static function havingChild(Matcher $elementMatcher = null) {
return new static($elementMatcher);
}
public static function havingChild( Matcher $elementMatcher = null ) {
return new static( $elementMatcher );
}

public function __construct(Matcher $matcher = null)
{
parent::__construct(\DOMNode::class);
$this->matcher = $matcher;
}
public function __construct( Matcher $matcher = null ) {
parent::__construct( \DOMNode::class );
$this->matcher = $matcher;
}

public function describeTo(Description $description)
{
$description->appendText('having child ');
if ($this->matcher) {
$description->appendDescriptionOf($this->matcher);
}
}
public function describeTo( Description $description ) {
$description->appendText( 'having child ' );
if ( $this->matcher ) {
$description->appendDescriptionOf( $this->matcher );
}
}

/**
* @param \DOMDocument|\DOMNode $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription)
{
if ($item instanceof \DOMDocument) {
$directChildren = iterator_to_array($item->documentElement->childNodes);
/**
* @param \DOMDocument|\DOMNode $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
if ( $item instanceof \DOMDocument ) {
$directChildren = iterator_to_array( $item->documentElement->childNodes );

$body = array_shift($directChildren);
$directChildren = $body->childNodes;
} else {
$directChildren = $item->childNodes;
}
$body = array_shift( $directChildren );
$directChildren = $body->childNodes;
} else {
$directChildren = $item->childNodes;
}

if ($directChildren->length === 0) {
$mismatchDescription->appendText('having no children');
return false;
}
if ( $directChildren->length === 0 ) {
$mismatchDescription->appendText( 'having no children' );
return false;
}

if (!$this->matcher) {
return $directChildren->length > 0;
}
if ( !$this->matcher ) {
return $directChildren->length > 0;
}

foreach (new XmlNodeRecursiveIterator($directChildren) as $child) {
if ($this->matcher && $this->matcher->matches($child)) {
return true;
}
}
foreach ( new XmlNodeRecursiveIterator( $directChildren ) as $child ) {
if ( $this->matcher && $this->matcher->matches( $child ) ) {
return true;
}
}

$mismatchDescription->appendText('having no children ')->appendDescriptionOf($this->matcher);
return false;
}
$mismatchDescription->appendText( 'having no children ' )->appendDescriptionOf( $this->matcher );
return false;
}

}
80 changes: 38 additions & 42 deletions src/ClassMatcher.php
Expand Up @@ -6,47 +6,43 @@
use Hamcrest\Matcher;
use Hamcrest\Util;

class ClassMatcher extends TagMatcher
{

/**
* @var Matcher
*/
private $classMatcher;

public static function withClass($class) {
return new static(Util::wrapValueWithIsEqual($class));
}

public function __construct(Matcher $class)
{
parent::__construct();
$this->classMatcher = $class;
}

public function describeTo(Description $description)
{
$description->appendText('with class ')->appendDescriptionOf($this->classMatcher);
}

/**
* @param \DOMElement $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription)
{
$classAttribute = $item->getAttribute('class');

$classes = preg_split('/\s+/u', $classAttribute);
foreach ($classes as $class) {
if ($this->classMatcher->matches($class)) {
return true;
}
}

return false;
}
class ClassMatcher extends TagMatcher {

/**
* @var Matcher
*/
private $classMatcher;

public static function withClass( $class ) {
return new static( Util::wrapValueWithIsEqual( $class ) );
}

public function __construct( Matcher $class ) {
parent::__construct();
$this->classMatcher = $class;
}

public function describeTo( Description $description ) {
$description->appendText( 'with class ' )->appendDescriptionOf( $this->classMatcher );
}

/**
* @param \DOMElement $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
$classAttribute = $item->getAttribute( 'class' );

$classes = preg_split( '/\s+/u', $classAttribute );
foreach ( $classes as $class ) {
if ( $this->classMatcher->matches( $class ) ) {
return true;
}
}

return false;
}

}

0 comments on commit dce4aa5

Please sign in to comment.