Passed
Pull Request — release-1.1 (#21)
by Josh
04:04
created

FieldAssertions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 93
ccs 31
cts 31
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assertFieldMissing() 0 9 1
A assertHasNoFields() 0 5 1
A assertHasValidFields() 0 9 1
A assertHasField() 0 9 1
A allowPanels() 0 3 2
A field() 0 13 2
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
15
trait FieldAssertions
16
{
17
    /**
18
     * Checks that this Nova component has a field with the same name or attribute.
19
     *
20
     * @param string $field The name or attribute of the field
21
     * @param string $message
22
     * @return $this
23
     */
24 6
    public function assertHasField(string $field, string $message = ''): self
25
    {
26 6
        PHPUnit::assertThat(
27 6
            $this->component->fields(Request::createFromGlobals()),
28 6
            new HasField($field, $this->allowPanels()),
29 6
            $message
30
        );
31
32 4
        return $this;
33
    }
34
35
    /**
36
     * Checks that no field on this Nova component has a name or attribute matching
37
     * the given parameter.
38
     *
39
     * @param string $field
40
     * @param string $message
41
     * @return $this
42
     */
43 4
    public function assertFieldMissing(string $field, string $message = ''): self
44
    {
45 4
        PHPUnit::assertThat(
46 4
            $this->component->fields(Request::createFromGlobals()),
47 4
            PHPUnit::logicalNot(new HasField($field, $this->allowPanels())),
48 4
            $message
49
        );
50
51 2
        return $this;
52
    }
53
54
    /**
55
     * Asserts that this Nova Action has no fields defined.
56
     * @param string $message
57
     * @return $this
58
     */
59 2
    public function assertHasNoFields(string $message = ''): self
60
    {
61 2
        PHPUnit::assertCount(0, $this->component->fields(Request::createFromGlobals()), $message);
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * Asserts that all fields defined on this Nova Action are valid fields.
68
     *
69
     * @param string $message
70
     * @return $this
71
     */
72 5
    public function assertHasValidFields(string $message = ''): self
73
    {
74 5
        PHPUnit::assertThat(
75 5
            $this->component->fields(Request::createFromGlobals()),
76 5
            new HasValidFields($this->allowPanels()),
77 5
            $message
78
        );
79
80 3
        return $this;
81
    }
82
83
    /**
84
     * Searches for a matching field instance on this component for testing.
85
     *
86
     * @param string $fieldName The name or attribute of the field to return
87
     * @return MockFieldElement
88
     * @throws FieldNotFoundException
89
     */
90 30
    public function field(string $fieldName): MockFieldElement
91
    {
92 30
        $field = FieldHelper::findField(
93 30
            $this->component->fields(Request::createFromGlobals()),
94 30
            $fieldName,
95 30
            $this->allowPanels()
96
        );
97
98 30
        if (is_null($field)) {
99 1
            throw new FieldNotFoundException();
100
        }
101
102 29
        return new MockFieldElement($field);
103
    }
104
105 45
    private function allowPanels(): bool
106
    {
107 45
        return $this instanceof MockLens || $this instanceof MockResource;
108
    }
109
}
110