From 44fa49a4c730c0010d594cac0a124fe27e0bca88 Mon Sep 17 00:00:00 2001 From: awohsen Date: Fri, 6 Oct 2023 22:32:33 +0330 Subject: [PATCH] Loop::get() to prevent loop duplication - As there should be only one main loop in ReactPHP projects --- src/AssertsPromise.php | 56 +++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/AssertsPromise.php b/src/AssertsPromise.php index a015cd7..a5a49f1 100644 --- a/src/AssertsPromise.php +++ b/src/AssertsPromise.php @@ -5,7 +5,7 @@ use Clue\React\Block; use Exception; use PHPUnit\Framework\AssertionFailedError; -use React\EventLoop\Factory as LoopFactory; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\PromiseInterface; use React\Promise\Timer\TimeoutException; @@ -28,9 +28,9 @@ public function assertPromiseFulfills(PromiseInterface $promise, int $timeout = try { $this->waitForPromise($promise, $timeout); - } catch (TimeoutException $exception) { + } catch (TimeoutException) { $this->fail($failMessage . 'Promise was cancelled due to timeout.'); - } catch (Exception $exception) { + } catch (Exception) { $this->fail($failMessage . 'Promise was rejected.'); } } @@ -41,7 +41,7 @@ public function assertPromiseFulfills(PromiseInterface $promise, int $timeout = * @param int|null $timeout * @throws AssertionFailedError */ - public function assertPromiseFulfillsWith(PromiseInterface $promise, $value, int $timeout = null): void + public function assertPromiseFulfillsWith(PromiseInterface $promise, mixed $value, int $timeout = null): void { $failMessage = 'Failed asserting that promise fulfills with a specified value. '; $result = null; @@ -49,9 +49,9 @@ public function assertPromiseFulfillsWith(PromiseInterface $promise, $value, int try { $result = $this->waitForPromise($promise, $timeout); - } catch (TimeoutException $exception) { + } catch (TimeoutException) { $this->fail($failMessage . 'Promise was cancelled due to timeout.'); - } catch (Exception $exception) { + } catch (Exception) { $this->fail($failMessage . 'Promise was rejected.'); } @@ -69,9 +69,9 @@ public function assertPromiseFulfillsWithInstanceOf(PromiseInterface $promise, s try { $result = $this->waitForPromise($promise, $timeout); - } catch (TimeoutException $exception) { + } catch (TimeoutException) { $this->fail($failMessage . 'Promise was cancelled due to timeout.'); - } catch (Exception $exception) { + } catch (Exception) { $this->fail($failMessage . 'Promise was rejected.'); } @@ -89,7 +89,7 @@ public function assertPromiseRejects(PromiseInterface $promise, int $timeout = n try { $this->waitForPromise($promise, $timeout); - } catch (Exception $exception) { + } catch (Exception) { return; } @@ -115,10 +115,11 @@ public function assertPromiseRejectsWith(PromiseInterface $promise, string $reas } /** - * @throws Exception + * @param PromiseInterface $promise + * @param int|null $timeout * @return mixed */ - public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null) + public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null): mixed { try { return $this->waitForPromise($promise, $timeout); @@ -129,10 +130,12 @@ public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout } /** + * @param PromiseInterface $promise + * @param int|null $timeout * @return mixed * @throws Exception */ - public function waitForPromise(PromiseInterface $promise, int $timeout = null) + public function waitForPromise(PromiseInterface $promise, int $timeout = null): mixed { return Block\await($promise, $this->eventLoop(), $timeout ?: $this->defaultWaitTimeout); } @@ -140,7 +143,7 @@ public function waitForPromise(PromiseInterface $promise, int $timeout = null) public function eventLoop(): LoopInterface { if (! $this->loop) { - $this->loop = LoopFactory::create(); + $this->loop = Loop::get(); } return $this->loop; @@ -154,9 +157,10 @@ public function eventLoop(): LoopInterface */ public function assertTrueAboutPromise( PromiseInterface $promise, - callable $predicate, - int $timeout = null - ): void { + callable $predicate, + int $timeout = null + ): void + { $this->assertAboutPromise($promise, $predicate, $timeout); } @@ -168,9 +172,10 @@ public function assertTrueAboutPromise( */ public function assertFalseAboutPromise( PromiseInterface $promise, - callable $predicate, - int $timeout = null - ): void { + callable $predicate, + int $timeout = null + ): void + { $this->assertAboutPromise($promise, $predicate, $timeout, false); } @@ -183,18 +188,19 @@ public function assertFalseAboutPromise( */ private function assertAboutPromise( PromiseInterface $promise, - callable $predicate, - int $timeout = null, - bool $assertTrue = true - ): void { + callable $predicate, + int $timeout = null, + bool $assertTrue = true + ): void + { $result = $assertTrue ? false : true; $this->addToAssertionCount(1); try { $result = $predicate($this->waitForPromise($promise, $timeout)); - } catch (TimeoutException $exception) { + } catch (TimeoutException) { $this->fail('Promise was cancelled due to timeout'); - } catch (Exception $exception) { + } catch (Exception) { $this->fail('Failed asserting that promise was fulfilled. Promise was rejected'); }