diff --git a/README.md b/README.md index 803a5b0..add742c 100644 --- a/README.md +++ b/README.md @@ -375,16 +375,14 @@ object to contain a list of entries. As stated above, this library provides you a powerful, async API by default. -If, however, you want to integrate this into your traditional, blocking environment, -you should look into also using [clue/reactphp-block](https://github.com/clue/reactphp-block). - -The resulting blocking code could look something like this: +You can also integrate this into your traditional, blocking environment by using +[reactphp/async](https://github.com/reactphp/async). This allows you to simply +await responses on the client like this: ```php -use Clue\React\Block; -use React\EventLoop\Loop; +use function React\Async\await; -function getSipPeers() +function getSipPeers(): array { $factory = new Clue\React\Ami\Factory(); @@ -398,11 +396,13 @@ function getSipPeers() return $ret; }); - return Block\await($promise, Loop::get(), 5.0); + return await($promise); } ``` -Refer to [clue/reactphp-block](https://github.com/clue/reactphp-block#readme) for more details. +This is made possible thanks to fibers available in PHP 8.1+ and our +compatibility API that also works on all supported PHP versions. +Please refer to [reactphp/async](https://github.com/reactphp/async#readme) for more details. ### Message diff --git a/composer.json b/composer.json index bd69c2e..9299d77 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ "react/socket": "^1.14" }, "require-dev": { - "clue/block-react": "^1.5", - "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36" + "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", + "react/async": "^4.2 || ^3.2 || ^2.2" }, "autoload": { "psr-4": { diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 4e3ff00..d281974 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -2,11 +2,11 @@ namespace Clue\Tests\React\Ami; -use Clue\React\Ami\Factory; -use Clue\React\Ami\Client; use Clue\React\Ami\ActionSender; -use Clue\React\Block; -use React\Promise\PromiseInterface; +use Clue\React\Ami\Client; +use Clue\React\Ami\Factory; +use Clue\React\Ami\Protocol\Response; +use React\EventLoop\Loop; class FunctionalTest extends TestCase { @@ -19,7 +19,6 @@ class FunctionalTest extends TestCase public static function setUpLoopBeforeClass() { self::$address = getenv('LOGIN'); - self::$loop = \React\EventLoop\Factory::create(); } /** @@ -34,10 +33,16 @@ public function setUpSkipTest() public function testConnection() { - $factory = new Factory(self::$loop); + $factory = new Factory(); + + $client = \React\Async\await($factory->createClient(self::$address)); + assert($client instanceof Client); - $client = $this->waitFor($factory->createClient(self::$address)); - /* @var $client Client */ + // let loop tick for reactphp/async v4 to clean up any remaining references + // @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged + if (function_exists('React\Async\async')) { + \React\Async\delay(0.0); + } $this->assertFalse($client->isBusy()); @@ -52,7 +57,7 @@ public function testPing(Client $client) { $sender = new ActionSender($client); - $pong = $this->waitFor($sender->ping()); + $pong = \React\Async\await($sender->ping()); $this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $pong); } @@ -64,7 +69,7 @@ public function testPing(Client $client) public function testInvalidCommandGetsRejected(Client $client) { $this->setExpectedException('Exception'); - $this->waitFor($client->request($client->createAction('Invalid'))); + \React\Async\await($client->request($client->createAction('Invalid'))); } /** @@ -75,15 +80,20 @@ public function testActionSenderLogoffDisconnects(Client $client) { $sender = new ActionSender($client); - $ret = $this->waitFor($sender->logoff()); + $ret = \React\Async\await($sender->logoff()); + assert($ret instanceof Response); - $this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $ret); + // let loop tick for reactphp/async v4 to clean up any remaining references + // @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged + if (function_exists('React\Async\async')) { + \React\Async\delay(0.0); + } $this->assertFalse($client->isBusy()); //$client->on('close', $this->expectCallableOnce()); - self::$loop->run(); + Loop::run(); return $client; } @@ -95,11 +105,6 @@ public function testActionSenderLogoffDisconnects(Client $client) public function testSendRejectedAfterClose(Client $client) { $this->setExpectedException('Exception'); - $this->waitFor($client->request($client->createAction('Ping'))); - } - - private function waitFor(PromiseInterface $promise) - { - return Block\await($promise, self::$loop, 5.0); + \React\Async\await($client->request($client->createAction('Ping'))); } }