diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cf214c8..06c449c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,10 +7,15 @@ on: jobs: PHPUnit: name: PHPUnit (PHP ${{ matrix.php }}) - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 strategy: matrix: php: + - 8.5 + - 8.4 + - 8.3 + - 8.2 + - 8.1 - 8.0 - 7.4 - 7.3 @@ -22,25 +27,32 @@ jobs: - 5.4 - 5.3 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} coverage: xdebug - run: composer install - - run: vendor/bin/phpunit --coverage-text - if: ${{ matrix.php >= 7.3 }} - - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy - if: ${{ matrix.php < 7.3 }} + - run: vendor/bin/phpunit --coverage-text --coverage-clover clover.xml ${{ matrix.php < 7.3 && '-c phpunit.xml.legacy' || '' }} + - name: Check 100% code coverage + shell: php {0} + run: | + project->metrics; + exit((int) $metrics['statements'] === (int) $metrics['coveredstatements'] ? 0 : 1); PHPUnit-hhvm: name: PHPUnit (HHVM) - runs-on: ubuntu-18.04 + runs-on: ubuntu-24.04 continue-on-error: true steps: - - uses: actions/checkout@v2 - - uses: azjezz/setup-hhvm@v1 + - uses: actions/checkout@v6 + - run: cp "$(which composer)" composer.phar && ./composer.phar self-update --2.2 # downgrade Composer for HHVM + - name: Run hhvm composer.phar install + uses: docker://hhvm/hhvm:3.30-lts-latest with: - version: lts-3.30 - - run: hhvm $(which composer) install - - run: hhvm vendor/bin/phpunit + args: hhvm composer.phar install + - name: Run hhvm vendor/bin/phpunit + uses: docker://hhvm/hhvm:3.30-lts-latest + with: + args: hhvm vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fa95a1..46b3d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,69 @@ # Changelog +## 1.10.0 (2026-06-29) + +* Feature: Remove abandoned `ringcentral/psr7` dependency to support PSR-7 v2 (`psr/http-message`). + (#65 by @kwhat and @clue) + +* Feature: Improve PHP 8.5+ and PHP 8.4+ support and update test environment. + (#59, #60 and #67 by @clue) + +* Improve test suite to ensure 100% code coverage and support legacy PHP 7.2 with PHPUnit 8.5. + (#66 and #68 by @clue) + +## 1.9.0 (2024-04-10) + +* Feature / Fix: Forward compatibility with Promise v3. + (#56 by @SimonFrings) + +* Feature: Full PHP 8.3 compatibility. + (#54 by @yadaiio) + +* Minor documentation improvements. + (#53 and #55 by @yadaiio) + +* Update test suite to use new reactphp/async package instead of clue/reactphp-block. + (#50 and #57 by @dinooo13 and @SimonFrings) + +## 1.8.0 (2022-09-01) + +* Feature: Full support for PHP 8.1 and PHP 8.2. + (#47 and #48 by @SimonFrings) + +* Feature: Mark passwords and URIs as `#[\SensitiveParameter]` (PHP 8.2+). + (#49 by @SimonFrings) + +* Feature: Forward compatibility with upcoming Promise v3. + (#44 by @clue) + +* Fix: Fix invalid references in exception stack trace. + (#45 by @clue) + +* Improve test suite and fix legacy HHVM build. + (#46 by @SimonFrings) + +## 1.7.0 (2021-08-06) + +* Feature: Simplify usage by supporting new default loop and making `Connector` optional. + (#41 and #42 by @clue) + + ```php + // old (still supported) + $proxy = new Clue\React\HttpProxy\ProxyConnector( + '127.0.0.1:8080', + new React\Socket\Connector($loop) + ); + + // new (using default loop) + $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); + ``` + +* Documentation improvements and updated examples. + (#39 and #43 by @clue and #40 by @PaulRotmann) + +* Improve test suite and use GitHub actions for continuous integration (CI). + (#38 by @SimonFrings) + ## 1.6.0 (2020-10-23) * Enhanced documentation for ReactPHP's new HTTP client. @@ -81,7 +145,7 @@ ```php $promise = $proxy->connect('imap.example.com:143'); - $promise->then(null, function (Exeption $e) { + $promise->then(null, function (Exception $e) { if ($e->getCode() === SOCKET_EACCES) { echo 'Failed to authenticate with proxy!'; } diff --git a/README.md b/README.md index 44bcf69..67bf46c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # clue/reactphp-http-proxy -[![CI status](https://github.com/clue/reactphp-http-proxy/workflows/CI/badge.svg)](https://github.com/clue/reactphp-http-proxy/actions) +[![CI status](https://github.com/clue/reactphp-http-proxy/actions/workflows/ci.yml/badge.svg)](https://github.com/clue/reactphp-http-proxy/actions) +[![code coverage](https://img.shields.io/badge/code%20coverage-100%25-success)](#tests) +[![installs on Packagist](https://img.shields.io/packagist/dt/clue/http-proxy-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/http-proxy-react) Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP -CONNECT proxy server, built on top of [ReactPHP](https://reactphp.org). +CONNECT proxy server, built on top of [ReactPHP](https://reactphp.org/). HTTP CONNECT proxy servers (also commonly known as "HTTPS proxy" or "SSL proxy") are commonly used to tunnel HTTPS traffic through an intermediary ("proxy"), to @@ -56,13 +58,11 @@ existing higher-level protocol implementation. ## Support us -We invest a lot of time developing, maintaining and updating our awesome -open-source projects. You can help us sustain this high-quality of our work by -[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Sponsors get -numerous benefits in return, see our [sponsoring page](https://github.com/sponsors/clue) -for details. - -Let's take these projects to the next level together! 🚀 +I maintain an ecosystem of open-source projects that have been downloaded +hundreds of millions of times and are actively maintained and continuously +improved. If you find any of these projects useful, please consider +[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Your support +helps ensure long-term maintenance and continued development. Thank you! 🚀 ## Quickstart example @@ -70,26 +70,24 @@ The following example code demonstrates how this library can be used to send a secure HTTPS request to google.com through a local HTTP proxy server: ```php -$loop = React\EventLoop\Factory::create(); + $proxy, - 'timeout' => 3.0, 'dns' => false )); -$connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) { - $connection->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n"); - $connection->on('data', function ($chunk) { - echo $chunk; - }); -}, 'printf'); +$browser = new React\Http\Browser($connector); -$loop->run(); +$browser->get('https://google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); ``` See also the [examples](examples). @@ -105,20 +103,34 @@ any destination by using an intermediary HTTP CONNECT proxy. [you] -> [proxy] -> [destination] ``` -Its constructor simply accepts an HTTP proxy URL and a connector used to connect -to the proxy server address: +Its constructor simply accepts an HTTP proxy URL with the proxy server address: ```php -$connector = new React\Socket\Connector($loop); -$proxy = new Clue\React\HttpProxy\ProxyConnector('http://127.0.0.1:8080', $connector); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); ``` The proxy URL may or may not contain a scheme and port definition. The default port will be `80` for HTTP (or `443` for HTTPS), but many common HTTP proxy servers use custom ports (often the alternative HTTP port `8080`). -In its most simple form, the given connector will be a -[`\React\Socket\Connector`](https://github.com/reactphp/socket#connector) if you -want to connect to a given IP address as above. + +If you need custom connector settings (DNS resolution, TLS parameters, timeouts, +proxy servers etc.), you can explicitly pass a custom instance of the +[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface): + +```php +$connector = new React\Socket\Connector(array( + 'dns' => '127.0.0.1', + 'tcp' => array( + 'bindto' => '192.168.10.1:0' + ), + 'tls' => array( + 'verify_peer' => false, + 'verify_peer_name' => false + ) +)); + +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', $connector); +``` This is the main class in this package. Because it implements ReactPHP's standard @@ -137,7 +149,7 @@ higher-level component: ```diff - $acme = new AcmeApi($connector); -+ $proxy = new Clue\React\HttpProxy\ProxyConnector('http://127.0.0.1:8080', $connector); ++ $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', $connector); + $acme = new AcmeApi($proxy); ``` @@ -150,16 +162,15 @@ As documented above, you can simply invoke its `connect()` method to establish a streaming plain TCP/IP connection and use any higher level protocol like so: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ConnectionInterface $connection) { $connection->write("EHLO local\r\n"); $connection->on('data', function ($chunk) use ($connection) { echo $chunk; }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -167,12 +178,9 @@ You can either use the `ProxyConnector` directly or you may want to wrap this co in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -182,6 +190,8 @@ $connector->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socke $connection->on('data', function ($chunk) use ($connection) { echo $chunk; }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -193,16 +203,12 @@ Many (public) proxy servers do in fact limit this to HTTPS (443) only. This class can also be used if you want to establish a secure TLS connection (formerly known as SSL) between you and your destination, such as when using secure HTTPS to your destination site. You can simply wrap this connector in -ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the -low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector): +ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -212,6 +218,8 @@ $connector->connect('tls://smtp.googlemail.com:465')->then(function (React\Socke $connection->on('data', function ($chunk) use ($connection) { echo $chunk; }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -227,17 +235,14 @@ In order to send HTTP requests, you first have to add a dependency for This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser($loop, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); @@ -247,7 +252,7 @@ $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseI ``` See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) -and any of the [examples](examples) for more details. +and any of the [examples](examples/) for more details. #### Connection timeout @@ -260,19 +265,14 @@ Many use cases require more control over the timeout and likely values much smaller, usually in the range of a few seconds only. You can use ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) -or the low-level -[`TimeoutConnector`](https://github.com/reactphp/socket#timeoutconnector) to decorate any given `ConnectorInterface` instance. It provides the same `connect()` method, but will automatically reject the underlying connection attempt if it takes too long: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false, 'timeout' => 3.0 @@ -280,6 +280,8 @@ $connector = new React\Socket\Connector($loop, array( $connector->connect('tcp://google.com:80')->then(function ($connection) { // connection succeeded within 3.0 seconds +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -314,12 +316,9 @@ Given that remote DNS resolution is assumed to be the preferred mode, all other examples explicitly disable DNS resolution like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -328,13 +327,10 @@ $connector = new React\Socket\Connector($loop, array( If you want to explicitly use *local DNS resolution*, you can use the following code: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); // set up Connector which uses Google's public DNS (8.8.8.8) -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => '8.8.8.8' )); @@ -349,10 +345,7 @@ If your HTTP proxy server requires authentication, you may pass the username and password as part of the HTTP proxy URL like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http://user:pass@127.0.0.1:8080', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('alice:password@127.0.0.1:8080'); ``` Note that both the username and password must be percent-encoded if they contain @@ -361,11 +354,9 @@ special characters: ```php $user = 'he:llo'; $pass = 'p@ss'; +$url = rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080'; -$proxy = new Clue\React\HttpProxy\ProxyConnector( - rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080', - $connector -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); ``` > The authentication details will be used for basic authentication and will be @@ -388,7 +379,7 @@ you may simply pass an assoc array of additional request headers like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - $connector, + null, array( 'Proxy-Authorization' => 'Bearer abc123', 'User-Agent' => 'ReactPHP' @@ -404,16 +395,10 @@ setup, because you can still establish a TLS connection between you and the destination host as above. If you want to connect to a (rather rare) HTTPS proxy, you may want use the -`https://` scheme (HTTPS default port 443) and use ReactPHP's -[`Connector`](https://github.com/reactphp/socket#connector) or the low-level -[`SecureConnector`](https://github.com/reactphp/socket#secureconnector) -instance to create a secure connection to the proxy: +`https://` scheme (HTTPS default port 443) to create a secure connection to the proxy: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'https://127.0.0.1:443', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('https://127.0.0.1:443'); $proxy->connect('tcp://smtp.googlemail.com:587'); ``` @@ -430,13 +415,12 @@ having to rely on explicit [authentication](#authentication). You can simply use the `http+unix://` URI scheme like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http+unix:///tmp/proxy.sock', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix:///tmp/proxy.sock'); $proxy->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $connection) { // connected… +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -444,10 +428,7 @@ Similarly, you can also combine this with [authentication](#authentication) like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http+unix://user:pass@/tmp/proxy.sock', - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://alice:password@/tmp/proxy.sock'); ``` > Note that Unix domain sockets (UDS) are considered advanced usage and PHP only @@ -462,14 +443,14 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector( ## Install -The recommended way to install this library is [through Composer](https://getcomposer.org). +The recommended way to install this library is [through Composer](https://getcomposer.org/). [New to Composer?](https://getcomposer.org/doc/00-intro.md) This project follows [SemVer](https://semver.org/). This will install the latest supported version: ```bash -$ composer require clue/http-proxy-react:^1.6 +composer require clue/http-proxy-react:^1.10 ``` See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. @@ -477,36 +458,44 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+ and HHVM. -It's *highly recommended to use PHP 7+* for this project. +It's *highly recommended to use the latest supported PHP version* for this project. ## Tests To run the test suite, you first need to clone this repo and then install all -dependencies [through Composer](https://getcomposer.org): +dependencies [through Composer](https://getcomposer.org/): ```bash -$ composer install +composer install ``` To run the test suite, go to the project root and run: ```bash -$ php vendor/bin/phpunit +vendor/bin/phpunit ``` The test suite contains tests that rely on a working internet connection, alternatively you can also run it like this: ```bash -$ php vendor/bin/phpunit --exclude-group internet +vendor/bin/phpunit --exclude-group internet +``` + +The test suite is set up to always ensure 100% code coverage across all +supported environments. If you have the Xdebug extension installed, you can also +generate a code coverage report locally like this: + +```bash +XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text ``` ## License This project is released under the permissive [MIT license](LICENSE). -> Did you know that I offer custom development services and issuing invoices for - sponsorships of releases and for contributions? Contact me (@clue) for details. +> Do you use this project in a commercial setting? Sponsoring with invoicing is + available, contact me ([@clue](https://github.com/clue)) for details. ## More diff --git a/composer.json b/composer.json index e34a14c..e04b32e 100644 --- a/composer.json +++ b/composer.json @@ -10,22 +10,26 @@ "email": "christian@clue.engineering" } ], - "autoload": { - "psr-4": { "Clue\\React\\HttpProxy\\": "src/" } - }, - "autoload-dev": { - "psr-4": { "Clue\\Tests\\React\\HttpProxy\\": "tests/" } - }, "require": { "php": ">=5.3", - "react/promise": " ^2.1 || ^1.2.1", - "react/socket": "^1.1", - "ringcentral/psr7": "^1.2" + "react/promise": "^3.3 || ^2.1 || ^1.2.1", + "react/socket": "^1.17" }, "require-dev": { - "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", - "react/http": "^1.0", - "clue/block-react": "^1.1" + "phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36", + "react/async": "^4.3 || ^3 || ^2", + "react/event-loop": "^1.6", + "react/http": "^1.11", + "react/promise-timer": "^1.11" + }, + "autoload": { + "psr-4": { + "Clue\\React\\HttpProxy\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Clue\\Tests\\React\\HttpProxy\\": "tests/" + } } } diff --git a/examples/01-http-request.php b/examples/01-http-request.php index 4b192ea..f6741d9 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -5,39 +5,33 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example go to the project root and run: // // $ php examples/01-http-request.php // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/01-http-request.php +// $ http_proxy=127.0.0.1:8080 php examples/01-http-request.php require __DIR__ . '/../vendor/autoload.php'; $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } -$loop = React\EventLoop\Factory::create(); -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector($loop) -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser($loop, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index 1c8005d..820aa4c 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -11,32 +11,25 @@ // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/02-optional-proxy-http-request.php +// $ http_proxy=127.0.0.1:8080 php examples/02-optional-proxy-http-request.php require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $connector = null; $url = getenv('http_proxy'); if ($url !== false) { - $proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector($loop) - ); - $connector = new React\Socket\Connector($loop, array( + $proxy = new Clue\React\HttpProxy\ProxyConnector($url); + + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, - 'timeout' => 3.0, 'dns' => false )); } -$browser = new React\Http\Browser($loop, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index d3c1f2b..dcfc7c5 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/11-proxy-raw-https-protocol.php // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/11-proxy-raw-https-protocol.php +// $ http_proxy=127.0.0.1:8080 php examples/11-proxy-raw-https-protocol.php // // For illustration purposes only. If you want to send HTTP requests in a real // world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. @@ -21,17 +21,12 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } -$loop = React\EventLoop\Factory::create(); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector($loop) -); - -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -45,5 +40,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index 9d3e919..1aac199 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -11,7 +11,7 @@ // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/12-optional-proxy-raw-https-protocol.php +// $ http_proxy=127.0.0.1:8080 php examples/12-optional-proxy-raw-https-protocol.php // // This example highlights how changing from direct connection to using a proxy // actually adds very little complexity and does not mess with your actual @@ -22,21 +22,17 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - -$connector = new React\Socket\Connector($loop); - $url = getenv('http_proxy'); if ($url !== false) { - $proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - $connector - ); - $connector = new React\Socket\Connector($loop, array( + $proxy = new Clue\React\HttpProxy\ProxyConnector($url); + + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); +} else { + $connector = new React\Socket\Connector(); } $connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) { @@ -47,5 +43,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 2612ee1..d4248f5 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/13-custom-proxy-headers.php // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/13-custom-proxy-headers.php +// $ http_proxy=127.0.0.1:8080 php examples/13-custom-proxy-headers.php // // For illustration purposes only. If you want to send HTTP requests in a real // world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. @@ -21,21 +21,19 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } -$loop = React\EventLoop\Factory::create(); - $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop), + null, array( 'X-Custom-Header-1' => 'Value-1', 'X-Custom-Header-2' => 'Value-2', ) ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false, @@ -49,5 +47,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index f66d253..a491d8b 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/21-proxy-raw-smtp-protocol.php // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/21-proxy-raw-smtp-protocol.php +// $ http_proxy=127.0.0.1:8080 php examples/21-proxy-raw-smtp-protocol.php // // Please note that MANY public proxies do not allow SMTP connections, YMMV. @@ -20,17 +20,12 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } -$loop = React\EventLoop\Factory::create(); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector($loop) -); - -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -45,5 +40,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index 796f59b..519464f 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/22-proxy-raw-smtps-protocol.php // // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.2:8080 php examples/22-proxy-raw-smtps-protocol.php +// $ http_proxy=127.0.0.1:8080 php examples/22-proxy-raw-smtps-protocol.php // // This example highlights how changing from plain connections (see previous // example) to using a secure connection actually adds very little complexity @@ -23,17 +23,12 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } -$loop = React\EventLoop\Factory::create(); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector($loop) -); - -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -48,5 +43,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dbe6515..af249cf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,12 @@ - - + + convertDeprecationsToExceptions="true"> ./tests/ @@ -16,4 +17,7 @@ ./src/ + + + diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy index c21d571..3d5928a 100644 --- a/phpunit.xml.legacy +++ b/phpunit.xml.legacy @@ -1,6 +1,6 @@ - + ./src/ + + + diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 020e12a..c9347fe 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -5,12 +5,13 @@ use Exception; use InvalidArgumentException; use RuntimeException; -use RingCentral\Psr7; use React\Promise; use React\Promise\Deferred; use React\Socket\ConnectionInterface; +use React\Socket\Connector; use React\Socket\ConnectorInterface; use React\Socket\FixedUriConnector; +use React\Socket\UnixConnector; /** * A simple Connector that uses an HTTP CONNECT proxy to create plain TCP/IP connections to any destination @@ -51,14 +52,16 @@ class ProxyConnector implements ConnectorInterface * @param string $proxyUrl The proxy URL may or may not contain a scheme and * port definition. The default port will be `80` for HTTP (or `443` for * HTTPS), but many common HTTP proxy servers use custom ports. - * @param ConnectorInterface $connector In its most simple form, the given - * connector will be a \React\Socket\Connector if you want to connect to - * a given IP address. + * @param ?ConnectorInterface $connector (Optional) Connector to use. * @param array $httpHeaders Custom HTTP headers to be sent to the proxy. * @throws InvalidArgumentException if the proxy URL is invalid */ - public function __construct($proxyUrl, ConnectorInterface $connector, array $httpHeaders = array()) - { + public function __construct( + #[\SensitiveParameter] + $proxyUrl, + $connector = null, + array $httpHeaders = array() + ) { // support `http+unix://` scheme for Unix domain socket (UDS) paths if (preg_match('/^http\+unix:\/\/(.*?@)?(.+?)$/', $proxyUrl, $match)) { // rewrite URI to parse authentication from dummy host @@ -67,7 +70,7 @@ public function __construct($proxyUrl, ConnectorInterface $connector, array $htt // connector uses Unix transport scheme and explicit path given $connector = new FixedUriConnector( 'unix://' . $match[2], - $connector + $connector ?: new UnixConnector() // @codeCoverageIgnore ); } @@ -80,13 +83,17 @@ public function __construct($proxyUrl, ConnectorInterface $connector, array $htt throw new InvalidArgumentException('Invalid proxy URL "' . $proxyUrl . '"'); } + if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface'); + } + // apply default port and TCP/TLS transport for given scheme if (!isset($parts['port'])) { $parts['port'] = $parts['scheme'] === 'https' ? 443 : 80; } $parts['scheme'] = $parts['scheme'] === 'https' ? 'tls' : 'tcp'; - $this->connector = $connector; + $this->connector = $connector ?: new Connector(); $this->proxyUri = $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']; // prepare Proxy-Authorization header if URI contains username/password @@ -155,6 +162,8 @@ public function connect($uri) // either close active connection or cancel pending connection attempt $connecting->then(function (ConnectionInterface $stream) { $stream->close(); + }, function () { + // ignore to avoid reporting unhandled rejection }); $connecting->cancel(); }); @@ -173,30 +182,30 @@ public function connect($uri) $fn = null; // try to parse headers as response message - try { - $response = Psr7\parse_response(substr($buffer, 0, $pos)); - } catch (Exception $e) { + if (!preg_match('/^HTTP\/1\.[01] ([0-9]{3})( .*)?\r\n/', $buffer, $matches)) { $deferred->reject(new RuntimeException( 'Connection to ' . $uri . ' failed because proxy returned invalid response (EBADMSG)', - defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71, - $e + defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG : 71 )); $stream->close(); return; } - if ($response->getStatusCode() === 407) { + $statusCode = (int) $matches[1]; + $reasonPhrase = isset($matches[2]) ? trim($matches[2]) : ''; + + if ($statusCode === 407) { // map status code 407 (Proxy Authentication Required) to EACCES $deferred->reject(new RuntimeException( - 'Connection to ' . $uri . ' failed because proxy denied access with HTTP error code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ') (EACCES)', + 'Connection to ' . $uri . ' failed because proxy denied access with HTTP error code ' . $statusCode . ' (' . $reasonPhrase . ') (EACCES)', defined('SOCKET_EACCES') ? SOCKET_EACCES : 13 )); $stream->close(); return; - } elseif ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { + } elseif ($statusCode < 200 || $statusCode >= 300) { // map non-2xx status code to ECONNREFUSED $deferred->reject(new RuntimeException( - 'Connection to ' . $uri . ' failed because proxy refused connection with HTTP error code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ') (ECONNREFUSED)', + 'Connection to ' . $uri . ' failed because proxy refused connection with HTTP error code ' . $statusCode . ' (' . $reasonPhrase . ') (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 )); $stream->close(); @@ -251,16 +260,18 @@ public function connect($uri) // avoid garbage references by replacing all closures in call stack. // what a lovely piece of code! $r = new \ReflectionProperty('Exception', 'trace'); - $r->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $r->setAccessible(true); // @codeCoverageIgnore + } $trace = $r->getValue($e); // Exception trace arguments are not available on some PHP 7.4 installs // @codeCoverageIgnoreStart - foreach ($trace as &$one) { + foreach ($trace as $ti => $one) { if (isset($one['args'])) { - foreach ($one['args'] as &$arg) { + foreach ($one['args'] as $ai => $arg) { if ($arg instanceof \Closure) { - $arg = 'Object(' . get_class($arg) . ')'; + $trace[$ti]['args'][$ai] = 'Object(' . get_class($arg) . ')'; } } } diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 85eb676..72bb151 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -4,7 +4,7 @@ use PHPUnit\Framework\TestCase; -abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase +abstract class AbstractTestCase extends TestCase { protected function expectCallableNever() { @@ -77,6 +77,4 @@ public function setExpectedException($exception, $exceptionMessage = '', $except parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); } } - } - diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 321ace8..9bd5dbc 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -2,38 +2,14 @@ namespace Clue\Tests\React\HttpProxy; -use React\EventLoop\Factory; use Clue\React\HttpProxy\ProxyConnector; -use React\Socket\TcpConnector; -use React\Socket\DnsConnector; -use Clue\React\Block; -use React\Socket\SecureConnector; /** @group internet */ class FunctionalTest extends AbstractTestCase { - private $loop; - private $tcpConnector; - private $dnsConnector; - - /** - * @before - */ - public function setUpConnector() - { - $this->loop = Factory::create(); - - $this->tcpConnector = new TcpConnector($this->loop); - - $f = new \React\Dns\Resolver\Factory(); - $resolver = $f->create('8.8.8.8', $this->loop); - - $this->dnsConnector = new DnsConnector($this->tcpConnector, $resolver); - } - public function testNonListeningSocketRejectsConnection() { - $proxy = new ProxyConnector('127.0.0.1:9999', $this->dnsConnector); + $proxy = new ProxyConnector('127.0.0.1:9999'); $promise = $proxy->connect('google.com:80'); @@ -42,12 +18,12 @@ public function testNonListeningSocketRejectsConnection() 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } public function testPlainGoogleDoesNotAcceptConnectMethod() { - $proxy = new ProxyConnector('google.com', $this->dnsConnector); + $proxy = new ProxyConnector('google.com'); $promise = $proxy->connect('google.com:80'); @@ -56,7 +32,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } public function testSecureGoogleDoesNotAcceptConnectMethod() @@ -65,8 +41,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() $this->markTestSkipped('TLS not supported on legacy HHVM'); } - $secure = new SecureConnector($this->dnsConnector, $this->loop); - $proxy = new ProxyConnector('https://google.com:443', $secure); + $proxy = new ProxyConnector('https://google.com:443'); $promise = $proxy->connect('google.com:80'); @@ -75,12 +50,12 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } public function testSecureGoogleDoesNotAcceptPlainStream() { - $proxy = new ProxyConnector('google.com:443', $this->dnsConnector); + $proxy = new ProxyConnector('google.com:443'); $promise = $proxy->connect('google.com:80'); @@ -89,7 +64,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() 'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response (ECONNRESET)', defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104 ); - Block\await($promise, $this->loop, 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } /** @@ -97,7 +72,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() */ public function testCancelWhileConnectingShouldNotCreateGarbageCycles() { - $proxy = new ProxyConnector('google.com', $this->dnsConnector); + $proxy = new ProxyConnector('google.com'); gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 654699a..bf2e292 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -3,9 +3,9 @@ namespace Clue\Tests\React\HttpProxy; use Clue\React\HttpProxy\ProxyConnector; -use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Promise\Deferred; +use React\Promise\Promise; class ProxyConnectorTest extends AbstractTestCase { @@ -19,6 +19,19 @@ public function setUpMock() $this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); } + public function testConstructWithoutConnectorAssignsConnectorAutomatically() + { + $proxy = new ProxyConnector('proxy.example.com'); + + $ref = new \ReflectionProperty($proxy, 'connector'); + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } + $connector = $ref->getValue($proxy); + + $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector); + } + public function testInvalidProxy() { $this->setExpectedException('InvalidArgumentException'); @@ -37,6 +50,12 @@ public function testInvalidHttpsUnixScheme() new ProxyConnector('https+unix:///tmp/proxy.sock', $this->connector); } + public function testContructorThrowsExceptionForInvalidConnector() + { + $this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface'); + new ProxyConnector('proxy.example.com', 'connector'); + } + public function testCreatesConnectionToHttpPort() { $promise = new Promise(function () { }); @@ -126,7 +145,7 @@ public function testCancelPromiseWillCancelPendingConnection() $promise = $proxy->connect('google.com:80'); - $this->assertInstanceOf('React\Promise\CancellablePromiseInterface', $promise); + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $promise->cancel(); } @@ -291,15 +310,17 @@ public function testRejectsWithPreviousIfConnectorRejects() $promise = $proxy->connect('google.com:80'); - $promise->then(null, $this->expectCallableOnceWithException( - 'RuntimeException', - 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', - defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 - )); + $exception = null; + $promise->then(null, function ($reason) use (&$exception) { + $exception = $reason; + }); - $promise->then(null, $this->expectCallableOnceWith($this->callback(function (\Exception $e) use ($previous) { - return $e->getPrevious() === $previous; - }))); + assert($exception instanceof \RuntimeException); + $this->assertInstanceOf('RuntimeException', $exception); + $this->assertEquals('Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', $exception->getMessage()); + $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); + $this->assertSame($previous, $exception->getPrevious()); + $this->assertNotEquals('', $exception->getTraceAsString()); } public function testRejectsAndClosesIfStreamWritesNonHttp() @@ -464,7 +485,7 @@ public function testCancelPromiseWhileConnectionIsReadyWillCloseOpenConnectionAn $deferred->resolve($stream); - $this->assertInstanceOf('React\Promise\CancellablePromiseInterface', $promise); + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $promise->cancel(); @@ -529,6 +550,9 @@ public function testRejectedConnectionShouldNotCreateGarbageCycles() $proxy = new ProxyConnector('proxy.example.com', $this->connector); $promise = $proxy->connect('google.com:80'); + + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection + unset($promise); $this->assertEquals(0, gc_collect_cycles());