Passed
Pull Request — 1.x (#62)
by Kevin
02:06
created

FieldSelectedAssertion::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 21
rs 9.7998
c 1
b 0
f 0
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()) {
0 ignored issues
show
Bug introduced by
The method isMultiple() does not exist on Zenstruck\Browser\Dom\Form\Field\ChoiceField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if ($this->field->/** @scrutinizer ignore-call */ isMultiple()) {
Loading history...
40
            (new ContainsAssertion(
41
                $this->expected,
42
                $this->field->getValue(),
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Zenstruck\Browser\Dom\Form\Field\ChoiceField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
                $this->field->/** @scrutinizer ignore-call */ 
43
                              getValue(),
Loading history...
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()),
0 ignored issues
show
Bug introduced by
The method getType() does not exist on Zenstruck\Browser\Dom\Form\Field\ChoiceField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            \sprintf('Expected %s matching "{selector}" to have "{expected}" selected.', $this->field->/** @scrutinizer ignore-call */ getType()),
Loading history...
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