|
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\ContainsAssertion; |
|
8
|
|
|
use Zenstruck\Assert\Assertion\Negatable; |
|
9
|
|
|
use Zenstruck\Assert\AssertionFailed; |
|
10
|
|
|
use Zenstruck\Browser\Dom; |
|
11
|
|
|
use Zenstruck\Browser\Dom\Form\Field\ChoiceField; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Kevin Bond <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
final class FieldSelectedAssertion implements Negatable |
|
19
|
|
|
{ |
|
20
|
|
|
private ChoiceField $field; |
|
21
|
|
|
private string $expected; |
|
22
|
|
|
private array $context; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Dom $dom, string $selector, string $expected) |
|
25
|
|
|
{ |
|
26
|
|
|
$field = Assert::try(fn() => $dom->getFormField($selector)); |
|
27
|
|
|
|
|
28
|
|
|
if (!$field instanceof ChoiceField || !$field->is(['radio', 'select'])) { |
|
29
|
|
|
Assert::fail('Expected element matching "{selector}" to be a select or radio.', ['selector' => $selector]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->field = $field; |
|
33
|
|
|
$this->expected = $expected; |
|
34
|
|
|
$this->context = ['selector' => $selector, 'needle' => $expected, 'expected' => $this->expected]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function __invoke(): void |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->field->isMultiple()) { |
|
|
|
|
|
|
40
|
|
|
(new ContainsAssertion( |
|
41
|
|
|
$this->expected, |
|
42
|
|
|
$this->field->getValue(), |
|
|
|
|
|
|
43
|
|
|
'Expected multi-select matching "{selector}" to have "{needle}" selected.', |
|
44
|
|
|
\array_merge($this->context, [ |
|
45
|
|
|
'compare_expected' => [$this->expected], |
|
46
|
|
|
'compare_actual' => $this->field->getValue(), |
|
47
|
|
|
]) |
|
48
|
|
|
))(); |
|
49
|
|
|
|
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
ComparisonAssertion::same( |
|
54
|
|
|
$this->field->getValue(), |
|
55
|
|
|
$this->expected, |
|
56
|
|
|
\sprintf('Expected %s matching "{selector}" to have "{expected}" selected.', $this->field->getType()), |
|
|
|
|
|
|
57
|
|
|
$this->context |
|
58
|
|
|
)(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function notFailure(): AssertionFailed |
|
62
|
|
|
{ |
|
63
|
|
|
return new AssertionFailed( |
|
64
|
|
|
$this->field->isMultiple() ? 'Expected multi-select matching "{selector}" to not have "{needle}" selected.' : \sprintf('Expected %s matching "{selector}" to not have "{expected}" selected.', $this->field->getType()), |
|
65
|
|
|
$this->context |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|