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
|
30 |
|
public function field(string $fieldName): MockFieldElement |
84
|
|
|
{ |
85
|
30 |
|
$field = FieldHelper::findField( |
86
|
30 |
|
$this->component->fields(Request::createFromGlobals()), |
87
|
30 |
|
$fieldName, |
88
|
30 |
|
$this->allowPanels() |
89
|
|
|
); |
90
|
|
|
|
91
|
30 |
|
if (is_null($field)) { |
92
|
1 |
|
throw new FieldNotFoundException(); |
93
|
|
|
} |
94
|
|
|
|
95
|
29 |
|
return new MockFieldElement($field); |
96
|
|
|
} |
97
|
|
|
|
98
|
45 |
|
private function allowPanels(): bool |
99
|
|
|
{ |
100
|
45 |
|
return $this instanceof MockLens || $this instanceof MockResource; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|