|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JoshGaber\NovaUnit\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use JoshGaber\NovaUnit\Actions\ActionNotFoundException; |
|
7
|
|
|
use JoshGaber\NovaUnit\Actions\MockActionElement; |
|
8
|
|
|
use JoshGaber\NovaUnit\Constraints\ArrayHasInstanceOf; |
|
9
|
|
|
use Laravel\Nova\Actions\Action; |
|
10
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
11
|
|
|
use PHPUnit\Framework\Constraint\IsType; |
|
12
|
|
|
use PHPUnit\Framework\Constraint\TraversableContainsOnly; |
|
13
|
|
|
|
|
14
|
|
|
trait ActionAssertions |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Asserts that this component has the specified field. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $action The class path of the Action |
|
20
|
|
|
* @param string $message |
|
21
|
|
|
* @return $this |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function assertHasAction(string $action, string $message = ''): self |
|
24
|
|
|
{ |
|
25
|
2 |
|
PHPUnit::assertThat( |
|
26
|
2 |
|
$this->component->actions(Request::createFromGlobals()), |
|
27
|
2 |
|
new ArrayHasInstanceOf($action), |
|
28
|
2 |
|
$message |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
1 |
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Asserts that this component does not have the specified field. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $action The class path of the Action |
|
38
|
|
|
* @param string $message |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function assertActionMissing(string $action, string $message = ''): self |
|
42
|
|
|
{ |
|
43
|
2 |
|
PHPUnit::assertThat( |
|
44
|
2 |
|
$this->component->actions(Request::createFromGlobals()), |
|
45
|
2 |
|
PHPUnit::logicalNot(new ArrayHasInstanceOf($action)), |
|
46
|
2 |
|
$message |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
1 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Asserts that this component has no Actions specified. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $message |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function assertHasNoActions(string $message = ''): self |
|
59
|
|
|
{ |
|
60
|
2 |
|
PHPUnit::assertCount(0, $this->component->actions(Request::createFromGlobals()), $message); |
|
61
|
|
|
|
|
62
|
1 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Asserts that all actions on this component are valid Actions. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $message |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function assertHasValidActions(string $message = ''): self |
|
72
|
|
|
{ |
|
73
|
3 |
|
PHPUnit::assertThat( |
|
74
|
3 |
|
$this->component->actions(Request::createFromGlobals()), |
|
75
|
3 |
|
PHPUnit::logicalAnd( |
|
76
|
3 |
|
new IsType(IsType::TYPE_ARRAY), |
|
77
|
3 |
|
new TraversableContainsOnly(Action::class, false) |
|
78
|
|
|
), |
|
79
|
3 |
|
$message |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
1 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Searches for a matching action instance on this component for testing. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $actionType The class name of the Action |
|
89
|
|
|
* @return MockActionElement |
|
90
|
|
|
* @throws ActionNotFoundException |
|
91
|
|
|
*/ |
|
92
|
14 |
|
public function action(string $actionType): MockActionElement |
|
93
|
|
|
{ |
|
94
|
14 |
|
$actions = array_filter( |
|
95
|
14 |
|
$this->component->actions(Request::createFromGlobals()), |
|
96
|
|
|
function ($a) use ($actionType) { |
|
97
|
14 |
|
return $a instanceof $actionType; |
|
98
|
14 |
|
} |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
14 |
|
if (count($actions) === 0) { |
|
102
|
1 |
|
throw new ActionNotFoundException(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
13 |
|
return new MockActionElement(array_shift($actions)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|