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