-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssertableForm.php
More file actions
133 lines (104 loc) · 4.33 KB
/
AssertableForm.php
File metadata and controls
133 lines (104 loc) · 4.33 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
<?php
declare(strict_types=1);
namespace Ziadoz\AssertableHtml\Dom\Elements;
use Dom\Element;
use Dom\HTMLElement;
use Ziadoz\AssertableHtml\Concerns\AssertsMany;
use Ziadoz\AssertableHtml\Contracts\PromotableAssertableElement;
use Ziadoz\AssertableHtml\Dom\AssertableElement;
readonly class AssertableForm extends AssertableElement implements PromotableAssertableElement
{
use AssertsMany;
/*
|--------------------------------------------------------------------------
| Interface
|--------------------------------------------------------------------------
*/
/** {@inheritDoc} */
public static function shouldPromote(Element|HTMLElement $element): bool
{
return $element->tagName === 'FORM';
}
/*
|--------------------------------------------------------------------------
| Assert Method
|--------------------------------------------------------------------------
*/
/** Assert the form has the given method attribute. */
public function assertMethod(string $method, ?string $message = null): static
{
$method = trim(mb_strtolower($method));
$this->assertAttribute(
'method',
fn (?string $value): bool => trim(mb_strtolower((string) $value)) === $method,
$message ?? sprintf("The form method doesn't equal [%s].", $method),
);
return $this;
}
/** Assert the form has the GET method attribute. */
public function assertMethodGet(?string $message = null): static
{
$this->assertMethod('get', $message);
return $this;
}
/** Assert the form has the POST method attribute. */
public function assertMethodPost(?string $message = null): static
{
$this->assertMethod('post', $message);
return $this;
}
/** Assert the form has the DIALOG method attribute. */
public function assertMethodDialog(?string $message = null): static
{
$this->assertMethod('dialog', $message);
return $this;
}
/*
|--------------------------------------------------------------------------
| Assert Hidden Method
|--------------------------------------------------------------------------
*/
/** Assert the form has the given hidden input method. */
public function assertHiddenInputMethod(string $selector, string $method, ?string $message = null): static
{
$this->assertMany(function () use ($selector, $method): void {
$method = trim(mb_strtolower($method));
$this->querySelector($selector)
->assertMatchesSelector('input[type="hidden"]')
->assertAttribute('value', fn (?string $value): bool => trim(mb_strtolower((string) $value)) === $method);
}, $message ?? sprintf("The form hidden input method doesn't equal [%s].", $method));
return $this;
}
/** Assert the form has the PUT hidden input method. */
public function assertMethodPut(?string $message = null): static
{
$this->assertHiddenInputMethod('input[type="hidden"][name="_method"]', 'put', $message);
return $this;
}
/** Assert the form has the PATCH hidden input method. */
public function assertMethodPatch(?string $message = null): static
{
$this->assertHiddenInputMethod('input[type="hidden"][name="_method"]', 'patch', $message);
return $this;
}
/** Assert the form has the DELETE hidden input method. */
public function assertMethodDelete(?string $message = null): static
{
$this->assertHiddenInputMethod('input[type="hidden"][name="_method"]', 'delete', $message);
return $this;
}
/*
|--------------------------------------------------------------------------
| Assert Upload
|--------------------------------------------------------------------------
*/
/** Assert the form accepts uploads (has correct enctype and at least one file input. */
public function assertAcceptsUpload(?string $message = null): static
{
$this->assertMany(function (): void {
$this->assertAttribute('enctype', fn (?string $value): bool => trim(mb_strtolower((string) $value)) === 'multipart/form-data')
->assertElementsCountGreaterThanOrEqual('input[type="file"]', 1);
}, $message ?? "The form doesn't accept uploads.");
return $this;
}
}