-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssertableElement.php
More file actions
141 lines (116 loc) · 4.21 KB
/
Copy pathAssertableElement.php
File metadata and controls
141 lines (116 loc) · 4.21 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace Ziadoz\AssertableHtml\Dom;
use Dom\Element;
use Dom\HTMLElement;
use PHPUnit\Framework\Assert as PHPUnit;
use Ziadoz\AssertableHtml\Concerns\Asserts\AssertsElement;
use Ziadoz\AssertableHtml\Concerns\IdentifiesElement;
use Ziadoz\AssertableHtml\Concerns\Targetable;
use Ziadoz\AssertableHtml\Concerns\Whenable;
use Ziadoz\AssertableHtml\Concerns\Withable;
readonly class AssertableElement
{
use AssertsElement;
use IdentifiesElement;
use Targetable;
use Whenable;
use Withable;
/** The element's inner HTML. */
public string $html;
/** The element's classes. */
public AssertableClassesList $classes;
/** The element's attributes. */
public AssertableAttributesList $attributes;
/** The element's tag (lowercase). */
public string $tag;
/** The element's ID. */
public string $id;
/** The element's text. */
public AssertableText $text;
/** Create an assertable element. */
public function __construct(private HTMLElement|Element $element)
{
// Properties
$this->html = $this->element->innerHTML;
$this->classes = new AssertableClassesList($this->element->classList);
$this->attributes = new AssertableAttributesList($this->element->attributes);
$this->tag = strtolower($this->element->tagName);
$this->id = $this->element->id;
$this->text = new AssertableText($this->element->textContent);
}
/** Get the underlying HTML element. */
private function getElement(): HTMLElement|Element
{
return $this->element;
}
/** Promote this assertable element to an element-specific equivalent assertable element, if possible. */
public function promote(): static
{
return new AssertableElementPromoter($this->getElement())->promote() ?? $this;
}
/** Get the assertable element HTML. */
public function getHtml(): string
{
return $this->element->ownerDocument->saveHtml($this->element);
}
/** Dump the assertable element. */
public function dump(): void
{
dump($this->getHtml());
}
/** Dump and die the assertable element. */
public function dd(): never
{
dd($this->getHtml());
}
/*
|--------------------------------------------------------------------------
| Native
|--------------------------------------------------------------------------
*/
/** Return whether the assertable element contains the given assertable element. */
public function contains(self $other): bool
{
return $this->element->contains($other->getElement());
}
/** Return the closest matching assertable element. */
public function closest(string $selector): static
{
if (($element = $this->element->closest($selector)) === null) {
PHPUnit::fail(sprintf(
"The element [%s] doesn't have a closest element matching the given selector [%s].",
$this->identifier(),
$selector,
));
}
return new static($element);
}
/** Return whether the assertable element matches the given selector. */
public function matches(string $selector): bool
{
return $this->element->matches($selector);
}
/** Return the assertable element matches the given selector. */
public function querySelector(string $selector): static
{
if (($element = $this->element->querySelector($selector)) === null) {
PHPUnit::fail(sprintf(
"The element [%s] doesn't contain an element matching the given selector [%s].",
$this->identifier(),
$selector,
));
}
return new static($element);
}
/** Return assertable elements matches the given selector. */
public function querySelectorAll(string $selector): AssertableElementsList
{
return new AssertableElementsList($this->element->querySelectorAll($selector));
}
/** Return assertable elements matches the given tag. */
public function getElementsByTagName(string $tag): AssertableElementsList
{
return new AssertableElementsList($this->element->getElementsByTagName($tag));
}
}