From 931ef9ad6f0fccfe763a8fbe90a80053f48d5b45 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 13 Nov 2022 23:07:05 +0100 Subject: [PATCH] Clean up event loop left overs from promise I somehow missed those while working on https://github.com/php-http/react-adapter/pull/50 --- src/Client.php | 1 - src/Promise.php | 16 +++------------- tests/PromiseTest.php | 18 ++++-------------- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/src/Client.php b/src/Client.php index fc11669..002ea8b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -69,7 +69,6 @@ public function sendAsyncRequest(RequestInterface $request) $request->getHeaders(), $request->getBody() ), - $this->loop, $request ); diff --git a/src/Promise.php b/src/Promise.php index c486888..c419a71 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -10,9 +10,7 @@ use function React\Async\await; -use React\EventLoop\LoopInterface; use React\Promise\PromiseInterface; -use RuntimeException; /** * React promise adapter implementation. @@ -58,19 +56,11 @@ final class Promise implements HttpPromise */ private $promise; - /** - * ReactPHP LoopInterface. - * - * @var LoopInterface - */ - private $loop; - - public function __construct(PromiseInterface $promise, LoopInterface $loop, RequestInterface $request) + public function __construct(PromiseInterface $promise, RequestInterface $request) { $this->state = self::PENDING; $this->request = $request; - $this->loop = $loop; $this->promise = $promise->then( function (?ResponseInterface $response): ?ResponseInterface { $this->response = $response; @@ -86,7 +76,7 @@ function ($reason): void { if ($reason instanceof HttplugException) { $this->exception = $reason; - } elseif ($reason instanceof RuntimeException) { + } elseif ($reason instanceof \RuntimeException) { $this->exception = new HttplugException\NetworkException($reason->getMessage(), $this->request, $reason); } elseif ($reason instanceof \Throwable) { $this->exception = new HttplugException\TransferException('Invalid exception returned from ReactPHP', 0, $reason); @@ -101,7 +91,7 @@ function ($reason): void { public function then(?callable $onFulfilled = null, ?callable $onRejected = null) { - return new self($this->promise->then($onFulfilled, $onRejected), $this->loop, $this->request); + return new self($this->promise->then($onFulfilled, $onRejected), $this->request); } /** diff --git a/tests/PromiseTest.php b/tests/PromiseTest.php index 288630c..071677a 100644 --- a/tests/PromiseTest.php +++ b/tests/PromiseTest.php @@ -4,27 +4,17 @@ use Http\Adapter\React\Exception\UnexpectedValueException; use Http\Adapter\React\Promise; -use Http\Adapter\React\ReactFactory; use Http\Client\Exception\HttpException; use Http\Client\Exception\NetworkException; use Http\Client\Exception\TransferException; -use InvalidArgumentException; use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use React\Promise\Promise as ReactPromise; -use RuntimeException; class PromiseTest extends TestCase { - private $loop; - - public function setUp(): void - { - $this->loop = ReactFactory::buildEventLoop(); - } - public function testChain() { $factory = new Psr17Factory(); @@ -35,7 +25,7 @@ public function testChain() $resolve($response); }); - $promise = new Promise($reactPromise, $this->loop, $request); + $promise = new Promise($reactPromise, $request); $lastPromise = $promise->then(function (ResponseInterface $response) use ($factory) { return $factory->createResponse(300, $response->getReasonPhrase()); @@ -60,7 +50,7 @@ public function testPromiseExceptionsAreTranslatedToHttplug( $reject($reason); }); - $promise = new Promise($reactPromise, $this->loop, $request); + $promise = new Promise($reactPromise, $request); $this->expectException($adapterExceptionClass); $promise->wait(); } @@ -72,8 +62,8 @@ public function exceptionThatIsThrownFromReactProvider() return [ 'string' => [$request, 'whatever', UnexpectedValueException::class], - 'InvalidArgumentException' => [$request, new InvalidArgumentException('Something went wrong'), TransferException::class], - 'RuntimeException' => [$request, new RuntimeException('Something happened inside ReactPHP engine'), NetworkException::class], + 'InvalidArgumentException' => [$request, new \InvalidArgumentException('Something went wrong'), TransferException::class], + 'RuntimeException' => [$request, new \RuntimeException('Something happened inside ReactPHP engine'), NetworkException::class], 'NetworkException' => [$request, new NetworkException('Something happened inside ReactPHP engine', $request), NetworkException::class], 'HttpException' => [$request, new HttpException('Something happened inside ReactPHP engine', $request, $response), HttpException::class], ];