1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Expectation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class NumericExpectations |
7
|
|
|
* @package Dgame\Expectation |
8
|
|
|
*/ |
9
|
|
|
class NumericExpectations extends ScalarExpectations |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* NumericExpectations constructor. |
13
|
|
|
* |
14
|
|
|
* @param mixed $value |
15
|
|
|
*/ |
16
|
14 |
|
public function __construct($value) |
17
|
|
|
{ |
18
|
14 |
|
if ($this->isNumeric($value)) { |
19
|
14 |
|
parent::__construct($value); |
20
|
|
|
} |
21
|
14 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param mixed $value |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
14 |
|
final protected function isNumeric($value): bool |
29
|
|
|
{ |
30
|
14 |
|
return $this->approveIf(is_numeric($value))->isApproved(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param float $lhs |
35
|
|
|
* @param float $rhs |
36
|
|
|
* |
37
|
|
|
* @return NumericExpectations |
38
|
|
|
*/ |
39
|
1 |
|
public function isBetween(float $lhs, float $rhs): self |
40
|
|
|
{ |
41
|
1 |
|
return $this->approveIf($this->value >= $lhs && $this->value <= $rhs); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param float $value |
46
|
|
|
* |
47
|
|
|
* @return NumericExpectations |
48
|
|
|
*/ |
49
|
2 |
|
public function isBelow(float $value): self |
50
|
|
|
{ |
51
|
2 |
|
return $this->approveIf($this->value < $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param float $value |
56
|
|
|
* |
57
|
|
|
* @return NumericExpectations |
58
|
|
|
*/ |
59
|
1 |
|
public function isAbove(float $value): self |
60
|
|
|
{ |
61
|
1 |
|
return $this->approveIf($this->value > $value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param float $value |
66
|
|
|
* |
67
|
|
|
* @return NumericExpectations |
68
|
|
|
*/ |
69
|
1 |
|
public function isBelowOrEqual(float $value): self |
70
|
|
|
{ |
71
|
1 |
|
return $this->approveIf($this->value <= $value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param float $value |
76
|
|
|
* |
77
|
|
|
* @return NumericExpectations |
78
|
|
|
*/ |
79
|
2 |
|
public function isAboveOrEqual(float $value): self |
80
|
|
|
{ |
81
|
2 |
|
return $this->approveIf($this->value >= $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return NumericExpectations |
|
|
|
|
86
|
|
|
*/ |
87
|
1 |
|
public function isPositive(): self |
88
|
|
|
{ |
89
|
1 |
|
return $this->isAboveOrEqual(0); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return NumericExpectations |
|
|
|
|
94
|
|
|
*/ |
95
|
1 |
|
public function isNegative(): self |
96
|
|
|
{ |
97
|
1 |
|
return $this->isBelow(0); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.