/
HasInElement.php
78 lines (66 loc) · 1.67 KB
/
HasInElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Illuminate\Foundation\Testing\Constraints;
use Symfony\Component\DomCrawler\Crawler;
class HasInElement extends PageConstraint
{
/**
* The name or ID of the element.
*
* @var string
*/
protected $element;
/**
* The text expected to be found.
*
* @var string
*/
protected $text;
/**
* Create a new constraint instance.
*
* @param string $element
* @param string $text
* @return void
*/
public function __construct($element, $text)
{
$this->text = $text;
$this->element = $element;
}
/**
* Check if the source or text is found within the element in the given crawler.
*
* @param \Symfony\Component\DomCrawler\Crawler|string $crawler
* @return bool
*/
public function matches($crawler)
{
$elements = $this->crawler($crawler)->filter($this->element);
$pattern = $this->getEscapedPattern($this->text);
foreach ($elements as $element) {
$element = new Crawler($element);
if (preg_match("/$pattern/i", $element->html())) {
return true;
}
}
return false;
}
/**
* Returns the description of the failure.
*
* @return string
*/
protected function getFailureDescription()
{
return sprintf('[%s] contains %s', $this->element, $this->text);
}
/**
* Returns the reversed description of the failure.
*
* @return string
*/
protected function getReverseFailureDescription()
{
return sprintf('[%s] does not contain %s', $this->element, $this->text);
}
}