Inheritance: implements Kraken\Promise\DeferredInterface
Beispiel #1
0
 /**
  *
  */
 public function testPromise_SupportsVeryDeepNesting()
 {
     ini_set('xdebug.max_nesting_level', 8192);
     $deferreds = [];
     for ($i = 0; $i < 10; $i++) {
         $deferreds[] = $d = new Deferred();
         $p = $d->getPromise();
         $last = $p;
         for ($j = 0; $j < 500; $j++) {
             $last = $last->then(function ($result) {
                 return $result;
             });
         }
     }
     $p = null;
     foreach ($deferreds as $d) {
         if ($p) {
             $d->resolve($p);
         }
         $p = $d->getPromise();
     }
     $deferreds[0]->resolve(true);
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(true));
     $deferreds[0]->getPromise()->then($mock);
 }
 /**
  *
  */
 public function testApiDoResolve_SupportsDeepNestingInPromiseChains()
 {
     $test = $this->getTest();
     $d = new Deferred();
     $d->resolve(false);
     $result = Promise::doResolve(Promise::doResolve($d->getPromise()->then(function ($val) {
         $d = new Deferred();
         $d->resolve($val);
         $identity = function ($val) {
             return $val;
         };
         return Promise::doResolve($d->getPromise()->then($identity))->then(function ($val) {
             return !$val;
         });
     })));
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo(true));
     $result->then($mock);
 }
 /**
  *
  */
 public function testApiCancel_CancelsPendingInputArrayPromises_IfOnePromiseFulfills()
 {
     $test = $this->getTest();
     $deferred = new Deferred();
     $deferred->resolve();
     $mock = $test->createCallableMock();
     $mock->expects($test->never())->method('__invoke');
     $promise = $deferred->getPromise();
     $promise->then(null, null, $mock);
     $mock2 = $test->getMock(PromiseInterface::class);
     $mock2->expects($test->once())->method('cancel');
     Promise::some([$promise, $mock2], 1)->cancel();
 }
 /**
  *
  */
 public function testApiDone_ThrowsException_WithDeepNestingPromiseChains_ForRejectedPromise()
 {
     $test = $this->getTest();
     $test->setExpectedException(Exception::class, 'UnhandledRejectionException');
     $exception = new Exception('UnhandledRejectionException');
     $d = new Deferred();
     $d->resolve();
     $result = Promise::doResolve(Promise::doResolve($d->getPromise()->then(function () use($exception) {
         $d = new Deferred();
         $d->resolve();
         return Promise::doResolve($d->getPromise()->then(function () {
         }))->then(function () use($exception) {
             throw $exception;
         });
     })));
     $result->done();
 }
 /**
  *
  */
 public function testApiReduce_ProvidesCorrectBasisValue()
 {
     $test = $this->getTest();
     $insertIntoArray = function ($arr, $val, $i) {
         $arr[$i] = $val;
         return $arr;
     };
     $d1 = new Deferred();
     $d2 = new Deferred();
     $d3 = new Deferred();
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo([1, 2, 3]));
     Promise::reduce([$d1->getPromise(), $d2->getPromise(), $d3->getPromise()], $insertIntoArray, [])->then($mock);
     $d3->resolve(3);
     $d1->resolve(1);
     $d2->resolve(2);
 }
 /**
  *
  */
 public function testApiSome_CancelsPendingInputArrayPromises_IfEnoughPromisesReject()
 {
     $test = $this->getTest();
     $mock = $test->createCallableMock();
     $mock->expects($test->never())->method('__invoke');
     $deferred = new Deferred($mock);
     $deferred->reject();
     $mock2 = $test->getMock(PromiseInterface::class);
     $mock2->expects($test->once())->method('cancel');
     Promise::some([$deferred->getPromise(), $mock2], 2);
 }