123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- namespace React\Promise\PromiseTest;
- use Exception;
- use React\Promise\PromiseAdapter\PromiseAdapterInterface;
- use React\Promise\PromiseInterface;
- use stdClass;
- use function React\Promise\reject;
- use function React\Promise\resolve;
- trait PromiseFulfilledTestTrait
- {
- abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface;
- /** @test */
- public function fulfilledPromiseShouldBeImmutable(): void
- {
- $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(): void
- {
- $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(): void
- {
- $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(): void
- {
- $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(): void
- {
- $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 resolve($val + 1);
- },
- $this->expectCallableNever()
- )
- ->then(
- $mock,
- $this->expectCallableNever()
- );
- }
- /** @test */
- public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejection(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $exception = new Exception();
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo($exception));
- $adapter->resolve(1);
- $adapter->promise()
- ->then(
- function () use ($exception) {
- return reject($exception);
- },
- $this->expectCallableNever()
- )
- ->then(
- $this->expectCallableNever(),
- $mock
- );
- }
- /** @test */
- public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows(): void
- {
- $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
- * @requires PHP 8.1
- */
- public function thenShouldContinueToExecuteCallbacksWhenPriorCallbackSuspendsFiber(): void
- {
- /** @var PromiseAdapterInterface<int> $adapter */
- $adapter = $this->getPromiseTestAdapter();
- $adapter->resolve(42);
- $fiber = new \Fiber(function () use ($adapter) {
- $adapter->promise()->then(function (int $value) {
- \Fiber::suspend($value);
- });
- });
- $ret = $fiber->start();
- $this->assertEquals(42, $ret);
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo(42));
- $adapter->promise()->then($mock);
- }
- /** @test */
- public function cancelShouldHaveNoEffectForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
- $adapter->resolve(null);
- $adapter->promise()->cancel();
- }
- /** @test */
- public function catchShouldNotInvokeRejectionHandlerForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $adapter->resolve(1);
- $adapter->promise()->catch($this->expectCallableNever());
- }
- /** @test */
- public function finallyShouldNotSuppressValueForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $value = new stdClass();
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo($value));
- $adapter->resolve($value);
- $adapter->promise()
- ->finally(function () {})
- ->then($mock);
- }
- /** @test */
- public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $value = new stdClass();
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo($value));
- $adapter->resolve($value);
- $adapter->promise()
- ->finally(function (): int { // @phpstan-ignore-line
- return 1;
- })
- ->then($mock);
- }
- /** @test */
- public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $value = new stdClass();
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo($value));
- $adapter->resolve($value);
- $adapter->promise()
- ->finally(function (): PromiseInterface { // @phpstan-ignore-line
- return resolve(1);
- })
- ->then($mock);
- }
- /** @test */
- public function finallyShouldRejectWhenHandlerThrowsForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $exception = new Exception();
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo($exception));
- $adapter->resolve(1);
- $adapter->promise()
- ->finally(function () use ($exception) {
- throw $exception;
- })
- ->then(null, $mock);
- }
- /** @test */
- public function finallyShouldRejectWhenHandlerRejectsForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $exception = new Exception();
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($this->identicalTo($exception));
- $adapter->resolve(1);
- $adapter->promise()
- ->finally(function () use ($exception) {
- return reject($exception);
- })
- ->then(null, $mock);
- }
- /**
- * @test
- * @deprecated
- */
- public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise(): void
- {
- $adapter = $this->getPromiseTestAdapter();
- $adapter->resolve(1);
- $adapter->promise()->otherwise($this->expectCallableNever());
- }
- /**
- * @test
- * @deprecated
- */
- public function alwaysShouldNotSuppressValueForFulfilledPromise(): void
- {
- $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
- * @deprecated
- */
- public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise(): void
- {
- $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 (): int { // @phpstan-ignore-line
- return 1;
- })
- ->then($mock);
- }
- /**
- * @test
- * @deprecated
- */
- public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise(): void
- {
- $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 (): PromiseInterface { // @phpstan-ignore-line
- return resolve(1);
- })
- ->then($mock);
- }
- /**
- * @test
- * @deprecated
- */
- public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise(): void
- {
- $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
- * @deprecated
- */
- public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise(): void
- {
- $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 reject($exception);
- })
- ->then(null, $mock);
- }
- }
|