You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
85 lines
2.3 KiB
|
9 years ago
|
<?php
|
||
|
|
|
||
|
|
namespace React\Promise;
|
||
|
|
|
||
|
|
use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
|
||
|
|
|
||
|
|
class PromiseTest extends TestCase
|
||
|
|
{
|
||
|
|
use PromiseTest\FullTestTrait;
|
||
|
|
|
||
|
|
public function getPromiseTestAdapter(callable $canceller = null)
|
||
|
|
{
|
||
|
|
$resolveCallback = $rejectCallback = $progressCallback = null;
|
||
|
|
|
||
|
|
$promise = new Promise(function ($resolve, $reject, $progress) use (&$resolveCallback, &$rejectCallback, &$progressCallback) {
|
||
|
|
$resolveCallback = $resolve;
|
||
|
|
$rejectCallback = $reject;
|
||
|
|
$progressCallback = $progress;
|
||
|
|
}, $canceller);
|
||
|
|
|
||
|
|
return new CallbackPromiseAdapter([
|
||
|
|
'promise' => function () use ($promise) {
|
||
|
|
return $promise;
|
||
|
|
},
|
||
|
|
'resolve' => $resolveCallback,
|
||
|
|
'reject' => $rejectCallback,
|
||
|
|
'notify' => $progressCallback,
|
||
|
|
'settle' => $resolveCallback,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @test */
|
||
|
|
public function shouldRejectIfResolverThrowsException()
|
||
|
|
{
|
||
|
|
$exception = new \Exception('foo');
|
||
|
|
|
||
|
|
$promise = new Promise(function () use ($exception) {
|
||
|
|
throw $exception;
|
||
|
|
});
|
||
|
|
|
||
|
|
$mock = $this->createCallableMock();
|
||
|
|
$mock
|
||
|
|
->expects($this->once())
|
||
|
|
->method('__invoke')
|
||
|
|
->with($this->identicalTo($exception));
|
||
|
|
|
||
|
|
$promise
|
||
|
|
->then($this->expectCallableNever(), $mock);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @test */
|
||
|
|
public function shouldFulfillIfFullfilledWithSimplePromise()
|
||
|
|
{
|
||
|
|
$adapter = $this->getPromiseTestAdapter();
|
||
|
|
|
||
|
|
$mock = $this->createCallableMock();
|
||
|
|
$mock
|
||
|
|
->expects($this->once())
|
||
|
|
->method('__invoke')
|
||
|
|
->with($this->identicalTo('foo'));
|
||
|
|
|
||
|
|
$adapter->promise()
|
||
|
|
->then($mock);
|
||
|
|
|
||
|
|
$adapter->resolve(new SimpleFulfilledTestPromise());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @test */
|
||
|
|
public function shouldRejectIfRejectedWithSimplePromise()
|
||
|
|
{
|
||
|
|
$adapter = $this->getPromiseTestAdapter();
|
||
|
|
|
||
|
|
$mock = $this->createCallableMock();
|
||
|
|
$mock
|
||
|
|
->expects($this->once())
|
||
|
|
->method('__invoke')
|
||
|
|
->with($this->identicalTo('foo'));
|
||
|
|
|
||
|
|
$adapter->promise()
|
||
|
|
->then($this->expectCallableNever(), $mock);
|
||
|
|
|
||
|
|
$adapter->resolve(new SimpleRejectedTestPromise());
|
||
|
|
}
|
||
|
|
}
|