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