Completed
Push — master ( f6e07a...607751 )
by Stephan
02:50
created

HtmlValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A assertThatHtmlIsValid() 0 5 1
A assertThatHtmlContains() 0 8 4
B loadHTML() 0 24 3
A wrapHtmlFragment() 0 3 1
A doAssert() 0 21 3
1
<?php
2
3
namespace SRF\Tests\Integration\JSONScript;
4
5
use DOMDocument;
6
use Symfony\Component\CssSelector\CssSelectorConverter;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since   2.5
11
 *
12
 * @author Stephan Gambke
13
 */
14
class HtmlValidator extends \PHPUnit_Framework_Assert {
15
16
	private $documentCache = [];
17
18
	/**
19
	 * @param string $actual
20
	 * @param string $message
21
	 */
22
	public function assertThatHtmlIsValid( $actual, $message = '' ) {
23
24
		$document = $this->loadHTML( $actual );
25
		self::assertTrue( $document !== false, "Failed test `{$message}` (assertion HtmlIsValid) for $actual" );
26
	}
27
28
	/**
29
	 * @param string | string[] $expected
30
	 * @param string $actual
31
	 * @param string $message
32
	 */
33
	public function assertThatHtmlContains( $expected, $actual, $message = '' ) {
34
35
		$this->doAssert( $expected, $actual, function ( $rule, $count, $expectedCount = false ) use ( $message, $actual ) {
36
			$expectedCountText = $expectedCount === false? '' : ( $expectedCount . 'x ' );
37
			$message = "Failed test `{$message}` for assertion HtmlContains: $expectedCountText`$rule` for \n=====\n$actual\n=====";
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $message, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
38
			self::assertTrue( ($expectedCount === false && $count > 0 ) || ( $count === $expectedCount ), $message );
39
		} );
40
	}
41
42
	/**
43
	 * @param string $fragment
44
	 * @return bool|DOMDocument
45
	 */
46
	protected function loadHTML( $fragment ) {
47
48
		$cacheKey = md5( $fragment );
49
50
		if ( !isset( $this->documentCache[ $cacheKey ] ) ) {
51
52
			$fragment = self::wrapHtmlFragment( $fragment );
53
54
			$document = new DOMDocument();
55
			$document->preserveWhiteSpace = false;
56
57
			libxml_use_internal_errors( true );
58
			$result = $document->loadHTML( $fragment );
59
			libxml_use_internal_errors( false );
60
61
			if ( $result === true ) {
62
				$this->documentCache[ $cacheKey ] = $document;
63
			} else {
64
				$this->documentCache[ $cacheKey ] = false;
65
			}
66
		}
67
68
		return $this->documentCache[ $cacheKey ];
69
	}
70
71
	/**
72
	 * @param string $fragment
73
	 * @return string
74
	 */
75
	protected static function wrapHtmlFragment( $fragment ) {
76
		return "<!DOCTYPE html><html><head><meta charset='utf-8'/><title>SomeTitle</title></head><body>$fragment</body></html>";
77
	}
78
79
	/**
80
	 * @param string[] $rules
81
	 * @param string $actual
82
	 * @param callable $cb
83
	 */
84
	private function doAssert( $rules, $actual, $cb ) {
85
86
		$document = $this->loadHTML( $actual );
87
		$xpath = new \DOMXPath( $document );
88
		$converter = new CssSelectorConverter();
89
90
		foreach ( $rules as $key => $rule ) {
91
92
			if ( is_array( $rule ) ) {
93
				$expectedCount = array_pop( $rule );
94
				$rule = array_shift( $rule );
95
			} else {
96
				$expectedCount = false;
97
			}
98
99
			$query = $converter->toXPath( $rule );
100
			$entries = $xpath->evaluate( $query );
101
102
			call_user_func( $cb, $rule, $entries->length, $expectedCount );
103
		}
104
	}
105
106
}
107