1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JoshGaber\NovaUnit\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
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 Laravel\Nova\Http\Requests\NovaRequest; |
14
|
|
|
use Laravel\Nova\Http\Requests\ResourceCreateOrAttachRequest; |
15
|
|
|
use Laravel\Nova\Http\Requests\ResourceDetailRequest; |
16
|
|
|
use Laravel\Nova\Http\Requests\ResourceIndexRequest; |
17
|
|
|
use Laravel\Nova\Http\Requests\ResourceUpdateOrUpdateAttachedRequest; |
18
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
19
|
|
|
use PHPUnit\Framework\Constraint\IsType; |
20
|
|
|
|
21
|
|
|
trait FieldAssertions |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Checks that this Nova component has a field with the same name or attribute. |
25
|
|
|
* |
26
|
|
|
* @param string $field The name or attribute of the field |
27
|
|
|
* @param string $message |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
6 |
|
public function assertHasField(string $field, string $message = ''): self |
31
|
|
|
{ |
32
|
6 |
|
return $this->assertHasFieldIn('fields', NovaRequest::class, $field, $message); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Checks that this Nova component has an index field with the same name or attribute. |
37
|
|
|
* |
38
|
|
|
* @param string $field The name or attribute of the field |
39
|
|
|
* @param string $message |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function assertHasIndexField(string $field, string $message = ''): self |
43
|
|
|
{ |
44
|
|
|
$this->ensureIsResource(); |
45
|
|
|
|
46
|
|
|
return $this->assertHasFieldIn('fieldsForIndex', ResourceIndexRequest::class, $field, $message); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Checks that this Nova component has a detail field with the same name or attribute. |
51
|
|
|
* |
52
|
|
|
* @param string $field The name or attribute of the field |
53
|
|
|
* @param string $message |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function assertHasDetailField(string $field, string $message = ''): self |
57
|
|
|
{ |
58
|
|
|
$this->ensureIsResource(); |
59
|
|
|
|
60
|
|
|
return $this->assertHasFieldIn('fieldsForDetail', ResourceDetailRequest::class, $field, $message); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks that this Nova component has a detail field with the same name or attribute. |
65
|
|
|
* |
66
|
|
|
* @param string $field The name or attribute of the field |
67
|
|
|
* @param string $message |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function assertHasCreationField(string $field, string $message = ''): self |
71
|
|
|
{ |
72
|
|
|
$this->ensureIsResource(); |
73
|
|
|
|
74
|
|
|
return $this->assertHasFieldIn('fieldsForUpdate', ResourceCreateOrAttachRequest::class, $field, $message); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Checks that this Nova component has a detail field with the same name or attribute. |
79
|
|
|
* |
80
|
|
|
* @param string $field The name or attribute of the field |
81
|
|
|
* @param string $message |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function assertHasUpdateField(string $field, string $message = ''): self |
85
|
|
|
{ |
86
|
|
|
$this->ensureIsResource(); |
87
|
|
|
|
88
|
|
|
return $this->assertHasFieldIn('fieldsForCreate', ResourceUpdateOrUpdateAttachedRequest::class, $field, $message); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks that no field on this Nova component has a name or attribute matching |
93
|
|
|
* the given parameter. |
94
|
|
|
* |
95
|
|
|
* @param string $field |
96
|
|
|
* @param string $message |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
4 |
|
public function assertFieldMissing(string $field, string $message = ''): self |
100
|
|
|
{ |
101
|
4 |
|
PHPUnit::assertThat( |
102
|
4 |
|
$this->component->fields(NovaRequest::createFromGlobals()), |
103
|
4 |
|
PHPUnit::logicalNot(new HasField($field, $this->allowPanels())), |
104
|
4 |
|
$message |
105
|
4 |
|
); |
106
|
|
|
|
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Asserts that this Nova Action has no fields defined. |
112
|
|
|
* |
113
|
|
|
* @param string $message |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
2 |
|
public function assertHasNoFields(string $message = ''): self |
117
|
|
|
{ |
118
|
2 |
|
PHPUnit::assertCount(0, $this->component->fields(NovaRequest::createFromGlobals()), $message); |
119
|
|
|
|
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Asserts that all fields defined on this Nova Action are valid fields. |
125
|
|
|
* |
126
|
|
|
* @param string $message |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
6 |
|
public function assertHasValidFields(string $message = ''): self |
130
|
|
|
{ |
131
|
6 |
|
PHPUnit::assertThat( |
132
|
6 |
|
$this->component->fields(NovaRequest::createFromGlobals()), |
133
|
6 |
|
PHPUnit::logicalAnd( |
134
|
6 |
|
new IsType(IsType::TYPE_ARRAY), |
135
|
6 |
|
new HasValidFields($this->allowPanels()) |
136
|
6 |
|
), |
137
|
6 |
|
$message |
138
|
6 |
|
); |
139
|
|
|
|
140
|
4 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Searches for a matching field instance on this component for testing. |
145
|
|
|
* |
146
|
|
|
* @param string $fieldName The name or attribute of the field to return |
147
|
|
|
* @return MockFieldElement |
148
|
|
|
* |
149
|
|
|
* @throws FieldNotFoundException |
150
|
|
|
*/ |
151
|
38 |
|
public function field(string $fieldName): MockFieldElement |
152
|
|
|
{ |
153
|
38 |
|
$field = FieldHelper::findField( |
154
|
38 |
|
$this->component->fields(NovaRequest::createFromGlobals()), |
155
|
38 |
|
$fieldName, |
156
|
38 |
|
$this->allowPanels() |
157
|
38 |
|
); |
158
|
|
|
|
159
|
38 |
|
if (is_null($field)) { |
160
|
1 |
|
throw new FieldNotFoundException(); |
161
|
|
|
} |
162
|
|
|
|
163
|
37 |
|
return new MockFieldElement($field); |
164
|
|
|
} |
165
|
|
|
|
166
|
54 |
|
private function allowPanels(): bool |
167
|
|
|
{ |
168
|
54 |
|
return $this instanceof MockLens || $this instanceof MockResource; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function ensureIsResource() |
172
|
|
|
{ |
173
|
|
|
if ($this instanceOf MockResource) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
throw new Exception('Method only available for nova resources.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
6 |
|
private function assertHasFieldIn(string $fieldsMethod, string $request, string $field, string $message = '') |
181
|
|
|
{ |
182
|
6 |
|
if (!method_exists($this->component, $fieldsMethod)) { |
183
|
|
|
$fieldsMethod = 'fields'; |
184
|
|
|
} |
185
|
|
|
|
186
|
6 |
|
PHPUnit::assertThat( |
187
|
6 |
|
$this->component->{$fieldsMethod}($request::createFromGlobals()), |
188
|
6 |
|
new HasField($field, $this->allowPanels()), |
189
|
6 |
|
$message |
190
|
6 |
|
); |
191
|
|
|
|
192
|
4 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|