forked from reactphp/promise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionRaceTest.php
More file actions
123 lines (97 loc) · 3 KB
/
FunctionRaceTest.php
File metadata and controls
123 lines (97 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace React\Promise;
use Exception;
class FunctionRaceTest extends TestCase
{
/** @test */
public function shouldReturnForeverPendingPromiseForEmptyInput()
{
race(
[]
)->then($this->expectCallableNever(), $this->expectCallableNever());
}
/** @test */
public function shouldResolveValuesArray()
{
$mock = $this->createCallableMock();
$mock
->expects(self::once())
->method('__invoke')
->with(self::identicalTo(1));
race(
[1, 2, 3]
)->then($mock);
}
/** @test */
public function shouldResolvePromisesArray()
{
$mock = $this->createCallableMock();
$mock
->expects(self::once())
->method('__invoke')
->with(self::identicalTo(2));
$d1 = new Deferred();
$d2 = new Deferred();
$d3 = new Deferred();
race(
[$d1->promise(), $d2->promise(), $d3->promise()]
)->then($mock);
$d2->resolve(2);
$d1->resolve(1);
$d3->resolve(3);
}
/** @test */
public function shouldResolveSparseArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects(self::once())
->method('__invoke')
->with(self::identicalTo(null));
race(
[null, 1, null, 2, 3]
)->then($mock);
}
/** @test */
public function shouldRejectIfFirstSettledPromiseRejects()
{
$exception = new Exception();
$mock = $this->createCallableMock();
$mock
->expects(self::once())
->method('__invoke')
->with(self::identicalTo($exception));
$d1 = new Deferred();
$d2 = new Deferred();
$d3 = new Deferred();
race(
[$d1->promise(), $d2->promise(), $d3->promise()]
)->then($this->expectCallableNever(), $mock);
$d2->reject($exception);
$d1->resolve(1);
$d3->resolve(3);
}
/** @test */
public function shouldCancelInputArrayPromises()
{
$promise1 = new Promise(function () {}, $this->expectCallableOnce());
$promise2 = new Promise(function () {}, $this->expectCallableOnce());
race([$promise1, $promise2])->cancel();
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
{
$deferred = new Deferred($this->expectCallableNever());
$deferred->resolve(null);
$promise2 = new Promise(function () {}, $this->expectCallableNever());
race([$deferred->promise(), $promise2])->cancel();
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects()
{
$deferred = new Deferred($this->expectCallableNever());
$deferred->reject(new Exception());
$promise2 = new Promise(function () {}, $this->expectCallableNever());
race([$deferred->promise(), $promise2])->cancel();
}
}