@@ -8,32 +8,32 @@ |
||
| 8 | 8 | |
| 9 | 9 | class TextContentsMatcher extends TagMatcher { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Matcher |
|
| 13 | - */ |
|
| 14 | - private $matcher; |
|
| 15 | - |
|
| 16 | - public static function havingTextContents( $text ) { |
|
| 17 | - return new static( Util::wrapValueWithIsEqual( $text ) ); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - public function __construct( Matcher $matcher ) { |
|
| 21 | - parent::__construct(); |
|
| 22 | - $this->matcher = $matcher; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function describeTo( Description $description ) { |
|
| 26 | - $description->appendText( 'having text contents ' )->appendDescriptionOf( $this->matcher ); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param \DOMElement $item |
|
| 31 | - * @param Description $mismatchDescription |
|
| 32 | - * |
|
| 33 | - * @return bool |
|
| 34 | - */ |
|
| 35 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 36 | - return $this->matcher->matches( $item->textContent ); |
|
| 37 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Matcher |
|
| 13 | + */ |
|
| 14 | + private $matcher; |
|
| 15 | + |
|
| 16 | + public static function havingTextContents( $text ) { |
|
| 17 | + return new static( Util::wrapValueWithIsEqual( $text ) ); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + public function __construct( Matcher $matcher ) { |
|
| 21 | + parent::__construct(); |
|
| 22 | + $this->matcher = $matcher; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function describeTo( Description $description ) { |
|
| 26 | + $description->appendText( 'having text contents ' )->appendDescriptionOf( $this->matcher ); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param \DOMElement $item |
|
| 31 | + * @param Description $mismatchDescription |
|
| 32 | + * |
|
| 33 | + * @return bool |
|
| 34 | + */ |
|
| 35 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 36 | + return $this->matcher->matches( $item->textContent ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -8,65 +8,65 @@ |
||
| 8 | 8 | |
| 9 | 9 | class DirectChildElementMatcher extends TypeSafeDiagnosingMatcher { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Matcher |
|
| 13 | - */ |
|
| 14 | - private $matcher; |
|
| 15 | - |
|
| 16 | - public static function havingDirectChild( Matcher $elementMatcher = null ) { |
|
| 17 | - return new static( $elementMatcher ); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - public function __construct( Matcher $matcher = null ) { |
|
| 21 | - parent::__construct( \DOMNode::class ); |
|
| 22 | - $this->matcher = $matcher; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function describeTo( Description $description ) { |
|
| 26 | - $description->appendText( 'having direct child ' ); |
|
| 27 | - if ( $this->matcher ) { |
|
| 28 | - $description->appendDescriptionOf( $this->matcher ); |
|
| 29 | - } |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @param \DOMDocument|\DOMNode $item |
|
| 34 | - * @param Description $mismatchDescription |
|
| 35 | - * |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 39 | - if ( $item instanceof \DOMDocument ) { |
|
| 40 | - $directChildren = iterator_to_array( $item->documentElement->childNodes ); |
|
| 41 | - |
|
| 42 | - $body = array_shift( $directChildren ); |
|
| 43 | - $directChildren = $body->childNodes; |
|
| 44 | - } else { |
|
| 45 | - $directChildren = $item->childNodes; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - if ( $directChildren->length === 0 ) { |
|
| 49 | - $mismatchDescription->appendText( 'with no direct children' ); |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - $childWord = $directChildren->length === 1 ? 'child' : 'children'; |
|
| 54 | - |
|
| 55 | - $mismatchDescription->appendText( "with direct {$childWord} " ); |
|
| 56 | - |
|
| 57 | - if ( !$this->matcher ) { |
|
| 58 | - return count( $directChildren ) !== 0; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - foreach ( $directChildren as $child ) { |
|
| 62 | - if ( $this->matcher && $this->matcher->matches( $child ) ) { |
|
| 63 | - return true; |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $this->matcher->describeMismatch( $child, $mismatchDescription ); |
|
| 68 | - |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Matcher |
|
| 13 | + */ |
|
| 14 | + private $matcher; |
|
| 15 | + |
|
| 16 | + public static function havingDirectChild( Matcher $elementMatcher = null ) { |
|
| 17 | + return new static( $elementMatcher ); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + public function __construct( Matcher $matcher = null ) { |
|
| 21 | + parent::__construct( \DOMNode::class ); |
|
| 22 | + $this->matcher = $matcher; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function describeTo( Description $description ) { |
|
| 26 | + $description->appendText( 'having direct child ' ); |
|
| 27 | + if ( $this->matcher ) { |
|
| 28 | + $description->appendDescriptionOf( $this->matcher ); |
|
| 29 | + } |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @param \DOMDocument|\DOMNode $item |
|
| 34 | + * @param Description $mismatchDescription |
|
| 35 | + * |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
| 39 | + if ( $item instanceof \DOMDocument ) { |
|
| 40 | + $directChildren = iterator_to_array( $item->documentElement->childNodes ); |
|
| 41 | + |
|
| 42 | + $body = array_shift( $directChildren ); |
|
| 43 | + $directChildren = $body->childNodes; |
|
| 44 | + } else { |
|
| 45 | + $directChildren = $item->childNodes; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + if ( $directChildren->length === 0 ) { |
|
| 49 | + $mismatchDescription->appendText( 'with no direct children' ); |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + $childWord = $directChildren->length === 1 ? 'child' : 'children'; |
|
| 54 | + |
|
| 55 | + $mismatchDescription->appendText( "with direct {$childWord} " ); |
|
| 56 | + |
|
| 57 | + if ( !$this->matcher ) { |
|
| 58 | + return count( $directChildren ) !== 0; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + foreach ( $directChildren as $child ) { |
|
| 62 | + if ( $this->matcher && $this->matcher->matches( $child ) ) { |
|
| 63 | + return true; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $this->matcher->describeMismatch( $child, $mismatchDescription ); |
|
| 68 | + |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | } |