xref: /plugin/davcal/vendor/sabre/event/tests/PromiseTest.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\Event;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehlerclass PromiseTest extends \PHPUnit_Framework_TestCase {
6*a1a3b679SAndreas Boehler
7*a1a3b679SAndreas Boehler    function testSuccess() {
8*a1a3b679SAndreas Boehler
9*a1a3b679SAndreas Boehler        $finalValue = 0;
10*a1a3b679SAndreas Boehler        $promise = new Promise();
11*a1a3b679SAndreas Boehler        $promise->fulfill(1);
12*a1a3b679SAndreas Boehler
13*a1a3b679SAndreas Boehler        $promise->then(function($value) use (&$finalValue) {
14*a1a3b679SAndreas Boehler            $finalValue = $value + 2;
15*a1a3b679SAndreas Boehler        });
16*a1a3b679SAndreas Boehler
17*a1a3b679SAndreas Boehler        $this->assertEquals(3, $finalValue);
18*a1a3b679SAndreas Boehler
19*a1a3b679SAndreas Boehler    }
20*a1a3b679SAndreas Boehler
21*a1a3b679SAndreas Boehler    function testFail() {
22*a1a3b679SAndreas Boehler
23*a1a3b679SAndreas Boehler        $finalValue = 0;
24*a1a3b679SAndreas Boehler        $promise = new Promise();
25*a1a3b679SAndreas Boehler        $promise->reject(1);
26*a1a3b679SAndreas Boehler
27*a1a3b679SAndreas Boehler        $promise->then(null, function($value) use (&$finalValue) {
28*a1a3b679SAndreas Boehler            $finalValue = $value + 2;
29*a1a3b679SAndreas Boehler        });
30*a1a3b679SAndreas Boehler
31*a1a3b679SAndreas Boehler        $this->assertEquals(3, $finalValue);
32*a1a3b679SAndreas Boehler
33*a1a3b679SAndreas Boehler    }
34*a1a3b679SAndreas Boehler
35*a1a3b679SAndreas Boehler    function testChain() {
36*a1a3b679SAndreas Boehler
37*a1a3b679SAndreas Boehler        $finalValue = 0;
38*a1a3b679SAndreas Boehler        $promise = new Promise();
39*a1a3b679SAndreas Boehler        $promise->fulfill(1);
40*a1a3b679SAndreas Boehler
41*a1a3b679SAndreas Boehler        $promise->then(function($value) use (&$finalValue) {
42*a1a3b679SAndreas Boehler            $finalValue = $value + 2;
43*a1a3b679SAndreas Boehler            return $finalValue;
44*a1a3b679SAndreas Boehler        })->then(function($value) use (&$finalValue) {
45*a1a3b679SAndreas Boehler            $finalValue = $value + 4;
46*a1a3b679SAndreas Boehler            return $finalValue;
47*a1a3b679SAndreas Boehler        });
48*a1a3b679SAndreas Boehler
49*a1a3b679SAndreas Boehler        $this->assertEquals(7, $finalValue);
50*a1a3b679SAndreas Boehler
51*a1a3b679SAndreas Boehler    }
52*a1a3b679SAndreas Boehler    function testChainPromise() {
53*a1a3b679SAndreas Boehler
54*a1a3b679SAndreas Boehler        $finalValue = 0;
55*a1a3b679SAndreas Boehler        $promise = new Promise();
56*a1a3b679SAndreas Boehler        $promise->fulfill(1);
57*a1a3b679SAndreas Boehler
58*a1a3b679SAndreas Boehler        $subPromise = new Promise();
59*a1a3b679SAndreas Boehler
60*a1a3b679SAndreas Boehler        $promise->then(function($value) use ($subPromise) {
61*a1a3b679SAndreas Boehler            return $subPromise;
62*a1a3b679SAndreas Boehler        })->then(function($value) use (&$finalValue) {
63*a1a3b679SAndreas Boehler            $finalValue = $value + 4;
64*a1a3b679SAndreas Boehler            return $finalValue;
65*a1a3b679SAndreas Boehler        });
66*a1a3b679SAndreas Boehler
67*a1a3b679SAndreas Boehler        $subPromise->fulfill(2);
68*a1a3b679SAndreas Boehler
69*a1a3b679SAndreas Boehler        $this->assertEquals(6, $finalValue);
70*a1a3b679SAndreas Boehler
71*a1a3b679SAndreas Boehler    }
72*a1a3b679SAndreas Boehler
73*a1a3b679SAndreas Boehler    function testPendingResult() {
74*a1a3b679SAndreas Boehler
75*a1a3b679SAndreas Boehler        $finalValue = 0;
76*a1a3b679SAndreas Boehler        $promise = new Promise();
77*a1a3b679SAndreas Boehler
78*a1a3b679SAndreas Boehler        $promise->then(function($value) use (&$finalValue) {
79*a1a3b679SAndreas Boehler            $finalValue = $value + 2;
80*a1a3b679SAndreas Boehler        });
81*a1a3b679SAndreas Boehler
82*a1a3b679SAndreas Boehler        $promise->fulfill(4);
83*a1a3b679SAndreas Boehler        $this->assertEquals(6, $finalValue);
84*a1a3b679SAndreas Boehler
85*a1a3b679SAndreas Boehler    }
86*a1a3b679SAndreas Boehler
87*a1a3b679SAndreas Boehler    function testPendingFail() {
88*a1a3b679SAndreas Boehler
89*a1a3b679SAndreas Boehler        $finalValue = 0;
90*a1a3b679SAndreas Boehler        $promise = new Promise();
91*a1a3b679SAndreas Boehler
92*a1a3b679SAndreas Boehler        $promise->then(null, function($value) use (&$finalValue) {
93*a1a3b679SAndreas Boehler            $finalValue = $value + 2;
94*a1a3b679SAndreas Boehler        });
95*a1a3b679SAndreas Boehler
96*a1a3b679SAndreas Boehler        $promise->reject(4);
97*a1a3b679SAndreas Boehler        $this->assertEquals(6, $finalValue);
98*a1a3b679SAndreas Boehler
99*a1a3b679SAndreas Boehler    }
100*a1a3b679SAndreas Boehler
101*a1a3b679SAndreas Boehler    function testExecutorSuccess() {
102*a1a3b679SAndreas Boehler
103*a1a3b679SAndreas Boehler        $promise = (new Promise(function($success, $fail) {
104*a1a3b679SAndreas Boehler
105*a1a3b679SAndreas Boehler            $success('hi');
106*a1a3b679SAndreas Boehler
107*a1a3b679SAndreas Boehler        }))->then(function($result) use (&$realResult) {
108*a1a3b679SAndreas Boehler
109*a1a3b679SAndreas Boehler            $realResult = $result;
110*a1a3b679SAndreas Boehler
111*a1a3b679SAndreas Boehler        });
112*a1a3b679SAndreas Boehler
113*a1a3b679SAndreas Boehler        $this->assertEquals('hi', $realResult);
114*a1a3b679SAndreas Boehler
115*a1a3b679SAndreas Boehler    }
116*a1a3b679SAndreas Boehler
117*a1a3b679SAndreas Boehler    function testExecutorFail() {
118*a1a3b679SAndreas Boehler
119*a1a3b679SAndreas Boehler        $promise = (new Promise(function($success, $fail) {
120*a1a3b679SAndreas Boehler
121*a1a3b679SAndreas Boehler            $fail('hi');
122*a1a3b679SAndreas Boehler
123*a1a3b679SAndreas Boehler        }))->then(function($result) use (&$realResult) {
124*a1a3b679SAndreas Boehler
125*a1a3b679SAndreas Boehler            $realResult = 'incorrect';
126*a1a3b679SAndreas Boehler
127*a1a3b679SAndreas Boehler        }, function($reason) use (&$realResult) {
128*a1a3b679SAndreas Boehler
129*a1a3b679SAndreas Boehler            $realResult = $reason;
130*a1a3b679SAndreas Boehler
131*a1a3b679SAndreas Boehler        });
132*a1a3b679SAndreas Boehler
133*a1a3b679SAndreas Boehler        $this->assertEquals('hi', $realResult);
134*a1a3b679SAndreas Boehler
135*a1a3b679SAndreas Boehler    }
136*a1a3b679SAndreas Boehler
137*a1a3b679SAndreas Boehler    /**
138*a1a3b679SAndreas Boehler     * @expectedException \Sabre\Event\PromiseAlreadyResolvedException
139*a1a3b679SAndreas Boehler     */
140*a1a3b679SAndreas Boehler    function testFulfillTwice() {
141*a1a3b679SAndreas Boehler
142*a1a3b679SAndreas Boehler        $promise = new Promise();
143*a1a3b679SAndreas Boehler        $promise->fulfill(1);
144*a1a3b679SAndreas Boehler        $promise->fulfill(1);
145*a1a3b679SAndreas Boehler
146*a1a3b679SAndreas Boehler    }
147*a1a3b679SAndreas Boehler
148*a1a3b679SAndreas Boehler    /**
149*a1a3b679SAndreas Boehler     * @expectedException \Sabre\Event\PromiseAlreadyResolvedException
150*a1a3b679SAndreas Boehler     */
151*a1a3b679SAndreas Boehler    function testRejectTwice() {
152*a1a3b679SAndreas Boehler
153*a1a3b679SAndreas Boehler        $promise = new Promise();
154*a1a3b679SAndreas Boehler        $promise->reject(1);
155*a1a3b679SAndreas Boehler        $promise->reject(1);
156*a1a3b679SAndreas Boehler
157*a1a3b679SAndreas Boehler    }
158*a1a3b679SAndreas Boehler
159*a1a3b679SAndreas Boehler    function testFromFailureHandler() {
160*a1a3b679SAndreas Boehler
161*a1a3b679SAndreas Boehler        $ok = 0;
162*a1a3b679SAndreas Boehler        $promise = new Promise();
163*a1a3b679SAndreas Boehler        $promise->error(function($reason) {
164*a1a3b679SAndreas Boehler
165*a1a3b679SAndreas Boehler            $this->assertEquals('foo', $reason);
166*a1a3b679SAndreas Boehler            throw new \Exception('hi');
167*a1a3b679SAndreas Boehler
168*a1a3b679SAndreas Boehler        })->then(function() use (&$ok) {
169*a1a3b679SAndreas Boehler
170*a1a3b679SAndreas Boehler            $ok = -1;
171*a1a3b679SAndreas Boehler
172*a1a3b679SAndreas Boehler        }, function() use (&$ok) {
173*a1a3b679SAndreas Boehler
174*a1a3b679SAndreas Boehler            $ok = 1;
175*a1a3b679SAndreas Boehler
176*a1a3b679SAndreas Boehler        });
177*a1a3b679SAndreas Boehler
178*a1a3b679SAndreas Boehler        $this->assertEquals(0, $ok);
179*a1a3b679SAndreas Boehler        $promise->reject('foo');
180*a1a3b679SAndreas Boehler        $this->assertEquals(1, $ok);
181*a1a3b679SAndreas Boehler
182*a1a3b679SAndreas Boehler    }
183*a1a3b679SAndreas Boehler
184*a1a3b679SAndreas Boehler    function testAll() {
185*a1a3b679SAndreas Boehler
186*a1a3b679SAndreas Boehler        $promise1 = new Promise();
187*a1a3b679SAndreas Boehler        $promise2 = new Promise();
188*a1a3b679SAndreas Boehler
189*a1a3b679SAndreas Boehler        $finalValue = 0;
190*a1a3b679SAndreas Boehler        Promise::all([$promise1, $promise2])->then(function($value) use (&$finalValue) {
191*a1a3b679SAndreas Boehler
192*a1a3b679SAndreas Boehler            $finalValue = $value;
193*a1a3b679SAndreas Boehler
194*a1a3b679SAndreas Boehler        });
195*a1a3b679SAndreas Boehler
196*a1a3b679SAndreas Boehler        $promise1->fulfill(1);
197*a1a3b679SAndreas Boehler        $this->assertEquals(0, $finalValue);
198*a1a3b679SAndreas Boehler        $promise2->fulfill(2);
199*a1a3b679SAndreas Boehler        $this->assertEquals([1, 2], $finalValue);
200*a1a3b679SAndreas Boehler
201*a1a3b679SAndreas Boehler    }
202*a1a3b679SAndreas Boehler
203*a1a3b679SAndreas Boehler    function testAllReject() {
204*a1a3b679SAndreas Boehler
205*a1a3b679SAndreas Boehler        $promise1 = new Promise();
206*a1a3b679SAndreas Boehler        $promise2 = new Promise();
207*a1a3b679SAndreas Boehler
208*a1a3b679SAndreas Boehler        $finalValue = 0;
209*a1a3b679SAndreas Boehler        Promise::all([$promise1, $promise2])->then(
210*a1a3b679SAndreas Boehler            function($value) use (&$finalValue) {
211*a1a3b679SAndreas Boehler                $finalValue = 'foo';
212*a1a3b679SAndreas Boehler                return 'test';
213*a1a3b679SAndreas Boehler            },
214*a1a3b679SAndreas Boehler            function($value) use (&$finalValue) {
215*a1a3b679SAndreas Boehler                $finalValue = $value;
216*a1a3b679SAndreas Boehler            }
217*a1a3b679SAndreas Boehler        );
218*a1a3b679SAndreas Boehler
219*a1a3b679SAndreas Boehler        $promise1->reject(1);
220*a1a3b679SAndreas Boehler        $this->assertEquals(1, $finalValue);
221*a1a3b679SAndreas Boehler        $promise2->reject(2);
222*a1a3b679SAndreas Boehler        $this->assertEquals(1, $finalValue);
223*a1a3b679SAndreas Boehler
224*a1a3b679SAndreas Boehler    }
225*a1a3b679SAndreas Boehler
226*a1a3b679SAndreas Boehler}
227