This commit is contained in:
2022-10-23 01:39:27 +02:00
parent 8c17aab483
commit 1929b84685
4130 changed files with 479334 additions and 0 deletions

View File

@@ -0,0 +1,246 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise;
trait CancelTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function cancelShouldCallCancellerWithResolverArguments()
{
$args = null;
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject, $notify) use (&$args) {
$args = func_get_args();
});
$adapter->promise()->cancel();
$this->assertCount(3, $args);
$this->assertTrue(is_callable($args[0]));
$this->assertTrue(is_callable($args[1]));
$this->assertTrue(is_callable($args[2]));
}
/** @test */
public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed()
{
$args = null;
$adapter = $this->getPromiseTestAdapter(function () use (&$args) {
$args = func_num_args();
});
$adapter->promise()->cancel();
$this->assertSame(0, $args);
}
/** @test */
public function cancelShouldFulfillPromiseIfCancellerFulfills()
{
$adapter = $this->getPromiseTestAdapter(function ($resolve) {
$resolve(1);
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock, $this->expectCallableNever());
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldRejectPromiseIfCancellerRejects()
{
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject) {
$reject(1);
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows()
{
$e = new \Exception();
$adapter = $this->getPromiseTestAdapter(function () use ($e) {
throw $e;
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($e));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldProgressPromiseIfCancellerNotifies()
{
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject, $progress) {
$progress(1);
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $this->expectCallableNever(), $mock);
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnCallback(function ($resolve) {
$resolve();
}));
$adapter = $this->getPromiseTestAdapter($mock);
$adapter->promise()->cancel();
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldHaveNoEffectIfCancellerDoesNothing()
{
$adapter = $this->getPromiseTestAdapter(function () {});
$adapter->promise()
->then($this->expectCallableNever(), $this->expectCallableNever());
$adapter->promise()->cancel();
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldCallCancellerFromDeepNestedPromiseChain()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke');
$adapter = $this->getPromiseTestAdapter($mock);
$promise = $adapter->promise()
->then(function () {
return new Promise\Promise(function () {});
})
->then(function () {
$d = new Promise\Deferred();
return $d->promise();
})
->then(function () {
return new Promise\Promise(function () {});
});
$promise->cancel();
}
/** @test */
public function cancelCalledOnChildrenSouldOnlyCancelWhenAllChildrenCancelled()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$child1 = $adapter->promise()
->then()
->then();
$adapter->promise()
->then();
$child1->cancel();
}
/** @test */
public function cancelShouldTriggerCancellerWhenAllChildrenCancel()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
$child1 = $adapter->promise()
->then()
->then();
$child2 = $adapter->promise()
->then();
$child1->cancel();
$child2->cancel();
}
/** @test */
public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultipleTimes()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$child1 = $adapter->promise()
->then()
->then();
$child2 = $adapter->promise()
->then();
$child1->cancel();
$child1->cancel();
}
/** @test */
public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
$adapter->promise()->cancel();
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
$adapter->promise()
->then()
->then();
$adapter->promise()
->then();
$adapter->promise()->cancel();
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace React\Promise\PromiseTest;
trait FullTestTrait
{
use PromisePendingTestTrait,
PromiseSettledTestTrait,
PromiseFulfilledTestTrait,
PromiseRejectedTestTrait,
ResolveTestTrait,
RejectTestTrait,
NotifyTestTrait,
CancelTestTrait;
}

View File

@@ -0,0 +1,336 @@
<?php
namespace React\Promise\PromiseTest;
trait NotifyTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function notifyShouldProgress()
{
$adapter = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then($this->expectCallableNever(), $this->expectCallableNever(), $mock);
$adapter->notify($sentinel);
}
/** @test */
public function notifyShouldPropagateProgressToDownstreamPromises()
{
$adapter = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnArgument(0));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->notify($sentinel);
}
/** @test */
public function notifyShouldPropagateTransformedProgressToDownstreamPromises()
{
$adapter = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnValue($sentinel));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->notify(1);
}
/** @test */
public function notifyShouldPropagateCaughtExceptionValueAsProgress()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->notify(1);
}
/** @test */
public function notifyShouldForwardProgressEventsWhenIntermediaryCallbackTiedToAResolvedPromiseReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$promise2 = $adapter2->promise();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
// resolve BEFORE attaching progress handler
$adapter->resolve();
$adapter->promise()
->then(function () use ($promise2) {
return $promise2;
})
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
);
$adapter2->notify($sentinel);
}
/** @test */
public function notifyShouldForwardProgressEventsWhenIntermediaryCallbackTiedToAnUnresolvedPromiseReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$promise2 = $adapter2->promise();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(function () use ($promise2) {
return $promise2;
})
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
);
// resolve AFTER attaching progress handler
$adapter->resolve();
$adapter2->notify($sentinel);
}
/** @test */
public function notifyShouldForwardProgressWhenResolvedWithAnotherPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnValue($sentinel));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->resolve($adapter2->promise());
$adapter2->notify($sentinel);
}
/** @test */
public function notifyShouldAllowResolveAfterProgress()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->at(0))
->method('__invoke')
->with($this->identicalTo(1));
$mock
->expects($this->at(1))
->method('__invoke')
->with($this->identicalTo(2));
$adapter->promise()
->then(
$mock,
$this->expectCallableNever(),
$mock
);
$adapter->notify(1);
$adapter->resolve(2);
}
/** @test */
public function notifyShouldAllowRejectAfterProgress()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->at(0))
->method('__invoke')
->with($this->identicalTo(1));
$mock
->expects($this->at(1))
->method('__invoke')
->with($this->identicalTo(2));
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock,
$mock
);
$adapter->notify(1);
$adapter->reject(2);
}
/** @test */
public function notifyShouldReturnSilentlyOnProgressWhenAlreadyRejected()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject(1);
$this->assertNull($adapter->notify());
}
/** @test */
public function notifyShouldInvokeProgressHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()->progress($mock);
$adapter->notify(1);
}
/** @test */
public function notifyShouldInvokeProgressHandlerFromDone()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$this->assertNull($adapter->promise()->done(null, null, $mock));
$adapter->notify(1);
}
/** @test */
public function notifyShouldThrowExceptionThrownProgressHandlerFromDone()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, null, function () {
throw new \Exception('UnhandledRejectionException');
}));
$adapter->notify(1);
}
}

View File

@@ -0,0 +1,351 @@
<?php
namespace React\Promise\PromiseTest;
trait PromiseFulfilledTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function fulfilledPromiseShouldBeImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->resolve(1);
$adapter->resolve(2);
$adapter->promise()
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function fulfilledPromiseShouldInvokeNewlyAddedCallback()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->resolve(1);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock, $this->expectCallableNever());
}
/** @test */
public function thenShouldForwardResultWhenCallbackIsNull()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->resolve(1);
$adapter->promise()
->then(
null,
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function thenShouldForwardCallbackResultToNextCallback()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->resolve(1);
$adapter->promise()
->then(
function ($val) {
return $val + 1;
},
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function thenShouldForwardPromisedCallbackResultValueToNextCallback()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->resolve(1);
$adapter->promise()
->then(
function ($val) {
return \React\Promise\resolve($val + 1);
},
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejection()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->resolve(1);
$adapter->promise()
->then(
function ($val) {
return \React\Promise\reject($val + 1);
},
$this->expectCallableNever()
)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->resolve(1);
$adapter->promise()
->then(
$mock,
$this->expectCallableNever()
)
->then(
$this->expectCallableNever(),
$mock2
);
}
/** @test */
public function cancelShouldReturnNullForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->resolve();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function cancelShouldHaveNoEffectForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$adapter->resolve();
$adapter->promise()->cancel();
}
/** @test */
public function doneShouldInvokeFulfillmentHandlerForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->resolve(1);
$this->assertNull($adapter->promise()->done($mock));
}
/** @test */
public function doneShouldThrowExceptionThrownFulfillmentHandlerForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->resolve(1);
$this->assertNull($adapter->promise()->done(function () {
throw new \Exception('UnhandledRejectionException');
}));
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenFulfillmentHandlerRejectsForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$adapter->resolve(1);
$this->assertNull($adapter->promise()->done(function () {
return \React\Promise\reject();
}));
}
/** @test */
public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->resolve(1);
$adapter->promise()->otherwise($this->expectCallableNever());
}
/** @test */
public function alwaysShouldNotSuppressValueForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->resolve($value);
$adapter->promise()
->always(function () {})
->then($mock);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->resolve($value);
$adapter->promise()
->always(function () {
return 1;
})
->then($mock);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->resolve($value);
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then($mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->resolve(1);
$adapter->promise()
->always(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->resolve(1);
$adapter->promise()
->always(function () use ($exception) {
return \React\Promise\reject($exception);
})
->then(null, $mock);
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace React\Promise\PromiseTest;
trait PromisePendingTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function thenShouldReturnAPromiseForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then());
}
/** @test */
public function thenShouldReturnAllowNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then(null, null, null));
}
/** @test */
public function cancelShouldReturnNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function doneShouldReturnNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->done());
}
/** @test */
public function doneShouldReturnAllowNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->done(null, null, null));
}
/** @test */
public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$adapter->promise()->otherwise($this->expectCallableNever());
}
/** @test */
public function alwaysShouldReturnAPromiseForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->always(function () {}));
}
}

View File

@@ -0,0 +1,512 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise\Deferred;
use React\Promise\UnhandledRejectionException;
trait PromiseRejectedTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function rejectedPromiseShouldBeImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->reject(1);
$adapter->reject(2);
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function rejectedPromiseShouldInvokeNewlyAddedCallback()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject(1);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldForwardUndefinedRejectionValue()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(null);
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function () {
// Presence of rejection handler is enough to switch back
// to resolve mode, even though it returns undefined.
// The ONLY way to propagate a rejection is to re-throw or
// return a rejected promise;
}
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldSwitchFromErrbacksToCallbacksWhenErrbackDoesNotExplicitlyPropagate()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function ($val) {
return $val + 1;
}
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldSwitchFromErrbacksToCallbacksWhenErrbackReturnsAResolution()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function ($val) {
return \React\Promise\resolve($val + 1);
}
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldPropagateRejectionsWhenErrbackThrows()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$mock2
);
}
/** @test */
public function shouldPropagateRejectionsWhenErrbackReturnsARejection()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function ($val) {
return \React\Promise\reject($val + 1);
}
)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function doneShouldInvokeRejectionHandlerForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, $mock));
}
/** @test */
public function doneShouldThrowExceptionThrownByRejectionHandlerForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, function () {
throw new \Exception('UnhandledRejectionException');
}));
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectedWithNonExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done());
}
/** @test */
public function unhandledRejectionExceptionThrownByDoneHoldsRejectionValue()
{
$adapter = $this->getPromiseTestAdapter();
$expected = new \stdClass();
$adapter->reject($expected);
try {
$adapter->promise()->done();
} catch (UnhandledRejectionException $e) {
$this->assertSame($expected, $e->getReason());
return;
}
$this->fail();
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectionHandlerRejectsForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject();
}));
}
/** @test */
public function doneShouldThrowRejectionExceptionWhenRejectionHandlerRejectsWithExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject(new \Exception('UnhandledRejectionException'));
}));
}
/** @test */
public function doneShouldThrowExceptionProvidedAsRejectionValueForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->reject(new \Exception('UnhandledRejectionException'));
$this->assertNull($adapter->promise()->done());
}
/** @test */
public function doneShouldThrowWithDeepNestingPromiseChainsForRejectedPromise()
{
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$exception = new \Exception('UnhandledRejectionException');
$d = new Deferred();
$d->resolve();
$result = \React\Promise\resolve(\React\Promise\resolve($d->promise()->then(function () use ($exception) {
$d = new Deferred();
$d->resolve();
return \React\Promise\resolve($d->promise()->then(function () {}))->then(
function () use ($exception) {
throw $exception;
}
);
})));
$result->done();
}
/** @test */
public function doneShouldRecoverWhenRejectionHandlerCatchesExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject(new \Exception('UnhandledRejectionException'));
$this->assertNull($adapter->promise()->done(null, function (\Exception $e) {
}));
}
/** @test */
public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->reject(1);
$adapter->promise()->otherwise($mock);
}
/** @test */
public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->otherwise(function ($reason) use ($mock) {
$mock($reason);
});
}
/** @test */
public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \InvalidArgumentException();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->otherwise(function (\InvalidArgumentException $reason) use ($mock) {
$mock($reason);
});
}
/** @test */
public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehintForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->expectCallableNever();
$adapter->reject($exception);
$adapter->promise()
->otherwise(function (\InvalidArgumentException $reason) use ($mock) {
$mock($reason);
});
}
/** @test */
public function alwaysShouldNotSuppressRejectionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->always(function () {})
->then(null, $mock);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->always(function () {
return 1;
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception1 = new \Exception();
$exception2 = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception2));
$adapter->reject($exception1);
$adapter->promise()
->always(function () use ($exception2) {
throw $exception2;
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception1 = new \Exception();
$exception2 = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception2));
$adapter->reject($exception1);
$adapter->promise()
->always(function () use ($exception2) {
return \React\Promise\reject($exception2);
})
->then(null, $mock);
}
/** @test */
public function cancelShouldReturnNullForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function cancelShouldHaveNoEffectForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$adapter->reject();
$adapter->promise()->cancel();
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace React\Promise\PromiseTest;
trait PromiseSettledTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function thenShouldReturnAPromiseForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then());
}
/** @test */
public function thenShouldReturnAllowNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then(null, null, null));
}
/** @test */
public function cancelShouldReturnNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function cancelShouldHaveNoEffectForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$adapter->settle();
$adapter->promise()->cancel();
}
/** @test */
public function doneShouldReturnNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertNull($adapter->promise()->done(null, function () {}));
}
/** @test */
public function doneShouldReturnAllowNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertNull($adapter->promise()->done(null, function () {}, null));
}
/** @test */
public function progressShouldNotInvokeProgressHandlerForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$adapter->promise()->progress($this->expectCallableNever());
$adapter->notify();
}
/** @test */
public function alwaysShouldReturnAPromiseForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->always(function () {}));
}
}

View File

@@ -0,0 +1,368 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise;
use React\Promise\Deferred;
trait RejectTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function rejectShouldRejectWithAnImmediateValue()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->reject(1);
}
/** @test */
public function rejectShouldRejectWithFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->reject(Promise\resolve(1));
}
/** @test */
public function rejectShouldRejectWithRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->reject(Promise\reject(1));
}
/** @test */
public function rejectShouldForwardReasonWhenCallbackIsNull()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(
$this->expectCallableNever()
)
->then(
$this->expectCallableNever(),
$mock
);
$adapter->reject(1);
}
/** @test */
public function rejectShouldMakePromiseImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(null, function ($value) use ($adapter) {
$adapter->reject(3);
return Promise\reject($value);
})
->then(
$this->expectCallableNever(),
$mock
);
$adapter->reject(1);
$adapter->reject(2);
}
/** @test */
public function notifyShouldInvokeOtherwiseHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->otherwise($mock);
$adapter->reject(1);
}
/** @test */
public function doneShouldInvokeRejectionHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$this->assertNull($adapter->promise()->done(null, $mock));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowExceptionThrownByRejectionHandler()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, function () {
throw new \Exception('UnhandledRejectionException');
}));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectedWithNonException()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$this->assertNull($adapter->promise()->done());
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectionHandlerRejects()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject();
}));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowRejectionExceptionWhenRejectionHandlerRejectsWithException()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject(new \Exception('UnhandledRejectionException'));
}));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectionHandlerRetunsPendingPromiseWhichRejectsLater()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$d = new Deferred();
$promise = $d->promise();
$this->assertNull($adapter->promise()->done(null, function () use ($promise) {
return $promise;
}));
$adapter->reject(1);
$d->reject(1);
}
/** @test */
public function doneShouldThrowExceptionProvidedAsRejectionValue()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done());
$adapter->reject(new \Exception('UnhandledRejectionException'));
}
/** @test */
public function doneShouldThrowWithDeepNestingPromiseChains()
{
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$exception = new \Exception('UnhandledRejectionException');
$d = new Deferred();
$result = \React\Promise\resolve(\React\Promise\resolve($d->promise()->then(function () use ($exception) {
$d = new Deferred();
$d->resolve();
return \React\Promise\resolve($d->promise()->then(function () {}))->then(
function () use ($exception) {
throw $exception;
}
);
})));
$result->done();
$d->resolve();
}
/** @test */
public function doneShouldRecoverWhenRejectionHandlerCatchesException()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->done(null, function (\Exception $e) {
}));
$adapter->reject(new \Exception('UnhandledRejectionException'));
}
/** @test */
public function alwaysShouldNotSuppressRejection()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () {})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () {
return 1;
})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForRejection()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForRejection()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
return \React\Promise\reject($exception);
})
->then(null, $mock);
$adapter->reject($exception);
}
}

View File

@@ -0,0 +1,312 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise;
trait ResolveTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function resolveShouldResolve()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock);
$adapter->resolve(1);
}
/** @test */
public function resolveShouldResolveWithPromisedValue()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock);
$adapter->resolve(Promise\resolve(1));
}
/** @test */
public function resolveShouldRejectWhenResolvedWithRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->resolve(Promise\reject(1));
}
/** @test */
public function resolveShouldForwardValueWhenCallbackIsNull()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(
null,
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
$adapter->resolve(1);
}
/** @test */
public function resolveShouldMakePromiseImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(function ($value) use ($adapter) {
$adapter->resolve(3);
return $value;
})
->then(
$mock,
$this->expectCallableNever()
);
$adapter->resolve(1);
$adapter->resolve(2);
}
/**
* @test
*/
public function resolveShouldRejectWhenResolvedWithItself()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(new \LogicException('Cannot resolve a promise with itself.'));
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock
);
$adapter->resolve($adapter->promise());
}
/**
* @test
*/
public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself()
{
$adapter1 = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(new \LogicException('Cannot resolve a promise with itself.'));
$promise1 = $adapter1->promise();
$promise2 = $adapter2->promise();
$promise2->then(
$this->expectCallableNever(),
$mock
);
$adapter1->resolve($promise2);
$adapter2->resolve($promise1);
}
/** @test */
public function doneShouldInvokeFulfillmentHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$this->assertNull($adapter->promise()->done($mock));
$adapter->resolve(1);
}
/** @test */
public function doneShouldThrowExceptionThrownFulfillmentHandler()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(function () {
throw new \Exception('UnhandledRejectionException');
}));
$adapter->resolve(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenFulfillmentHandlerRejects()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(function () {
return \React\Promise\reject();
}));
$adapter->resolve(1);
}
/** @test */
public function alwaysShouldNotSuppressValue()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->promise()
->always(function () {})
->then($mock);
$adapter->resolve($value);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->promise()
->always(function () {
return 1;
})
->then($mock);
$adapter->resolve($value);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then($mock);
$adapter->resolve($value);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForFulfillment()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
$adapter->resolve(1);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForFulfillment()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
return \React\Promise\reject($exception);
})
->then(null, $mock);
$adapter->resolve(1);
}
}