Example #1
0
 /** @test */
 public function shouldReturnAPromiseForAPromisedResolutionValue()
 {
     $d = new Deferred();
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(1));
     $d->resolver()->resolve(When::resolve(1))->then($mock);
 }
Example #2
0
 /** @test */
 public function shouldSupportDeepNestingInPromiseChains()
 {
     $d = new Deferred();
     $d->resolve(false);
     $result = When::resolve(When::resolve($d->then(function ($val) {
         $d = new Deferred();
         $d->resolve($val);
         $identity = function ($val) {
             return $val;
         };
         return When::resolve($d->then($identity))->then(function ($val) {
             return !$val;
         });
     })));
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(true));
     $result->then($mock);
 }
Example #3
0
 public static function reduce($promisesOrValues, $reduceFunc, $initialValue = null)
 {
     return When::resolve($promisesOrValues)->then(function ($array) use($reduceFunc, $initialValue) {
         if (!is_array($array)) {
             $array = array();
         }
         $total = count($array);
         $i = 0;
         // Wrap the supplied $reduceFunc with one that handles promises and then
         // delegates to the supplied.
         $wrappedReduceFunc = function ($current, $val) use($reduceFunc, $total, &$i) {
             return When::resolve($current)->then(function ($c) use($reduceFunc, $total, &$i, $val) {
                 return When::resolve($val)->then(function ($value) use($reduceFunc, $total, &$i, $c) {
                     return call_user_func($reduceFunc, $c, $value, $i++, $total);
                 });
             });
         };
         return array_reduce($array, $wrappedReduceFunc, $initialValue);
     });
 }
Example #4
0
 /** @test */
 public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
 {
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(array()));
     When::some(When::resolve(1), 1, $mock);
 }
Example #5
0
 /** @test */
 public function shouldResolveToInitialValueWhenInputPromiseDoesNotResolveToAnArray()
 {
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(1));
     When::reduce(When::resolve(1), $this->plus(), 1)->then($mock);
 }
Example #6
0
 /** @test */
 public function shouldRejectWhenInputContainsRejection()
 {
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(2));
     When::map(array(When::resolve(1), When::reject(2), When::resolve(3)), $this->mapper())->then($this->expectCallableNever(), $mock);
 }