1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\phpspectime\Matcher; |
6
|
|
|
|
7
|
|
|
use loophp\phpspectime\TimeMatcherException; |
8
|
|
|
use PhpSpec\Exception\Example\MatcherException; |
9
|
|
|
|
10
|
|
|
use function count; |
11
|
|
|
use function in_array; |
12
|
|
|
|
13
|
|
|
final class TakeInBetweenMatcher extends AbstractTakeThanMatcher |
14
|
|
|
{ |
15
|
|
|
protected $keywords = [ |
16
|
|
|
'takeInBetween', |
17
|
|
|
'lastInBetween', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @psalm-suppress UndefinedDocblockClass |
22
|
|
|
* |
23
|
|
|
* @param mixed $subject |
24
|
|
|
*/ |
25
|
1 |
|
public function supports(string $name, $subject, array $arguments): bool |
26
|
|
|
{ |
27
|
1 |
|
return in_array($name, $this->keywords, true) && 2 === count($arguments); |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
public function verifyNegative(callable $callable, array $arguments, array $parameters): bool |
31
|
|
|
{ |
32
|
2 |
|
[$from, $to] = $parameters; |
33
|
|
|
|
34
|
2 |
|
$elapsedTime = $this->bench($callable, $arguments); |
35
|
|
|
|
36
|
2 |
|
if ($elapsedTime < $from || $elapsedTime > $to) { |
37
|
1 |
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
throw TimeMatcherException::notInRangeMatcherException( |
41
|
1 |
|
$this->presenter, |
42
|
|
|
$elapsedTime, |
43
|
|
|
$from, |
44
|
|
|
$to |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function verifyPositive(callable $callable, array $arguments, array $parameters): bool |
49
|
|
|
{ |
50
|
2 |
|
[$from, $to] = $parameters; |
51
|
|
|
|
52
|
2 |
|
$elapsedTime = $this->bench($callable, $arguments); |
53
|
|
|
|
54
|
2 |
|
if ($elapsedTime >= $from && $elapsedTime <= $to) { |
55
|
1 |
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
throw TimeMatcherException::notInRangeMatcherException( |
59
|
1 |
|
$this->presenter, |
60
|
|
|
$elapsedTime, |
61
|
|
|
$from, |
62
|
|
|
$to |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
protected function getParameters(array $arguments): array |
67
|
|
|
{ |
68
|
5 |
|
$from = current($arguments); |
69
|
5 |
|
$to = end($arguments); |
70
|
|
|
|
71
|
5 |
|
if (!is_numeric($from)) { |
72
|
1 |
|
throw new MatcherException( |
73
|
1 |
|
sprintf( |
74
|
|
|
"Wrong 'from' argument provided in TakeInBetween matcher.\n" . |
75
|
|
|
"Numeric value expected,\n" . |
76
|
1 |
|
'Got %s.', |
77
|
1 |
|
$this->presenter->presentValue($arguments[0]) |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
if (!is_numeric($to)) { |
83
|
1 |
|
throw new MatcherException( |
84
|
1 |
|
sprintf( |
85
|
|
|
"Wrong 'to' argument provided in TakeInBetween matcher.\n" . |
86
|
|
|
"Numeric value expected,\n" . |
87
|
1 |
|
'Got %s.', |
88
|
1 |
|
$this->presenter->presentValue($arguments[0]) |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
if ($to < $from) { |
94
|
1 |
|
throw new MatcherException( |
95
|
|
|
"Wrong argument provided in TakeInBetween matcher.\n" . |
96
|
1 |
|
"First argument should be equal or greater to second argument.\n" |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return [$from, $to]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|