Passed
Push — master ( de4139...96da48 )
by Josh
06:18
created

FieldAssertions::assertFieldMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JoshGaber\NovaUnit\Traits;
4
5
use Illuminate\Http\Request;
6
use JoshGaber\NovaUnit\Constraints\HasField;
7
use JoshGaber\NovaUnit\Constraints\HasValidFields;
8
use JoshGaber\NovaUnit\Fields\FieldHelper;
9
use JoshGaber\NovaUnit\Fields\FieldNotFoundException;
10
use JoshGaber\NovaUnit\Fields\MockFieldElement;
11
use JoshGaber\NovaUnit\Lenses\MockLens;
12
use JoshGaber\NovaUnit\Resources\MockResource;
13
use PHPUnit\Framework\Assert as PHPUnit;
14
use PHPUnit\Framework\Constraint\IsType;
15
16
trait FieldAssertions
17
{
18
    /**
19
     * Checks that this Nova component has a field with the same name or attribute.
20
     *
21
     * @param string $field The name or attribute of the field
22
     * @param string $message
23
     * @return $this
24
     */
25 6
    public function assertHasField(string $field, string $message = ''): self
26
    {
27 6
        PHPUnit::assertThat(
28 6
            $this->component->fields(Request::createFromGlobals()),
29 6
            new HasField($field, $this->allowPanels()),
30 6
            $message
31
        );
32
33 4
        return $this;
34
    }
35
36
    /**
37
     * Checks that no field on this Nova component has a name or attribute matching
38
     * the given parameter.
39
     *
40
     * @param string $field
41
     * @param string $message
42
     * @return $this
43
     */
44 4
    public function assertFieldMissing(string $field, string $message = ''): self
45
    {
46 4
        PHPUnit::assertThat(
47 4
            $this->component->fields(Request::createFromGlobals()),
48 4
            PHPUnit::logicalNot(new HasField($field, $this->allowPanels())),
49 4
            $message
50
        );
51
52 2
        return $this;
53
    }
54
55
    /**
56
     * Asserts that this Nova Action has no fields defined.
57
     * @param string $message
58
     * @return $this
59
     */
60 2
    public function assertHasNoFields(string $message = ''): self
61
    {
62 2
        PHPUnit::assertCount(0, $this->component->fields(Request::createFromGlobals()), $message);
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * Asserts that all fields defined on this Nova Action are valid fields.
69
     *
70
     * @param string $message
71
     * @return $this
72
     */
73 5
    public function assertHasValidFields(string $message = ''): self
74
    {
75 5
        PHPUnit::assertThat(
76 5
            $this->component->fields(Request::createFromGlobals()),
77 5
            PHPUnit::logicalAnd(
78 5
                new IsType(IsType::TYPE_ARRAY),
79 5
                new HasValidFields($this->allowPanels())
80
            ),
81 5
            $message
82
        );
83
84 3
        return $this;
85
    }
86
87
    /**
88
     * Searches for a matching field instance on this component for testing.
89
     *
90
     * @param string $fieldName The name or attribute of the field to return
91
     * @return MockFieldElement
92
     * @throws FieldNotFoundException
93
     */
94 30
    public function field(string $fieldName): MockFieldElement
95
    {
96 30
        $field = FieldHelper::findField(
97 30
            $this->component->fields(Request::createFromGlobals()),
98 30
            $fieldName,
99 30
            $this->allowPanels()
100
        );
101
102 30
        if (is_null($field)) {
103 1
            throw new FieldNotFoundException();
104
        }
105
106 29
        return new MockFieldElement($field);
107
    }
108
109 45
    private function allowPanels(): bool
110
    {
111 45
        return $this instanceof MockLens || $this instanceof MockResource;
112
    }
113
}
114