MockFieldElement::assertShownWhenUpdating()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace JoshGaber\NovaUnit\Fields;
4
5
use Laravel\Nova\Fields\Field;
6
use PHPUnit\Framework\Assert as PHPUnit;
7
8
class MockFieldElement
9
{
10
    private $field;
11
12 37
    public function __construct(Field $field)
13
    {
14 37
        $this->field = $field;
15 37
    }
16
17
    /**
18
     * Assert that the following rule can be found on the field.
19
     *
20
     * @param string $rule The rule to match this field against
21
     * @param string $message
22
     * @return $this
23
     */
24 2
    public function assertHasRule(string $rule, string $message = ''): self
25
    {
26 2
        PHPUnit::assertContains($rule, $this->field->rules, $message);
27
28 1
        return $this;
29
    }
30
31
    /**
32
     * Assert that the following rule cannot be found on the field.
33
     *
34
     * @param string $rule The rule to match this field against
35
     * @param string $message
36
     * @return $this
37
     */
38 2
    public function assertRuleMissing(string $rule, string $message = ''): self
39
    {
40 2
        PHPUnit::assertNotContains($rule, $this->field->rules, $message);
41
42 1
        return $this;
43
    }
44
45
    /**
46
     * Assert that the following rule can be found on the field when a
47
     * resource being created.
48
     *
49
     * @param string $rule The rule to match this field against
50
     * @param string $message
51
     * @return $this
52
     */
53 2
    public function assertHasCreationRule(string $rule, string $message = ''): self
54
    {
55 2
        PHPUnit::assertContains($rule, $this->field->creationRules, $message);
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * Assert that the following rule cannot be found on the field when a
62
     * resource being created.
63
     *
64
     * @param string $rule The rule to match this field against
65
     * @param string $message
66
     * @return $this
67
     */
68 2
    public function assertCreationRuleMissing(string $rule, string $message = ''): self
69
    {
70 2
        PHPUnit::assertNotContains($rule, $this->field->creationRules, $message);
71
72 1
        return $this;
73
    }
74
75
    /**
76
     * Assert that the following rule can be found on the field when a
77
     * resource being updated.
78
     *
79
     * @param string $rule The rule to match this field against
80
     * @param string $message
81
     * @return $this
82
     */
83 2
    public function assertHasUpdateRule(string $rule, string $message = ''): self
84
    {
85 2
        PHPUnit::assertContains($rule, $this->field->updateRules, $message);
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * Assert that the following rule cannot be found on the field when a
92
     * resource being created.
93
     *
94
     * @param string $rule The rule to match this field against
95
     * @param string $message
96
     * @return $this
97
     */
98 2
    public function assertUpdateRuleMissing(string $rule, string $message = ''): self
99
    {
100 2
        PHPUnit::assertNotContains($rule, $this->field->updateRules, $message);
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * Assert that the field can be shown on the index view.
107
     *
108
     * @param string $message
109
     * @return $this
110
     */
111 2
    public function assertShownOnIndex(string $message = ''): self
112
    {
113 2
        $test = $this->field->showOnIndex;
114 2
        PHPUnit::assertTrue(
115 2
            is_callable($test) ? $test() : $test,
116 2
            $message
117
        );
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * Assert that the field is hidden from the index view.
124
     *
125
     * @param string $message
126
     * @return $this
127
     */
128 2
    public function assertHiddenFromIndex(string $message = ''): self
129
    {
130 2
        $test = $this->field->showOnIndex;
131 2
        PHPUnit::assertFalse(
132 2
            is_callable($test) ? $test() : $test,
133 2
            $message
134
        );
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * Assert that the field can be shown on the detail view.
141
     *
142
     * @param string $message
143
     * @return $this
144
     */
145 2
    public function assertShownOnDetail(string $message = ''): self
146
    {
147 2
        $test = $this->field->showOnDetail;
148 2
        PHPUnit::assertTrue(
149 2
            is_callable($test) ? $test() : $test,
150 2
            $message
151
        );
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * Assert that the field is hidden from the detail view.
158
     *
159
     * @param string $message
160
     * @return $this
161
     */
162 2
    public function assertHiddenFromDetail(string $message = ''): self
163
    {
164 2
        $test = $this->field->showOnDetail;
165 2
        PHPUnit::assertFalse(
166 2
            is_callable($test) ? $test() : $test,
167 2
            $message
168
        );
169
170 1
        return $this;
171
    }
172
173
    /**
174
     * Assert that the field can be shown when creating a new record.
175
     *
176
     * @param string $message
177
     * @return $this
178
     */
179 2
    public function assertShownWhenCreating(string $message = ''): self
180
    {
181 2
        $test = $this->field->showOnCreation;
182 2
        PHPUnit::assertTrue(
183 2
            is_callable($test) ? $test() : $test,
184 2
            $message
185
        );
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * Assert that the field is hidden when creating a new record.
192
     *
193
     * @param string $message
194
     * @return $this
195
     */
196 2
    public function assertHiddenWhenCreating(string $message = ''): self
197
    {
198 2
        $test = $this->field->showOnCreation;
199 2
        PHPUnit::assertFalse(
200 2
            is_callable($test) ? $test() : $test,
201 2
            $message
202
        );
203
204 1
        return $this;
205
    }
206
207
    /**
208
     * Assert that the field can be shown when updating a record.
209
     *
210
     * @param string $message
211
     * @return $this
212
     */
213 2
    public function assertShownWhenUpdating(string $message = ''): self
214
    {
215 2
        $test = $this->field->showOnUpdate;
216 2
        PHPUnit::assertTrue(
217 2
            is_callable($test) ? $test() : $test,
218 2
            $message
219
        );
220
221 1
        return $this;
222
    }
223
224
    /**
225
     * Assert that the field is hidden when updating a record.
226
     *
227
     * @param string $message
228
     * @return $this
229
     */
230 2
    public function assertHiddenWhenUpdating(string $message = ''): self
231
    {
232 2
        $test = $this->field->showOnUpdate;
233 2
        PHPUnit::assertFalse(
234 2
            is_callable($test) ? $test() : $test,
235 2
            $message
236
        );
237
238 1
        return $this;
239
    }
240
241
    /**
242
     * Assert that the field should be set to null if the value is empty.
243
     *
244
     * @param string $message
245
     * @return $this
246
     */
247 2
    public function assertNullable(string $message = ''): self
248
    {
249 2
        PHPUnit::assertTrue($this->field->nullable, $message);
250
251 1
        return $this;
252
    }
253
254
    /**
255
     * Assert that the field should not be set to null if the value is empty.
256
     *
257
     * @param string $message
258
     * @return $this
259
     */
260 2
    public function assertNotNullable(string $message = ''): self
261
    {
262 2
        PHPUnit::assertFalse($this->field->nullable, $message);
263
264 1
        return $this;
265
    }
266
267
    /**
268
     * Assert that records can be sorted by this field.
269
     *
270
     * @param string $message
271
     * @return $this
272
     */
273 2
    public function assertSortable(string $message = ''): self
274
    {
275 2
        PHPUnit::assertTrue($this->field->sortable, $message);
276
277 1
        return $this;
278
    }
279
280
    /**
281
     * Assert that records cannot be sorted by this field.
282
     *
283
     * @param string $message
284
     * @return $this
285
     */
286 2
    public function assertNotSortable(string $message = ''): self
287
    {
288 2
        PHPUnit::assertFalse($this->field->sortable, $message);
289
290 1
        return $this;
291
    }
292
}
293