2016-12-02 14:43:54 +03:00
|
|
|
<?php
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-12-02 14:43:54 +03:00
|
|
|
namespace GraphQL\Tests\Executor\Promise;
|
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
use Exception;
|
2016-12-02 14:43:54 +03:00
|
|
|
use GraphQL\Executor\Promise\Adapter\SyncPromise;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\Error\Error;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-09-26 12:07:23 +03:00
|
|
|
use Throwable;
|
2018-09-01 18:07:06 +03:00
|
|
|
use function uniqid;
|
2016-12-02 14:43:54 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class SyncPromiseTest extends TestCase
|
2016-12-02 14:43:54 +03:00
|
|
|
{
|
|
|
|
public function getFulfilledPromiseResolveData()
|
|
|
|
{
|
2018-09-26 12:07:23 +03:00
|
|
|
$onFulfilledReturnsNull = static function () {
|
2016-12-02 14:43:54 +03:00
|
|
|
return null;
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onFulfilledReturnsSameValue = static function ($value) {
|
2016-12-02 14:43:54 +03:00
|
|
|
return $value;
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onFulfilledReturnsOtherValue = static function ($value) {
|
2016-12-02 14:43:54 +03:00
|
|
|
return 'other-' . $value;
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onFulfilledThrows = static function ($value) {
|
|
|
|
throw new Exception('onFulfilled throws this!');
|
2016-12-02 14:43:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
// $resolvedValue, $onFulfilled, $expectedNextValue, $expectedNextReason, $expectedNextState
|
|
|
|
['test-value', null, 'test-value', null, SyncPromise::FULFILLED],
|
|
|
|
[uniqid(), $onFulfilledReturnsNull, null, null, SyncPromise::FULFILLED],
|
|
|
|
['test-value', $onFulfilledReturnsSameValue, 'test-value', null, SyncPromise::FULFILLED],
|
|
|
|
['test-value-2', $onFulfilledReturnsOtherValue, 'other-test-value-2', null, SyncPromise::FULFILLED],
|
2018-09-01 18:07:06 +03:00
|
|
|
['test-value-3', $onFulfilledThrows, null, 'onFulfilled throws this!', SyncPromise::REJECTED],
|
2016-12-02 14:43:54 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getFulfilledPromiseResolveData
|
|
|
|
*/
|
2017-12-21 09:52:43 +03:00
|
|
|
public function testFulfilledPromiseCannotChangeValue(
|
2016-12-02 14:43:54 +03:00
|
|
|
$resolvedValue,
|
|
|
|
$onFulfilled,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextState
|
2018-09-01 18:07:06 +03:00
|
|
|
) {
|
2016-12-02 14:43:54 +03:00
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$promise->resolve($resolvedValue);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::FULFILLED, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$this->expectException(Throwable::class);
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectExceptionMessage('Cannot change value of fulfilled promise');
|
2017-12-21 09:52:43 +03:00
|
|
|
$promise->resolve($resolvedValue . '-other-value');
|
|
|
|
}
|
2016-12-02 14:43:54 +03:00
|
|
|
|
2017-12-21 09:52:43 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider getFulfilledPromiseResolveData
|
|
|
|
*/
|
|
|
|
public function testFulfilledPromiseCannotBeRejected(
|
|
|
|
$resolvedValue,
|
|
|
|
$onFulfilled,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextState
|
2018-09-01 18:07:06 +03:00
|
|
|
) {
|
2017-12-21 09:52:43 +03:00
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
|
|
|
$promise->resolve($resolvedValue);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::FULFILLED, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$this->expectException(Throwable::class);
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectExceptionMessage('Cannot reject fulfilled promise');
|
2018-09-26 12:07:23 +03:00
|
|
|
$promise->reject(new Exception('anything'));
|
2017-12-21 09:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getFulfilledPromiseResolveData
|
|
|
|
*/
|
|
|
|
public function testFulfilledPromise(
|
|
|
|
$resolvedValue,
|
|
|
|
$onFulfilled,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextState
|
2018-09-01 18:07:06 +03:00
|
|
|
) {
|
2017-12-21 09:52:43 +03:00
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
|
|
|
$promise->resolve($resolvedValue);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::FULFILLED, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
$nextPromise = $promise->then(
|
|
|
|
null,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function () {
|
2018-09-01 18:07:06 +03:00
|
|
|
}
|
|
|
|
);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($promise, $nextPromise);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$onRejectedCalled = false;
|
2018-09-01 18:07:06 +03:00
|
|
|
$nextPromise = $promise->then(
|
|
|
|
$onFulfilled,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function () use (&$onRejectedCalled) {
|
2018-09-01 18:07:06 +03:00
|
|
|
$onRejectedCalled = true;
|
|
|
|
}
|
|
|
|
);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
if ($onFulfilled) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertNotSame($promise, $nextPromise);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
} else {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::FULFILLED, $nextPromise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(false, $onRejectedCalled);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($nextPromise, $expectedNextReason, $expectedNextValue, $expectedNextState);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$nextPromise2 = $promise->then($onFulfilled);
|
|
|
|
$nextPromise3 = $promise->then($onFulfilled);
|
|
|
|
|
|
|
|
if ($onFulfilled) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertNotSame($nextPromise, $nextPromise2);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SyncPromise::runQueue();
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($nextPromise2, $expectedNextReason, $expectedNextValue, $expectedNextState);
|
|
|
|
self::assertValidPromise($nextPromise3, $expectedNextReason, $expectedNextValue, $expectedNextState);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
private static function assertValidPromise(
|
2018-09-01 18:07:06 +03:00
|
|
|
SyncPromise $promise,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextState
|
2018-09-19 18:12:09 +03:00
|
|
|
) : void {
|
2018-09-01 18:07:06 +03:00
|
|
|
$actualNextValue = null;
|
|
|
|
$actualNextReason = null;
|
|
|
|
$onFulfilledCalled = false;
|
|
|
|
$onRejectedCalled = false;
|
|
|
|
|
|
|
|
$promise->then(
|
2018-09-19 18:12:09 +03:00
|
|
|
static function ($nextValue) use (&$actualNextValue, &$onFulfilledCalled) {
|
2018-09-01 18:07:06 +03:00
|
|
|
$onFulfilledCalled = true;
|
|
|
|
$actualNextValue = $nextValue;
|
|
|
|
},
|
2018-09-26 12:07:23 +03:00
|
|
|
static function (Throwable $reason) use (&$actualNextReason, &$onRejectedCalled) {
|
2018-09-01 18:07:06 +03:00
|
|
|
$onRejectedCalled = true;
|
|
|
|
$actualNextReason = $reason->getMessage();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($onFulfilledCalled, false);
|
|
|
|
self::assertEquals($onRejectedCalled, false);
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
SyncPromise::runQueue();
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(! $expectedNextReason, $onFulfilledCalled);
|
|
|
|
self::assertEquals(! ! $expectedNextReason, $onRejectedCalled);
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expectedNextValue, $actualNextValue);
|
|
|
|
self::assertEquals($expectedNextReason, $actualNextReason);
|
|
|
|
self::assertEquals($expectedNextState, $promise->state);
|
2018-09-01 18:07:06 +03:00
|
|
|
}
|
|
|
|
|
2016-12-02 14:43:54 +03:00
|
|
|
public function getRejectedPromiseData()
|
|
|
|
{
|
2018-09-26 12:07:23 +03:00
|
|
|
$onRejectedReturnsNull = static function () {
|
2016-12-02 14:43:54 +03:00
|
|
|
return null;
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onRejectedReturnsSomeValue = static function ($reason) {
|
2016-12-02 14:43:54 +03:00
|
|
|
return 'some-value';
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onRejectedThrowsSameReason = static function ($reason) {
|
2016-12-02 14:43:54 +03:00
|
|
|
throw $reason;
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onRejectedThrowsOtherReason = static function ($value) {
|
|
|
|
throw new Exception('onRejected throws other!');
|
2016-12-02 14:43:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
// $rejectedReason, $onRejected, $expectedNextValue, $expectedNextReason, $expectedNextState
|
2018-09-26 12:07:23 +03:00
|
|
|
[new Exception('test-reason'), null, null, 'test-reason', SyncPromise::REJECTED],
|
|
|
|
[new Exception('test-reason-2'), $onRejectedReturnsNull, null, null, SyncPromise::FULFILLED],
|
|
|
|
[new Exception('test-reason-3'), $onRejectedReturnsSomeValue, 'some-value', null, SyncPromise::FULFILLED],
|
|
|
|
[new Exception('test-reason-4'), $onRejectedThrowsSameReason, null, 'test-reason-4', SyncPromise::REJECTED],
|
|
|
|
[new Exception('test-reason-5'), $onRejectedThrowsOtherReason, null, 'onRejected throws other!', SyncPromise::REJECTED],
|
2016-12-02 14:43:54 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-12-21 09:52:43 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider getRejectedPromiseData
|
|
|
|
*/
|
|
|
|
public function testRejectedPromiseCannotChangeReason(
|
|
|
|
$rejectedReason,
|
|
|
|
$onRejected,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextState
|
2018-09-01 18:07:06 +03:00
|
|
|
) {
|
2017-12-21 09:52:43 +03:00
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
|
|
|
$promise->reject($rejectedReason);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::REJECTED, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$this->expectException(Throwable::class);
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectExceptionMessage('Cannot change rejection reason');
|
2018-09-26 12:07:23 +03:00
|
|
|
$promise->reject(new Exception('other-reason'));
|
2017-12-21 09:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getRejectedPromiseData
|
|
|
|
*/
|
|
|
|
public function testRejectedPromiseCannotBeResolved(
|
|
|
|
$rejectedReason,
|
|
|
|
$onRejected,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextState
|
2018-09-01 18:07:06 +03:00
|
|
|
) {
|
2017-12-21 09:52:43 +03:00
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
|
|
|
$promise->reject($rejectedReason);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::REJECTED, $promise->state);
|
2017-12-21 09:52:43 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$this->expectException(Throwable::class);
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectExceptionMessage('Cannot resolve rejected promise');
|
2017-12-21 09:52:43 +03:00
|
|
|
$promise->resolve('anything');
|
|
|
|
}
|
|
|
|
|
2016-12-02 14:43:54 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider getRejectedPromiseData
|
|
|
|
*/
|
|
|
|
public function testRejectedPromise(
|
|
|
|
$rejectedReason,
|
|
|
|
$onRejected,
|
|
|
|
$expectedNextValue,
|
|
|
|
$expectedNextReason,
|
|
|
|
$expectedNextState
|
2018-09-01 18:07:06 +03:00
|
|
|
) {
|
2016-12-02 14:43:54 +03:00
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$promise->reject($rejectedReason);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::REJECTED, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
try {
|
2018-09-26 12:07:23 +03:00
|
|
|
$promise->reject(new Exception('other-reason'));
|
2018-10-09 17:51:23 +03:00
|
|
|
self::fail('Expected exception not thrown');
|
2018-09-26 12:07:23 +03:00
|
|
|
} catch (Throwable $e) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals('Cannot change rejection reason', $e->getMessage());
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$promise->resolve('anything');
|
2018-10-09 17:51:23 +03:00
|
|
|
self::fail('Expected exception not thrown');
|
2018-09-26 12:07:23 +03:00
|
|
|
} catch (Throwable $e) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals('Cannot resolve rejected promise', $e->getMessage());
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
$nextPromise = $promise->then(
|
2018-09-26 12:07:23 +03:00
|
|
|
static function () {
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($promise, $nextPromise);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$onFulfilledCalled = false;
|
2018-09-01 18:07:06 +03:00
|
|
|
$nextPromise = $promise->then(
|
2018-09-26 12:07:23 +03:00
|
|
|
static function () use (&$onFulfilledCalled) {
|
2016-12-02 14:43:54 +03:00
|
|
|
$onFulfilledCalled = true;
|
|
|
|
},
|
|
|
|
$onRejected
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($onRejected) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertNotSame($promise, $nextPromise);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
} else {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::REJECTED, $nextPromise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(false, $onFulfilledCalled);
|
|
|
|
self::assertValidPromise($nextPromise, $expectedNextReason, $expectedNextValue, $expectedNextState);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$nextPromise2 = $promise->then(null, $onRejected);
|
|
|
|
$nextPromise3 = $promise->then(null, $onRejected);
|
|
|
|
|
|
|
|
if ($onRejected) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertNotSame($nextPromise, $nextPromise2);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SyncPromise::runQueue();
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($nextPromise2, $expectedNextReason, $expectedNextValue, $expectedNextState);
|
|
|
|
self::assertValidPromise($nextPromise3, $expectedNextReason, $expectedNextValue, $expectedNextState);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPendingPromise() : void
|
2016-12-02 14:43:54 +03:00
|
|
|
{
|
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$promise->resolve($promise);
|
2018-10-09 17:51:23 +03:00
|
|
|
self::fail('Expected exception not thrown');
|
2018-09-26 12:07:23 +03:00
|
|
|
} catch (Throwable $e) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals('Cannot resolve promise with self', $e->getMessage());
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to resolve with other promise (must resolve when other promise resolves)
|
|
|
|
$otherPromise = new SyncPromise();
|
|
|
|
$promise->resolve($otherPromise);
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $otherPromise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$otherPromise->resolve('the value');
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::FULFILLED, $otherPromise->state);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
|
|
|
self::assertValidPromise($promise, null, 'the value', SyncPromise::FULFILLED);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$promise = new SyncPromise();
|
|
|
|
$promise->resolve('resolved!');
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($promise, null, 'resolved!', SyncPromise::FULFILLED);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
// Test rejections
|
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$promise->reject('a');
|
2018-10-09 17:51:23 +03:00
|
|
|
self::fail('Expected exception not thrown');
|
2018-07-29 18:43:10 +03:00
|
|
|
} catch (Error $e) {
|
2017-06-06 13:55:38 +03:00
|
|
|
throw $e;
|
2018-09-26 12:07:23 +03:00
|
|
|
} catch (Throwable $e) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$promise->reject(new Exception('Rejected Reason'));
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($promise, 'Rejected Reason', null, SyncPromise::REJECTED);
|
2016-12-02 22:48:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
$promise = new SyncPromise();
|
|
|
|
$promise2 = $promise->then(
|
|
|
|
null,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function () {
|
2018-09-01 18:07:06 +03:00
|
|
|
return 'value';
|
|
|
|
}
|
|
|
|
);
|
2018-09-26 12:07:23 +03:00
|
|
|
$promise->reject(new Exception('Rejected Again'));
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($promise2, null, 'value', SyncPromise::FULFILLED);
|
2016-12-02 22:48:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
$promise = new SyncPromise();
|
2016-12-02 22:48:51 +03:00
|
|
|
$promise2 = $promise->then();
|
2018-09-26 12:07:23 +03:00
|
|
|
$promise->reject(new Exception('Rejected Once Again'));
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidPromise($promise2, 'Rejected Once Again', null, SyncPromise::REJECTED);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPendingPromiseThen() : void
|
2016-12-02 14:43:54 +03:00
|
|
|
{
|
|
|
|
$promise = new SyncPromise();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
$nextPromise = $promise->then();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertNotSame($promise, $nextPromise);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $promise->state);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
// Make sure that it queues derivative promises until resolution:
|
|
|
|
$onFulfilledCount = 0;
|
2018-09-01 18:07:06 +03:00
|
|
|
$onRejectedCount = 0;
|
2018-09-26 12:07:23 +03:00
|
|
|
$onFulfilled = static function ($value) use (&$onFulfilledCount) {
|
2016-12-02 14:43:54 +03:00
|
|
|
$onFulfilledCount++;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-02 14:43:54 +03:00
|
|
|
return $onFulfilledCount;
|
|
|
|
};
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$onRejected = static function ($reason) use (&$onRejectedCount) {
|
2016-12-02 14:43:54 +03:00
|
|
|
$onRejectedCount++;
|
|
|
|
throw $reason;
|
|
|
|
};
|
|
|
|
|
|
|
|
$nextPromise2 = $promise->then($onFulfilled, $onRejected);
|
|
|
|
$nextPromise3 = $promise->then($onFulfilled, $onRejected);
|
|
|
|
$nextPromise4 = $promise->then($onFulfilled, $onRejected);
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::getQueue()->count(), 0);
|
|
|
|
self::assertEquals($onFulfilledCount, 0);
|
|
|
|
self::assertEquals($onRejectedCount, 0);
|
2016-12-02 14:43:54 +03:00
|
|
|
$promise->resolve(1);
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::getQueue()->count(), 4);
|
|
|
|
self::assertEquals($onFulfilledCount, 0);
|
|
|
|
self::assertEquals($onRejectedCount, 0);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise->state);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise2->state);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise3->state);
|
|
|
|
self::assertEquals(SyncPromise::PENDING, $nextPromise4->state);
|
2016-12-02 14:43:54 +03:00
|
|
|
|
|
|
|
SyncPromise::runQueue();
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(SyncPromise::getQueue()->count(), 0);
|
|
|
|
self::assertEquals($onFulfilledCount, 3);
|
|
|
|
self::assertEquals($onRejectedCount, 0);
|
|
|
|
self::assertValidPromise($nextPromise, null, 1, SyncPromise::FULFILLED);
|
|
|
|
self::assertValidPromise($nextPromise2, null, 1, SyncPromise::FULFILLED);
|
|
|
|
self::assertValidPromise($nextPromise3, null, 2, SyncPromise::FULFILLED);
|
|
|
|
self::assertValidPromise($nextPromise4, null, 3, SyncPromise::FULFILLED);
|
2016-12-02 14:43:54 +03:00
|
|
|
}
|
|
|
|
}
|