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\MockComponent; |
8
|
|
|
use JoshGaber\NovaUnit\Traits\ActionAssertions; |
9
|
|
|
use JoshGaber\NovaUnit\Traits\FieldAssertions; |
10
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
11
|
|
|
|
12
|
|
|
class MockLens extends MockComponent |
13
|
|
|
{ |
14
|
|
|
use ActionAssertions, FieldAssertions; |
15
|
|
|
|
16
|
|
|
public $model; |
17
|
|
|
|
18
|
9 |
|
public function __construct($component, $model) |
19
|
|
|
{ |
20
|
9 |
|
parent::__construct($component); |
21
|
9 |
|
$this->model = $model; |
22
|
9 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Assert that the query builder will return the given model. |
26
|
|
|
* |
27
|
|
|
* @param Model $element The model contained in the query result |
28
|
|
|
* @param string $message |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
2 |
|
public function assertQueryContains(Model $element, string $message = ''): self |
32
|
|
|
{ |
33
|
2 |
|
PHPUnit::assertThat( |
34
|
2 |
|
$this->component::query(new MockLensRequest(), $this->model::query())->get(), |
35
|
2 |
|
new EloquentCollectionContains($element), |
36
|
2 |
|
$message |
37
|
|
|
); |
38
|
|
|
|
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Assert that the query builder will not return the given model. |
44
|
|
|
* |
45
|
|
|
* @param Model $element The model not contained in the query result |
46
|
|
|
* @param string $message |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
2 |
|
public function assertQueryMissing(Model $element, string $message = ''): self |
50
|
|
|
{ |
51
|
2 |
|
PHPUnit::assertThat( |
52
|
2 |
|
$this->component::query(new MockLensRequest(), $this->model::query())->get(), |
53
|
2 |
|
PHPUnit::logicalNot(new EloquentCollectionContains($element)), |
54
|
2 |
|
$message |
55
|
|
|
); |
56
|
|
|
|
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Assert that the query builder uses the "withFilters" request. |
62
|
|
|
* |
63
|
|
|
* @param string $message |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
2 |
|
public function assertQueryWithFilters(string $message = ''): self |
67
|
|
|
{ |
68
|
2 |
|
$request = new MockLensRequest(); |
69
|
2 |
|
$this->component::query($request, $this->model::query()); |
70
|
2 |
|
PHPUnit::assertTrue($request->withFilters, $message); |
71
|
|
|
|
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Assert that the query builder uses the "withOrdering" request. |
77
|
|
|
* |
78
|
|
|
* @param string $message |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
2 |
|
public function assertQueryWithOrdering(string $message = '') |
82
|
|
|
{ |
83
|
2 |
|
$request = new MockLensRequest(); |
84
|
2 |
|
$this->component::query($request, $this->model::query()); |
85
|
2 |
|
PHPUnit::assertTrue($request->withOrdering, $message); |
86
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|