/
Has.php
213 lines (180 loc) · 5.01 KB
/
Has.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Illuminate\Testing\Fluent\Concerns;
use Closure;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert as PHPUnit;
trait Has
{
/**
* Assert that the prop is of the expected size.
*
* @param string|int $key
* @param int|null $length
* @return $this
*/
public function count($key, int $length = null): self
{
if (is_null($length)) {
$path = $this->dotPath();
PHPUnit::assertCount(
$key,
$this->prop(),
$path
? sprintf('Property [%s] does not have the expected size.', $path)
: sprintf('Root level does not have the expected size.')
);
return $this;
}
PHPUnit::assertCount(
$length,
$this->prop($key),
sprintf('Property [%s] does not have the expected size.', $this->dotPath($key))
);
return $this;
}
/**
* Ensure that the given prop exists.
*
* @param string|int $key
* @param int|\Closure|null $length
* @param \Closure|null $callback
* @return $this
*/
public function has($key, $length = null, Closure $callback = null): self
{
$prop = $this->prop();
if (is_int($key) && is_null($length)) {
return $this->count($key);
}
PHPUnit::assertTrue(
Arr::has($prop, $key),
sprintf('Property [%s] does not exist.', $this->dotPath($key))
);
$this->interactsWith($key);
if (! is_null($callback)) {
return $this->has($key, function (self $scope) use ($length, $callback) {
return $scope
->tap(function (self $scope) use ($length) {
if (! is_null($length)) {
$scope->count($length);
}
})
->first($callback)
->etc();
});
}
if (is_callable($length)) {
return $this->scope($key, $length);
}
if (! is_null($length)) {
return $this->count($key, $length);
}
return $this;
}
/**
* Assert that all of the given props exist.
*
* @param array|string $key
* @return $this
*/
public function hasAll($key): self
{
$keys = is_array($key) ? $key : func_get_args();
foreach ($keys as $prop => $count) {
if (is_int($prop)) {
$this->has($count);
} else {
$this->has($prop, $count);
}
}
return $this;
}
/**
* Assert that at least one of the given props exists.
*
* @param array|string $key
* @return $this
*/
public function hasAny($key): self
{
$keys = is_array($key) ? $key : func_get_args();
PHPUnit::assertTrue(
Arr::hasAny($this->prop(), $keys),
sprintf('None of properties [%s] exist.', implode(', ', $keys))
);
foreach ($keys as $key) {
$this->interactsWith($key);
}
return $this;
}
/**
* Assert that none of the given props exist.
*
* @param array|string $key
* @return $this
*/
public function missingAll($key): self
{
$keys = is_array($key) ? $key : func_get_args();
foreach ($keys as $prop) {
$this->missing($prop);
}
return $this;
}
/**
* Assert that the given prop does not exist.
*
* @param string $key
* @return $this
*/
public function missing(string $key): self
{
PHPUnit::assertNotTrue(
Arr::has($this->prop(), $key),
sprintf('Property [%s] was found while it was expected to be missing.', $this->dotPath($key))
);
return $this;
}
/**
* Compose the absolute "dot" path to the given key.
*
* @param string $key
* @return string
*/
abstract protected function dotPath(string $key = ''): string;
/**
* Marks the property as interacted.
*
* @param string $key
* @return void
*/
abstract protected function interactsWith(string $key): void;
/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
abstract protected function prop(string $key = null);
/**
* Instantiate a new "scope" at the path of the given key.
*
* @param string $key
* @param \Closure $callback
* @return $this
*/
abstract protected function scope(string $key, Closure $callback);
/**
* Disables the interaction check.
*
* @return $this
*/
abstract public function etc();
/**
* Instantiate a new "scope" on the first element.
*
* @param \Closure $callback
* @return $this
*/
abstract public function first(Closure $callback);
}