1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JoshGaber\NovaUnit\Filters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use JoshGaber\NovaUnit\Exceptions\InvalidModelException; |
8
|
|
|
use JoshGaber\NovaUnit\MockComponent; |
9
|
|
|
use Laravel\Nova\Filters\BooleanFilter; |
10
|
|
|
use Laravel\Nova\Filters\DateFilter; |
11
|
|
|
use Laravel\Nova\Filters\Filter; |
12
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
13
|
|
|
use PHPUnit\Framework\Constraint\ArrayHasKey; |
14
|
|
|
use PHPUnit\Framework\Constraint\IsInstanceOf; |
15
|
|
|
use PHPUnit\Framework\Constraint\TraversableContainsEqual; |
16
|
|
|
|
17
|
|
|
class MockFilter extends MockComponent |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Assert that the subject filter is a select filter. |
21
|
|
|
* |
22
|
|
|
* @param string $message |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
2 |
|
public function assertSelectFilter(string $message = ''): self |
26
|
|
|
{ |
27
|
2 |
|
PHPUnit::assertThat( |
28
|
2 |
|
$this->component, |
29
|
2 |
|
PHPUnit::logicalAnd( |
30
|
2 |
|
new isInstanceOf(Filter::class), |
31
|
2 |
|
PHPUnit::logicalNot(new IsInstanceOf(BooleanFilter::class)), |
32
|
2 |
|
PHPUnit::logicalNot(new IsInstanceOf(DateFilter::class)) |
33
|
|
|
), |
34
|
2 |
|
$message |
35
|
|
|
); |
36
|
|
|
|
37
|
1 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Assert that the subject filter is a boolean filter. |
42
|
|
|
* |
43
|
|
|
* @param string $message |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
2 |
|
public function assertBooleanFilter(string $message = ''): self |
47
|
|
|
{ |
48
|
2 |
|
PHPUnit::assertInstanceOf(BooleanFilter::class, $this->component, $message); |
49
|
|
|
|
50
|
1 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Assert that the subject filter is a date filter. |
55
|
|
|
* |
56
|
|
|
* @param string $message |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
2 |
|
public function assertDateFilter(string $message = ''): self |
60
|
|
|
{ |
61
|
2 |
|
PHPUnit::assertInstanceOf(DateFilter::class, $this->component, $message); |
62
|
|
|
|
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Assert that the filter has the given option. |
68
|
|
|
* |
69
|
|
|
* @param string $option The key or value of the option |
70
|
|
|
* @param string $message |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
3 |
|
public function assertHasOption(string $option, string $message = ''): self |
74
|
|
|
{ |
75
|
3 |
|
PHPUnit::assertThat( |
76
|
3 |
|
$this->component->options(Request::createFromGlobals()), |
77
|
3 |
|
PHPUnit::logicalOr( |
78
|
3 |
|
new ArrayHasKey($option), |
79
|
3 |
|
new TraversableContainsEqual($option) |
80
|
|
|
), |
81
|
3 |
|
$message |
82
|
|
|
); |
83
|
|
|
|
84
|
2 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Assert that the filter does not have the given option. |
89
|
|
|
* |
90
|
|
|
* @param string $option The key or value of the option |
91
|
|
|
* @param string $message |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
3 |
|
public function assertOptionMissing(string $option, string $message = ''): self |
95
|
|
|
{ |
96
|
3 |
|
PHPUnit::assertThat( |
97
|
3 |
|
$this->component->options(Request::createFromGlobals()), |
98
|
3 |
|
PHPUnit::logicalNot( |
99
|
3 |
|
PHPUnit::logicalOr( |
100
|
3 |
|
new ArrayHasKey($option), |
101
|
3 |
|
new TraversableContainsEqual($option) |
102
|
|
|
) |
103
|
|
|
), |
104
|
3 |
|
$message |
105
|
|
|
); |
106
|
|
|
|
107
|
1 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Apply the filter with the provided value and allow tests on the result. |
112
|
|
|
* |
113
|
|
|
* @param string $model The model that the filter should be applied to |
114
|
|
|
* @param mixed|array $value The value returned by the filter |
115
|
|
|
* @return MockFilterQuery |
116
|
|
|
* @throws InvalidModelException If the class provided is not a valid model class |
117
|
|
|
*/ |
118
|
7 |
|
public function apply(string $model, $value): MockFilterQuery |
119
|
|
|
{ |
120
|
7 |
|
if (! is_subclass_of($model, Model::class)) { |
121
|
1 |
|
throw new InvalidModelException(); |
122
|
|
|
} |
123
|
|
|
|
124
|
6 |
|
return new MockFilterQuery( |
125
|
6 |
|
$this->component->apply(Request::createFromGlobals(), $model::query(), $value) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|