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

FieldCheckedAssertion::notFailure()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
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\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()) {
0 ignored issues
show
Bug introduced by
The method hasValue() 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

38
            if (!$this->field->/** @scrutinizer ignore-call */ hasValue()) {
Loading history...
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(),
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

47
            $this->field->/** @scrutinizer ignore-call */ 
48
                          getValue(),
Loading history...
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