1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Expectation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class CommonExpectations |
7
|
|
|
* @package Dgame\Expectation |
8
|
|
|
*/ |
9
|
|
|
final class CommonExpectations |
10
|
|
|
{ |
11
|
|
|
use ConditionTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* CommonExpectations constructor. |
15
|
|
|
* |
16
|
|
|
* @param mixed $value |
17
|
|
|
*/ |
18
|
37 |
|
public function __construct($value) |
19
|
|
|
{ |
20
|
37 |
|
$this->value = $value; |
21
|
37 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param callable $callback |
25
|
|
|
* |
26
|
|
|
* @return CommonExpectations |
27
|
|
|
*/ |
28
|
3 |
|
public function is(callable $callback): self |
29
|
|
|
{ |
30
|
3 |
|
return $this->approveIf($callback($this->value)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param callable $callback |
35
|
|
|
* |
36
|
|
|
* @return CommonExpectations |
37
|
|
|
*/ |
38
|
|
|
public function isNot(callable $callback): self |
39
|
|
|
{ |
40
|
|
|
return $this->approveIf(!$callback($this->value)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return CommonExpectations |
45
|
|
|
*/ |
46
|
1 |
|
public function isEmpty(): self |
47
|
|
|
{ |
48
|
1 |
|
return $this->approveIf(empty($this->value)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return CommonExpectations |
53
|
|
|
*/ |
54
|
1 |
|
public function isNotEmpty(): self |
55
|
|
|
{ |
56
|
1 |
|
return $this->approveIf(!empty($this->value)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return CommonExpectations |
|
|
|
|
61
|
|
|
*/ |
62
|
1 |
|
public function isNull(): self |
63
|
|
|
{ |
64
|
1 |
|
return $this->isIdenticalTo(null); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return CommonExpectations |
|
|
|
|
69
|
|
|
*/ |
70
|
1 |
|
public function isNotNull(): self |
71
|
|
|
{ |
72
|
1 |
|
return $this->isNotIdenticalTo(null); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $value |
77
|
|
|
* |
78
|
|
|
* @return CommonExpectations |
79
|
|
|
*/ |
80
|
2 |
|
public function isEqual($value): self |
81
|
|
|
{ |
82
|
2 |
|
return $this->approveIf($this->value == $value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param mixed $value |
87
|
|
|
* |
88
|
|
|
* @return CommonExpectations |
89
|
|
|
*/ |
90
|
1 |
|
public function isNotEqual($value): self |
91
|
|
|
{ |
92
|
1 |
|
return $this->approveIf($this->value != $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $value |
97
|
|
|
* |
98
|
|
|
* @return CommonExpectations |
99
|
|
|
*/ |
100
|
2 |
|
public function isIdenticalTo($value): self |
101
|
|
|
{ |
102
|
2 |
|
return $this->approveIf($this->value === $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $value |
107
|
|
|
* |
108
|
|
|
* @return CommonExpectations |
109
|
|
|
*/ |
110
|
2 |
|
public function isNotIdenticalTo($value): self |
111
|
|
|
{ |
112
|
2 |
|
return $this->approveIf($this->value !== $value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return IntegerExpectations |
117
|
|
|
*/ |
118
|
2 |
|
public function isInt(): IntegerExpectations |
119
|
|
|
{ |
120
|
2 |
|
return new IntegerExpectations($this->value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return FloatExpectations |
125
|
|
|
*/ |
126
|
1 |
|
public function isFloat(): FloatExpectations |
127
|
|
|
{ |
128
|
1 |
|
return new FloatExpectations($this->value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return ScalarExpectations |
133
|
|
|
*/ |
134
|
1 |
|
public function isScalar(): ScalarExpectations |
135
|
|
|
{ |
136
|
1 |
|
return new ScalarExpectations($this->value); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return NumericExpectations |
141
|
|
|
*/ |
142
|
1 |
|
public function isNumeric(): NumericExpectations |
143
|
|
|
{ |
144
|
1 |
|
return new NumericExpectations($this->value); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return StringExpectations |
149
|
|
|
*/ |
150
|
1 |
|
public function isString(): StringExpectations |
151
|
|
|
{ |
152
|
1 |
|
return new StringExpectations($this->value); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return BooleanExpectations |
157
|
|
|
*/ |
158
|
1 |
|
public function isBool(): BooleanExpectations |
159
|
|
|
{ |
160
|
1 |
|
return new BooleanExpectations($this->value); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return ArrayExpectations |
165
|
|
|
*/ |
166
|
1 |
|
public function isArray(): ArrayExpectations |
167
|
|
|
{ |
168
|
1 |
|
return new ArrayExpectations($this->value); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return ObjectExpectations |
173
|
|
|
*/ |
174
|
1 |
|
public function isObject(): ObjectExpectations |
175
|
|
|
{ |
176
|
1 |
|
return new ObjectExpectations($this->value); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param mixed $object |
181
|
|
|
* |
182
|
|
|
* @return ObjectExpectations |
183
|
|
|
*/ |
184
|
1 |
|
public function isInstanceOf($object): ObjectExpectations |
185
|
|
|
{ |
186
|
1 |
|
$expectation = new ObjectExpectations($this->value); |
187
|
|
|
|
188
|
1 |
|
return $expectation->isInstanceOf($object); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return CommonExpectations |
|
|
|
|
193
|
|
|
*/ |
194
|
1 |
|
public function isCallable(): self |
195
|
|
|
{ |
196
|
1 |
|
return $this->is('is_callable'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return CommonExpectations |
|
|
|
|
201
|
|
|
*/ |
202
|
1 |
|
public function isDir(): self |
203
|
|
|
{ |
204
|
1 |
|
return $this->is('is_dir'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return CommonExpectations |
|
|
|
|
209
|
|
|
*/ |
210
|
1 |
|
public function isFile(): self |
211
|
|
|
{ |
212
|
1 |
|
return $this->is('is_file'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param float $lhs |
217
|
|
|
* @param float $rhs |
218
|
|
|
* |
219
|
|
|
* @return NumericExpectations |
220
|
|
|
*/ |
221
|
1 |
|
public function isBetween(float $lhs, float $rhs): NumericExpectations |
222
|
|
|
{ |
223
|
1 |
|
$expectation = new NumericExpectations($this->value); |
224
|
|
|
|
225
|
1 |
|
return $expectation->isBetween($lhs, $rhs); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param float $value |
230
|
|
|
* |
231
|
|
|
* @return NumericExpectations |
232
|
|
|
*/ |
233
|
1 |
|
public function isBelow(float $value): NumericExpectations |
234
|
|
|
{ |
235
|
1 |
|
$expectation = new NumericExpectations($this->value); |
236
|
|
|
|
237
|
1 |
|
return $expectation->isBelow($value); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param float $value |
242
|
|
|
* |
243
|
|
|
* @return NumericExpectations |
244
|
|
|
*/ |
245
|
1 |
|
public function isAbove(float $value): NumericExpectations |
246
|
|
|
{ |
247
|
1 |
|
$expectation = new NumericExpectations($this->value); |
248
|
|
|
|
249
|
1 |
|
return $expectation->isAbove($value); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param float $value |
254
|
|
|
* |
255
|
|
|
* @return NumericExpectations |
256
|
|
|
*/ |
257
|
1 |
|
public function isBelowOrEqual(float $value): NumericExpectations |
258
|
|
|
{ |
259
|
1 |
|
$expectation = new NumericExpectations($this->value); |
260
|
|
|
|
261
|
1 |
|
return $expectation->isBelowOrEqual($value); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param float $value |
266
|
|
|
* |
267
|
|
|
* @return NumericExpectations |
268
|
|
|
*/ |
269
|
1 |
|
public function isAboveOrEqual(float $value): NumericExpectations |
270
|
|
|
{ |
271
|
1 |
|
$expectation = new NumericExpectations($this->value); |
272
|
|
|
|
273
|
1 |
|
return $expectation->isAboveOrEqual($value); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return NumericExpectations |
278
|
|
|
*/ |
279
|
1 |
|
public function isPositive(): NumericExpectations |
280
|
|
|
{ |
281
|
1 |
|
$expectation = new NumericExpectations($this->value); |
282
|
|
|
|
283
|
1 |
|
return $expectation->isPositive(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return NumericExpectations |
288
|
|
|
*/ |
289
|
1 |
|
public function isNegative(): NumericExpectations |
290
|
|
|
{ |
291
|
1 |
|
$expectation = new NumericExpectations($this->value); |
292
|
|
|
|
293
|
1 |
|
return $expectation->isNegative(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return IntegerExpectations |
298
|
|
|
*/ |
299
|
2 |
|
public function isEven(): IntegerExpectations |
300
|
|
|
{ |
301
|
2 |
|
$expectation = new IntegerExpectations($this->value); |
302
|
|
|
|
303
|
2 |
|
return $expectation->isEven(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return IntegerExpectations |
308
|
|
|
*/ |
309
|
2 |
|
public function isOdd(): IntegerExpectations |
310
|
|
|
{ |
311
|
2 |
|
$expectation = new IntegerExpectations($this->value); |
312
|
|
|
|
313
|
2 |
|
return $expectation->isOdd(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return BooleanExpectations |
318
|
|
|
*/ |
319
|
1 |
|
public function isTrue(): BooleanExpectations |
320
|
|
|
{ |
321
|
1 |
|
$expectation = new BooleanExpectations($this->value); |
322
|
|
|
|
323
|
1 |
|
return $expectation->isTrue(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return BooleanExpectations |
328
|
|
|
*/ |
329
|
1 |
|
public function isFalse(): BooleanExpectations |
330
|
|
|
{ |
331
|
1 |
|
$expectation = new BooleanExpectations($this->value); |
332
|
|
|
|
333
|
1 |
|
return $expectation->isFalse(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param string $pattern |
338
|
|
|
* |
339
|
|
|
* @return StringExpectations |
340
|
|
|
*/ |
341
|
1 |
|
public function match(string $pattern): StringExpectations |
342
|
|
|
{ |
343
|
1 |
|
$expectation = new StringExpectations($this->value); |
344
|
|
|
|
345
|
1 |
|
return $expectation->match($pattern); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param array $values |
350
|
|
|
* |
351
|
|
|
* @return ArrayValueExpectations |
352
|
|
|
*/ |
353
|
1 |
|
public function isIn(array $values): ArrayValueExpectations |
354
|
|
|
{ |
355
|
1 |
|
$expecation = new ArrayValueExpectations($this->value); |
356
|
|
|
|
357
|
1 |
|
return $expecation->isIn($values); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param array $values |
362
|
|
|
* |
363
|
|
|
* @return ArrayValueExpectations |
364
|
|
|
*/ |
365
|
1 |
|
public function isKeyOf(array $values): ArrayValueExpectations |
366
|
|
|
{ |
367
|
1 |
|
$expecation = new ArrayValueExpectations($this->value); |
368
|
|
|
|
369
|
1 |
|
return $expecation->isKeyOf($values); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param int $length |
374
|
|
|
* |
375
|
|
|
* @return ArrayExpectations|StringExpectations |
376
|
|
|
*/ |
377
|
1 |
|
public function hasLength(int $length) |
378
|
|
|
{ |
379
|
1 |
|
$expectation = new StringExpectations($this->value); |
380
|
1 |
|
if ($expectation->isApproved()) { |
381
|
1 |
|
return $expectation->hasLength($length); |
382
|
|
|
} |
383
|
|
|
|
384
|
1 |
|
$expecation = new ArrayExpectations($this->value); |
385
|
|
|
|
386
|
1 |
|
return $expecation->hasLength($length); |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.