123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace React\Promise;
- use Exception;
- use React\Promise\Exception\CompositeException;
- use React\Promise\Exception\LengthException;
- class FunctionAnyTest extends TestCase
- {
- /** @test */
- public function shouldRejectWithLengthExceptionWithEmptyInputArray(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(
- self::callback(function ($exception) {
- return $exception instanceof LengthException &&
- 'Must contain at least 1 item but contains only 0 items.' === $exception->getMessage();
- })
- );
- any([])
- ->then($this->expectCallableNever(), $mock);
- }
- /** @test */
- public function shouldRejectWithLengthExceptionWithEmptyInputGenerator(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(new LengthException('Must contain at least 1 item but contains only 0 items.'));
- $gen = (function () {
- if (false) { // @phpstan-ignore-line
- yield;
- }
- })();
- any($gen)->then($this->expectCallableNever(), $mock);
- }
- /** @test */
- public function shouldResolveWithAnInputValue(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(1));
- any([1, 2, 3])
- ->then($mock);
- }
- /** @test */
- public function shouldResolveWithAPromisedInputValue(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(1));
- any([resolve(1), resolve(2), resolve(3)])
- ->then($mock);
- }
- /** @test */
- public function shouldResolveWithAnInputValueFromDeferred(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(1));
- $deferred = new Deferred();
- any([$deferred->promise()])->then($mock);
- $deferred->resolve(1);
- }
- /** @test */
- public function shouldResolveValuesGenerator(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(1));
- $gen = (function () {
- for ($i = 1; $i <= 3; ++$i) {
- yield $i;
- }
- })();
- any($gen)->then($mock);
- }
- /** @test */
- public function shouldResolveValuesInfiniteGenerator(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(1));
- $gen = (function () {
- for ($i = 1; ; ++$i) {
- yield $i;
- }
- })();
- any($gen)->then($mock);
- }
- /** @test */
- public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected(): void
- {
- $exception1 = new Exception();
- $exception2 = new Exception();
- $exception3 = new Exception();
- $compositeException = new CompositeException(
- [0 => $exception1, 1 => $exception2, 2 => $exception3],
- 'All promises rejected.'
- );
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with($compositeException);
- any([reject($exception1), reject($exception2), reject($exception3)])
- ->then($this->expectCallableNever(), $mock);
- }
- /** @test */
- public function shouldRejectWithAllRejectedInputValuesIfInputIsRejectedFromDeferred(): void
- {
- $exception = new Exception();
- $compositeException = new CompositeException(
- [2 => $exception],
- 'All promises rejected.'
- );
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with($compositeException);
- $deferred = new Deferred();
- any([2 => $deferred->promise()])->then($this->expectCallableNever(), $mock);
- $deferred->reject($exception);
- }
- /** @test */
- public function shouldResolveWhenFirstInputPromiseResolves(): void
- {
- $exception2 = new Exception();
- $exception3 = new Exception();
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(1));
- any([resolve(1), reject($exception2), reject($exception3)])
- ->then($mock);
- }
- /** @test */
- public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue(): void
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects(self::once())
- ->method('__invoke')
- ->with(self::identicalTo(2));
- $d1 = new Deferred();
- $d2 = new Deferred();
- any(['abc' => $d1->promise(), 1 => $d2->promise()])
- ->then($mock);
- $d2->resolve(2);
- $d1->resolve(1);
- }
- /** @test */
- public function shouldCancelInputArrayPromises(): void
- {
- $promise1 = new Promise(function () {}, $this->expectCallableOnce());
- $promise2 = new Promise(function () {}, $this->expectCallableOnce());
- any([$promise1, $promise2])->cancel();
- }
- /** @test */
- public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills(): void
- {
- $deferred = new Deferred($this->expectCallableNever());
- $deferred->resolve(null);
- $promise2 = new Promise(function () {}, $this->expectCallableNever());
- any([$deferred->promise(), $promise2])->cancel();
- }
- }
|