ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FunctionsTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Event\Promise;
4 
7 
9 
10  function testAll() {
11 
12  $promise1 = new Promise();
13  $promise2 = new Promise();
14 
15  $finalValue = 0;
16  Promise\all([$promise1, $promise2])->then(function($value) use (&$finalValue) {
17 
18  $finalValue = $value;
19 
20  });
21 
22  $promise1->fulfill(1);
23  Loop\run();
24  $this->assertEquals(0, $finalValue);
25 
26  $promise2->fulfill(2);
27  Loop\run();
28  $this->assertEquals([1, 2], $finalValue);
29 
30  }
31 
32  function testAllReject() {
33 
34  $promise1 = new Promise();
35  $promise2 = new Promise();
36 
37  $finalValue = 0;
38  Promise\all([$promise1, $promise2])->then(
39  function($value) use (&$finalValue) {
40  $finalValue = 'foo';
41  return 'test';
42  },
43  function($value) use (&$finalValue) {
44  $finalValue = $value;
45  }
46  );
47 
48  $promise1->reject(1);
49  Loop\run();
50  $this->assertEquals(1, $finalValue);
51  $promise2->reject(2);
52  Loop\run();
53  $this->assertEquals(1, $finalValue);
54 
55  }
56 
58 
59  $promise1 = new Promise();
60  $promise2 = new Promise();
61 
62  $finalValue = 0;
63  Promise\all([$promise1, $promise2])->then(
64  function($value) use (&$finalValue) {
65  $finalValue = 'foo';
66  return 'test';
67  },
68  function($value) use (&$finalValue) {
69  $finalValue = $value;
70  }
71  );
72 
73  $promise1->reject(1);
74  Loop\run();
75  $this->assertEquals(1, $finalValue);
76  $promise2->fulfill(2);
77  Loop\run();
78  $this->assertEquals(1, $finalValue);
79 
80  }
81 
82  function testRace() {
83 
84  $promise1 = new Promise();
85  $promise2 = new Promise();
86 
87  $finalValue = 0;
88  Promise\race([$promise1, $promise2])->then(
89  function($value) use (&$finalValue) {
90  $finalValue = $value;
91  },
92  function($value) use (&$finalValue) {
93  $finalValue = $value;
94  }
95  );
96 
97  $promise1->fulfill(1);
98  Loop\run();
99  $this->assertEquals(1, $finalValue);
100  $promise2->fulfill(2);
101  Loop\run();
102  $this->assertEquals(1, $finalValue);
103 
104  }
105 
106  function testRaceReject() {
107 
108  $promise1 = new Promise();
109  $promise2 = new Promise();
110 
111  $finalValue = 0;
112  Promise\race([$promise1, $promise2])->then(
113  function($value) use (&$finalValue) {
114  $finalValue = $value;
115  },
116  function($value) use (&$finalValue) {
117  $finalValue = $value;
118  }
119  );
120 
121  $promise1->reject(1);
122  Loop\run();
123  $this->assertEquals(1, $finalValue);
124  $promise2->reject(2);
125  Loop\run();
126  $this->assertEquals(1, $finalValue);
127 
128  }
129 
130  function testResolve() {
131 
132  $finalValue = 0;
133 
134  $promise = resolve(1);
135  $promise->then(function($value) use (&$finalValue) {
136 
137  $finalValue = $value;
138 
139  });
140 
141  $this->assertEquals(0, $finalValue);
142  Loop\run();
143  $this->assertEquals(1, $finalValue);
144 
145  }
146 
150  function testResolvePromise() {
151 
152  $finalValue = 0;
153 
154  $promise = new Promise();
155  $promise->reject(new \Exception('uh oh'));
156 
157  $newPromise = resolve($promise);
158  $newPromise->wait();
159 
160  }
161 
162  function testReject() {
163 
164  $finalValue = 0;
165 
166  $promise = reject(1);
167  $promise->then(function($value) use (&$finalValue) {
168 
169  $finalValue = 'im broken';
170 
171  }, function($reason) use (&$finalValue) {
172 
173  $finalValue = $reason;
174 
175  });
176 
177  $this->assertEquals(0, $finalValue);
178  Loop\run();
179  $this->assertEquals(1, $finalValue);
180 
181  }
182 
183 
184 }
An implementation of the Promise pattern.
Definition: Promise.php:23
$promise
This example shows demonstrates the Promise api.
Definition: promise.php:16
resolve($value)
Returns a Promise that resolves with the given value.
Definition: functions.php:111
static all(array $promises)
Deprecated.
Definition: Promise.php:314
run()
Runs the loop.
Definition: functions.php:130
reject($reason=null)
Marks this promise as rejected, and set it&#39;s rejection reason.
Definition: Promise.php:161
race(array $promises)
The race function returns a promise that resolves or rejects as soon as one of the promises in the ar...
Definition: functions.php:71