|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Browser\Dom\Assertion; |
|
4
|
|
|
|
|
5
|
|
|
use Zenstruck\Assert; |
|
6
|
|
|
use Zenstruck\Assert\Assertion\ComparisonAssertion; |
|
7
|
|
|
use Zenstruck\Assert\Assertion\Negatable; |
|
8
|
|
|
use Zenstruck\Assert\AssertionFailed; |
|
9
|
|
|
use Zenstruck\Browser\Dom; |
|
10
|
|
|
use Zenstruck\Browser\Dom\Form\Field\ChoiceField; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Kevin Bond <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
|
|
final class FieldCheckedAssertion implements Negatable |
|
18
|
|
|
{ |
|
19
|
|
|
private ChoiceField $field; |
|
20
|
|
|
private array $context; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(Dom $dom, string $selector) |
|
23
|
|
|
{ |
|
24
|
|
|
$field = Assert::try(fn() => $dom->getFormField($selector)); |
|
25
|
|
|
|
|
26
|
|
|
if (!$field instanceof ChoiceField || !$field->is(['radio', 'checkbox'])) { |
|
27
|
|
|
Assert::fail('Expected element matching "{selector}" to be a checkbox or radio.', ['selector' => $selector]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$this->field = $field; |
|
31
|
|
|
$this->context = ['selector' => $selector]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function __invoke(): void |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->field->is('checkbox')) { |
|
37
|
|
|
// todo truthy assertion object in zenstruck/assert |
|
38
|
|
|
if (!$this->field->hasValue()) { |
|
|
|
|
|
|
39
|
|
|
AssertionFailed::throw('Expected checkbox matching "{selector}" to be checked.', $this->context); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// todo wrapper assertion object in zenstruck/assert |
|
46
|
|
|
ComparisonAssertion::same( |
|
47
|
|
|
$this->field->getValue(), |
|
|
|
|
|
|
48
|
|
|
$this->field->attr('value'), |
|
49
|
|
|
'Expected radio matching "{selector}" to be selected.', |
|
50
|
|
|
$this->context |
|
51
|
|
|
)(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function notFailure(): AssertionFailed |
|
55
|
|
|
{ |
|
56
|
|
|
return new AssertionFailed( |
|
57
|
|
|
$this->field->is('checkbox') ? 'Expected checkbox matching "{selector}" to not be checked.' : 'Expected radio matching "{selector}" to not be selected.', |
|
58
|
|
|
$this->context |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|