|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kdn\attemptsCounter; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class ActionTest. |
|
7
|
|
|
* @package kdn\attemptsCounter |
|
8
|
|
|
*/ |
|
9
|
|
|
class ActionTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @covers \kdn\attemptsCounter\Action::__construct |
|
13
|
|
|
* @covers \kdn\attemptsCounter\Action::getName |
|
14
|
|
|
* @covers \kdn\attemptsCounter\Action::validate |
|
15
|
|
|
* @small |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testGetName() |
|
18
|
|
|
{ |
|
19
|
|
|
$name = 'foo'; |
|
20
|
|
|
$this->assertEquals($name, (new Action($name, 1))->getName()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @covers \kdn\attemptsCounter\Action::__construct |
|
25
|
|
|
* @covers \kdn\attemptsCounter\Action::increment |
|
26
|
|
|
* @covers \kdn\attemptsCounter\Action::validate |
|
27
|
|
|
* @small |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testIncrement() |
|
30
|
|
|
{ |
|
31
|
|
|
$action = new Action('foo', 2); |
|
32
|
|
|
$action->increment(); // this is fine |
|
33
|
|
|
$actualErrorMessage = null; |
|
34
|
|
|
try { |
|
35
|
|
|
$action->increment(); |
|
36
|
|
|
} catch (AttemptsLimitException $e) { |
|
37
|
|
|
$actualErrorMessage = $e->getMessage(); |
|
38
|
|
|
} |
|
39
|
|
|
$this->assertEquals( |
|
40
|
|
|
'Cannot perform action "foo" after 2 attempt(s) with 0 nanosecond(s) interval(s).', |
|
41
|
|
|
$actualErrorMessage |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @covers \kdn\attemptsCounter\Action::__construct |
|
47
|
|
|
* @covers \kdn\attemptsCounter\Action::increment |
|
48
|
|
|
* @covers \kdn\attemptsCounter\Action::validate |
|
49
|
|
|
* @small |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testIncrementExceptionChaining() |
|
52
|
|
|
{ |
|
53
|
|
|
$action = new Action('foo', 1); |
|
54
|
|
|
$expectedException = new \Exception('Something wrong.'); |
|
55
|
|
|
$actualException = null; |
|
56
|
|
|
try { |
|
57
|
|
|
$action->increment($expectedException); |
|
58
|
|
|
} catch (AttemptsLimitException $e) { |
|
59
|
|
|
$actualException = $e->getPrevious(); |
|
60
|
|
|
} |
|
61
|
|
|
$this->assertEquals($expectedException, $actualException); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @covers \kdn\attemptsCounter\Action::__construct |
|
66
|
|
|
* @covers \kdn\attemptsCounter\Action::increment |
|
67
|
|
|
* @covers \kdn\attemptsCounter\Action::validate |
|
68
|
|
|
* @small |
|
69
|
|
|
*/ |
|
70
|
|
|
public function testIncrementNumber() |
|
71
|
|
|
{ |
|
72
|
|
|
$maxAttempts = 3; |
|
73
|
|
|
$action = new Action('foo', $maxAttempts); |
|
74
|
|
|
$actualErrorMessage = null; |
|
75
|
|
|
try { |
|
76
|
|
|
$action->increment(null, $maxAttempts); |
|
77
|
|
|
} catch (AttemptsLimitException $e) { |
|
78
|
|
|
$actualErrorMessage = $e->getMessage(); |
|
79
|
|
|
} |
|
80
|
|
|
$this->assertEquals( |
|
81
|
|
|
'Cannot perform action "foo" after 3 attempt(s) with 0 nanosecond(s) interval(s).', |
|
82
|
|
|
$actualErrorMessage |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @covers \kdn\attemptsCounter\Action::__construct |
|
88
|
|
|
* @covers \kdn\attemptsCounter\Action::increment |
|
89
|
|
|
* @covers \kdn\attemptsCounter\Action::validate |
|
90
|
|
|
* @medium |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testIncrementDelay() |
|
93
|
|
|
{ |
|
94
|
|
|
$sleepTime = 2; |
|
95
|
|
|
$action = new Action('foo', 3, $sleepTime * 1000000000); |
|
96
|
|
|
$wastedAttemptsNumber = 2; |
|
97
|
|
|
$startTime = time(); |
|
98
|
|
|
$action->increment(null, $wastedAttemptsNumber); |
|
99
|
|
|
$this->assertGreaterThanOrEqual($startTime + $wastedAttemptsNumber * $sleepTime, time()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array[] |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function validateProvider() |
|
106
|
|
|
{ |
|
107
|
|
|
return [ |
|
108
|
|
|
'action name is array' => [[], 1, 0, 'The action name must be a string or an integer.'], |
|
109
|
|
|
'action name is float' => [1.5, 1, 0, 'The action name must be a string or an integer.'], |
|
110
|
|
|
|
|
111
|
|
|
'maximum number of attempts is float' => [ |
|
112
|
|
|
'foo', |
|
113
|
|
|
1.5, |
|
114
|
|
|
0, |
|
115
|
|
|
'The maximum number of attempts must be an integer.', |
|
116
|
|
|
], |
|
117
|
|
|
'maximum number of attempts is zero' => [ |
|
118
|
|
|
'foo', |
|
119
|
|
|
0, |
|
120
|
|
|
0, |
|
121
|
|
|
'The maximum number of attempts must be a positive integer.', |
|
122
|
|
|
], |
|
123
|
|
|
'maximum number of attempts is negative' => [ |
|
124
|
|
|
'foo', |
|
125
|
|
|
-1, |
|
126
|
|
|
0, |
|
127
|
|
|
'The maximum number of attempts must be a positive integer.', |
|
128
|
|
|
], |
|
129
|
|
|
|
|
130
|
|
|
'time interval between attempts is float' => [ |
|
131
|
|
|
'foo', |
|
132
|
|
|
1, |
|
133
|
|
|
1.5, |
|
134
|
|
|
'The time interval in nanoseconds between attempts must be an integer.', |
|
135
|
|
|
], |
|
136
|
|
|
'time interval between attempts is negative' => [ |
|
137
|
|
|
'foo', |
|
138
|
|
|
1, |
|
139
|
|
|
-1, |
|
140
|
|
|
'The time interval in nanoseconds between attempts must be a non-negative integer.', |
|
141
|
|
|
], |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param mixed $name |
|
147
|
|
|
* @param mixed $maxAttempts |
|
148
|
|
|
* @param mixed $sleepTime |
|
149
|
|
|
* @param string $expectedErrorMessage |
|
150
|
|
|
* @covers \kdn\attemptsCounter\Action::__construct |
|
151
|
|
|
* @covers \kdn\attemptsCounter\Action::validate |
|
152
|
|
|
* @dataProvider validateProvider |
|
153
|
|
|
* @small |
|
154
|
|
|
*/ |
|
155
|
|
|
public function testValidate($name, $maxAttempts, $sleepTime, $expectedErrorMessage) |
|
156
|
|
|
{ |
|
157
|
|
|
$actualErrorMessage = null; |
|
158
|
|
|
try { |
|
159
|
|
|
new Action($name, $maxAttempts, $sleepTime); |
|
160
|
|
|
} catch (InvalidConfigException $e) { |
|
161
|
|
|
$actualErrorMessage = $e->getMessage(); |
|
162
|
|
|
} |
|
163
|
|
|
$this->assertEquals($expectedErrorMessage, $actualErrorMessage); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|