Passed
Push — master ( 45207e...ca5789 )
by Josh
14:32
created

FieldAssertions::assertHasNoFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
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\Lenses\MockLens;
9
use JoshGaber\NovaUnit\Resources\MockResource;
10
use PHPUnit\Framework\Assert as PHPUnit;
11
12
trait FieldAssertions
13
{
14
    /**
15
     * Checks that this Nova component has a field with the same name or attribute.
16
     *
17
     * @param string $field The name or attribute of the field
18
     * @param string $message
19
     * @return $this
20
     */
21 6
    public function assertHasField(string $field, string $message = ''): self
22
    {
23 6
        PHPUnit::assertThat(
24 6
            $this->component->fields(Request::createFromGlobals()),
25 6
            new HasField($field, $this->allowPanels()),
26 6
            $message
27
        );
28
29 4
        return $this;
30
    }
31
32
    /**
33
     * Checks that no field on this Nova component has a name or attribute matching
34
     * the given parameter.
35
     *
36
     * @param string $field
37
     * @param string $message
38
     * @return $this
39
     */
40 4
    public function assertFieldMissing(string $field, string $message = ''): self
41
    {
42 4
        PHPUnit::assertThat(
43 4
            $this->component->fields(Request::createFromGlobals()),
44 4
            PHPUnit::logicalNot(new HasField($field, $this->allowPanels())),
45 4
            $message
46
        );
47
48 2
        return $this;
49
    }
50
51
    /**
52
     * Asserts that this Nova Action has no fields defined.
53
     * @param string $message
54
     * @return $this
55
     */
56 2
    public function assertHasNoFields(string $message = ''): self
57
    {
58 2
        PHPUnit::assertCount(0, $this->component->fields(Request::createFromGlobals()), $message);
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Asserts that all fields defined on this Nova Action are valid fields.
65
     *
66
     * @param string $message
67
     * @return $this
68
     */
69 5
    public function assertHasValidFields(string $message = ''): self
70
    {
71 5
        PHPUnit::assertThat(
72 5
            $this->component->fields(Request::createFromGlobals()),
73 5
            new HasValidFields($this->allowPanels()),
74 5
            $message
75
        );
76
77 3
        return $this;
78
    }
79
80 15
    private function allowPanels(): bool
81
    {
82 15
        return $this instanceof MockLens || $this instanceof MockResource;
83
    }
84
}
85