1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Unitesting; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_Assert as Assert; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
8
|
|
|
use Closure; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class to execute html related assertions in a StreamInterface instance. |
12
|
|
|
*/ |
13
|
|
|
class Html extends Stream |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Crawler |
17
|
|
|
*/ |
18
|
|
|
protected $html; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor. |
22
|
|
|
* |
23
|
|
|
* @param StreamInterface $stream |
24
|
|
|
* @param AbstractAssert|null $previous |
25
|
|
|
*/ |
26
|
|
|
public function __construct(StreamInterface $stream, Utils\AbstractAssert $previous = null) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($stream, $previous); |
29
|
|
|
|
30
|
|
|
$this->html = new Crawler(); |
31
|
|
|
$this->html->addContent($this->string); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Asserts the number of elements matching with a selector. |
36
|
|
|
* |
37
|
|
|
* @param string $selector |
38
|
|
|
* @param int $count |
39
|
|
|
* @param string $message |
40
|
|
|
* |
41
|
|
|
* @return self |
42
|
|
|
*/ |
43
|
|
|
public function count($selector, $count, $message = '') |
44
|
|
|
{ |
45
|
|
|
return $this->assert($this->html, new Html\Count($selector, $count), $message); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Asserts that there is (at least) one element matching with a selector. |
50
|
|
|
* |
51
|
|
|
* @param string $selector |
52
|
|
|
* @param string $message |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function has($selector, $message = '') |
57
|
|
|
{ |
58
|
|
|
return $this->assert($this->html, new Html\Has($selector), $message); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Asserts that there is not elements matching with a selector. |
63
|
|
|
* |
64
|
|
|
* @param string $selector |
65
|
|
|
* @param string $message |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function hasNot($selector, $message = '') |
70
|
|
|
{ |
71
|
|
|
return $this->assert($this->html, new Html\HasNot($selector), $message); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Asserts that there is (at least) one element matching with a selector and content. |
76
|
|
|
* |
77
|
|
|
* @param string $selector |
78
|
|
|
* @param string $text |
79
|
|
|
* @param string $message |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
|
public function contains($selector, $text, $message = '') |
84
|
|
|
{ |
85
|
|
|
return $this->assert($this->html, new Html\Contains($selector, $text), $message); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Asserts that there is no elements matching with a selector and content. |
90
|
|
|
* |
91
|
|
|
* @param string $selector |
92
|
|
|
* @param string $text |
93
|
|
|
* @param string $message |
94
|
|
|
* |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function notContains($selector, $text, $message = '') |
98
|
|
|
{ |
99
|
|
|
return $this->assert($this->html, new Html\NotContains($selector, $text), $message); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Asserts that the html is valid. |
104
|
|
|
* |
105
|
|
|
* @param string $message |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function isValid($message = '') |
110
|
|
|
{ |
111
|
|
|
return $this->assert($this->string, new Html\IsValid(), $message); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Executes the callback for each element found. |
116
|
|
|
* |
117
|
|
|
* @param string $selector |
118
|
|
|
* @param Closure $callback |
119
|
|
|
* |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
|
|
public function map($selector, Closure $callback) |
123
|
|
|
{ |
124
|
|
|
$this->html->filter($selector)->each($callback); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|