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