1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche/Async component. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Async\Tests\Units\Loop\Timer; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Async\Tests\Units\Promise\PromiseInterfaceTestCase; |
14
|
|
|
use React\EventLoop\Factory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Timer Tests class. |
18
|
|
|
* |
19
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TimerTests extends PromiseInterfaceTestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Test interval,iterations,maxIterations and isActive methods. |
25
|
|
|
*/ |
26
|
|
|
public function testAccessMethods() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
/* @var \Cubiche\Core\Async\Loop\Timer\TimerInterface $timer */ |
30
|
|
|
->given($timer = $this->newDefaultTestedInstance()) |
31
|
|
|
->then() |
32
|
|
|
->float($timer->interval()) |
33
|
|
|
->isEqualTo(0.001) |
34
|
|
|
->integer($timer->iterations()) |
35
|
|
|
->isEqualTo(0) |
36
|
|
|
->integer($timer->maxIterations()) |
37
|
|
|
->isEqualTo(1) |
38
|
|
|
->boolean($timer->isActive()) |
39
|
|
|
->isTrue() |
40
|
|
|
; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test __construct. |
45
|
|
|
*/ |
46
|
|
|
public function testConstructor() |
47
|
|
|
{ |
48
|
|
|
$this |
49
|
|
|
->exception(function () { |
50
|
|
|
$this->newTestedInstance( |
51
|
|
|
Factory::create(), |
52
|
|
|
$this->delegateMock(), |
53
|
|
|
0.001, |
54
|
|
|
true, |
55
|
|
|
'foo' |
56
|
|
|
); |
57
|
|
|
})->isInstanceOf(\InvalidArgumentException::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test Timeout method. |
62
|
|
|
*/ |
63
|
|
|
public function testTimeout() |
64
|
|
|
{ |
65
|
|
|
$this |
66
|
|
|
->given( |
67
|
|
|
$loop = Factory::create(), |
68
|
|
|
$task = $this->delegateMockWithReturn('foo'), |
69
|
|
|
$onFulfilled = $this->delegateMock() |
70
|
|
|
) |
71
|
|
|
/* @var \Cubiche\Core\Async\Loop\Timer\TimerInterface $timer */ |
72
|
|
|
->let($timer = $this->newTestedInstance($loop, $task, 0.001)) |
73
|
|
|
->when(function () use ($loop, $timer, $onFulfilled) { |
74
|
|
|
$timer->then($onFulfilled); |
75
|
|
|
$loop->run(); |
76
|
|
|
}) |
77
|
|
|
->then() |
78
|
|
|
->delegateCall($task) |
79
|
|
|
->once() |
80
|
|
|
->delegateCall($onFulfilled) |
81
|
|
|
->withArguments('foo') |
82
|
|
|
->once() |
83
|
|
|
; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test Timer method. |
88
|
|
|
*/ |
89
|
|
|
public function testTimer() |
90
|
|
|
{ |
91
|
|
|
$this |
92
|
|
|
->given( |
93
|
|
|
$loop = Factory::create(), |
94
|
|
|
$task = $this->delegateMockWithReturn('foo'), |
95
|
|
|
$onRejected = $this->delegateMock(), |
96
|
|
|
$onNotify = $this->delegateMock() |
97
|
|
|
) |
98
|
|
|
/* @var \Cubiche\Core\Async\Loop\Timer\TimerInterface $timer */ |
99
|
|
|
->let($timer = $this->newTestedInstance($loop, $task, 0.001, true, 2)) |
100
|
|
|
/* @var \Cubiche\Core\Async\Loop\Timer\TimerInterface $timer */ |
101
|
|
|
->when(function () use ($loop, $timer, $onRejected, $onNotify) { |
102
|
|
|
$timer->then(null, $onRejected, $onNotify); |
103
|
|
|
$loop->run(); |
104
|
|
|
}) |
105
|
|
|
->then() |
106
|
|
|
->delegateCall($task) |
107
|
|
|
->twice() |
108
|
|
|
->delegateCall($onRejected) |
109
|
|
|
->once() |
110
|
|
|
->delegateCall($onNotify) |
111
|
|
|
->withArguments('foo') |
112
|
|
|
->twice() |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
protected function defaultConstructorArguments() |
120
|
|
|
{ |
121
|
|
|
return array( |
122
|
|
|
Factory::create(), |
123
|
|
|
function () { |
124
|
|
|
return 'foo'; |
125
|
|
|
}, |
126
|
|
|
0.001, |
127
|
|
|
true, |
128
|
|
|
1, |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
protected function promiseDataProvider() |
136
|
|
|
{ |
137
|
|
|
$timeout = $this->newTestedInstance( |
138
|
|
|
$loop = Factory::create(), |
139
|
|
|
function () { |
140
|
|
|
return $this->defaultResolveValue(); |
141
|
|
|
}, |
142
|
|
|
0.001 |
143
|
|
|
); |
144
|
|
|
$loop->run(); |
145
|
|
|
|
146
|
|
|
$timer = $this->newTestedInstance( |
147
|
|
|
$loop = Factory::create(), |
148
|
|
|
function () { |
149
|
|
|
throw $this->defaultRejectReason(); |
150
|
|
|
}, |
151
|
|
|
0.001, |
152
|
|
|
true, |
153
|
|
|
1 |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$loop->run(); |
157
|
|
|
|
158
|
|
|
return array( |
159
|
|
|
array($this->newDefaultTestedInstance()), |
160
|
|
|
array($timeout), |
161
|
|
|
array($timer), |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|