|
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====="; |
|
|
|
|
|
|
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
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement 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
Change visible in outer-scope