1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JoshGaber\NovaUnit\Lenses; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use JoshGaber\NovaUnit\Constraints\EloquentCollectionContains; |
7
|
|
|
use JoshGaber\NovaUnit\Exceptions\InvalidModelException; |
8
|
|
|
use JoshGaber\NovaUnit\MockComponent; |
9
|
|
|
use JoshGaber\NovaUnit\Traits\ActionAssertions; |
10
|
|
|
use JoshGaber\NovaUnit\Traits\FieldAssertions; |
11
|
|
|
use JoshGaber\NovaUnit\Traits\FilterAssertions; |
12
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
13
|
|
|
|
14
|
|
|
class MockLens extends MockComponent |
15
|
|
|
{ |
16
|
|
|
use ActionAssertions, FieldAssertions, FilterAssertions; |
17
|
|
|
|
18
|
|
|
public $model; |
19
|
|
|
|
20
|
23 |
|
public function __construct($component, $model = null) |
21
|
|
|
{ |
22
|
23 |
|
parent::__construct($component); |
23
|
23 |
|
$this->model = $model; |
24
|
23 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Assert that the query builder will return the given model. |
28
|
|
|
* |
29
|
|
|
* @deprecated |
30
|
|
|
* |
31
|
|
|
* @param Model $element The model contained in the query result |
32
|
|
|
* @param string $message |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
2 |
|
public function assertQueryContains(Model $element, string $message = ''): self |
36
|
|
|
{ |
37
|
2 |
|
PHPUnit::assertThat( |
38
|
2 |
|
$this->component::query(new MockLensRequest(), $this->model::query())->get(), |
39
|
2 |
|
new EloquentCollectionContains($element), |
40
|
2 |
|
$message |
41
|
|
|
); |
42
|
|
|
|
43
|
1 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Assert that the query builder will not return the given model. |
48
|
|
|
* |
49
|
|
|
* @deprecated |
50
|
|
|
* |
51
|
|
|
* @param Model $element The model not contained in the query result |
52
|
|
|
* @param string $message |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
2 |
|
public function assertQueryMissing(Model $element, string $message = ''): self |
56
|
|
|
{ |
57
|
2 |
|
PHPUnit::assertThat( |
58
|
2 |
|
$this->component::query(new MockLensRequest(), $this->model::query())->get(), |
59
|
2 |
|
PHPUnit::logicalNot(new EloquentCollectionContains($element)), |
60
|
2 |
|
$message |
61
|
|
|
); |
62
|
|
|
|
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Assert that the query builder uses the "withFilters" request. |
68
|
|
|
* |
69
|
|
|
* @deprecated |
70
|
|
|
* |
71
|
|
|
* @param string $message |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
2 |
|
public function assertQueryWithFilters(string $message = ''): self |
75
|
|
|
{ |
76
|
2 |
|
$request = new MockLensRequest(); |
77
|
2 |
|
$this->component::query($request, $this->model::query()); |
78
|
2 |
|
PHPUnit::assertTrue($request->withFilters, $message); |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Assert that the query builder uses the "withOrdering" request. |
85
|
|
|
* |
86
|
|
|
* @deprecated |
87
|
|
|
* |
88
|
|
|
* @param string $message |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
2 |
|
public function assertQueryWithOrdering(string $message = ''): self |
92
|
|
|
{ |
93
|
2 |
|
$request = new MockLensRequest(); |
94
|
2 |
|
$this->component::query($request, $this->model::query()); |
95
|
2 |
|
PHPUnit::assertTrue($request->withOrdering, $message); |
96
|
|
|
|
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Apply lens query and test the response. |
102
|
|
|
* |
103
|
|
|
* @param string|null $model |
104
|
|
|
* @return MockLensQuery |
105
|
|
|
* @throws InvalidModelException |
106
|
|
|
*/ |
107
|
12 |
|
public function query(?string $model = null): MockLensQuery |
108
|
|
|
{ |
109
|
12 |
|
if (! is_subclass_of($model, Model::class) && ! is_subclass_of($this->model, Model::class)) { |
110
|
1 |
|
throw new InvalidModelException(); |
111
|
|
|
} |
112
|
|
|
|
113
|
11 |
|
$query = ($model ?? $this->model)::query(); |
114
|
11 |
|
$request = new MockLensRequest(); |
115
|
|
|
|
116
|
11 |
|
return new MockLensQuery( |
117
|
11 |
|
$this->component::query($request, $query), |
118
|
11 |
|
$request |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|