diff --git a/README.md b/README.md index 5777904..e426ee7 100644 --- a/README.md +++ b/README.md @@ -90,15 +90,16 @@ you should look into also using [clue/block-react](https://github.com/clue/php-b The resulting blocking code could look something like this: ```php +use Clue\React\Block; + $loop = React\EventLoop\Factory::create(); $browser = new Clue\React\Buzz\Browser($loop); -$blocker = new Clue\React\Block\Blocker($loop); $client = new Client($url /* change me */, $browser); $promise = $client->fetchFile($path /* change me */); try { - $contents = $blocker->awaitOne($promise); + $contents = Block\await($promise, $loop); // file contents received } catch (Exception $e) { // an error occured while executing the request diff --git a/composer.json b/composer.json index 8b4d6ff..92f258c 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,12 @@ "require": { "php": ">=5.3", "react/event-loop": "~0.4.0|~0.3.0", - "clue/buzz-react": "~0.2.0", + "react/promise": "~2.1|~1.1", + "clue/buzz-react": "~0.4.0", "ext-simplexml": "*", "neitanod/forceutf8": "~1.4" }, "require-dev": { - "clue/block-react": "~0.1.0" + "clue/block-react": "~0.3.0" } } diff --git a/src/Client.php b/src/Client.php index 78f7678..daf642d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -8,7 +8,7 @@ use InvalidArgumentException; use Clue\React\Buzz\Browser; use Clue\React\Buzz\Message\Response; -use React\Promise\Deferred; +use React\Promise; use Clue\React\ViewVcApi\Io\Parser; use Clue\React\ViewVcApi\Io\Loader; @@ -40,7 +40,7 @@ public function __construct($url, Browser $browser, Parser $parser = null, Loade public function fetchFile($path, $revision = null) { if (substr($path, -1) === '/') { - return $this->reject(new InvalidArgumentException('File path MUST NOT end with trailing slash')); + return Promise\reject(new InvalidArgumentException('File path MUST NOT end with trailing slash')); } $url = $path . '?view=co'; @@ -59,7 +59,7 @@ public function fetchFile($path, $revision = null) public function fetchDirectory($path, $revision = null, $showAttic = false) { if (substr($path, -1) !== '/') { - return $this->reject(new InvalidArgumentException('Directory path MUST end with trailing slash')); + return Promise\reject(new InvalidArgumentException('Directory path MUST end with trailing slash')); } $url = $path; @@ -144,12 +144,4 @@ function ($error) { } ); } - - private function reject($with) - { - $deferred = new Deferred(); - $deferred->reject($with); - - return $deferred->promise(); - } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 4684f13..33b8720 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -3,6 +3,7 @@ use Clue\React\ViewVcApi\Client; use Clue\React\Buzz\Message\Response; use Clue\React\Buzz\Message\Body; +use React\Promise; class ClientTest extends TestCase { @@ -31,8 +32,8 @@ public function testInvalidFile() public function testFetchFile() { - $response = new Response('HTTP/1.0', 200, 'OK', null, new Body('# hello')); - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue($this->createPromiseResolved($response))); + $response = new Response('HTTP/1.0', 200, 'OK', array(), new Body('# hello')); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue(Promise\resolve($response))); $promise = $this->client->fetchFile('README.md'); @@ -41,7 +42,7 @@ public function testFetchFile() public function testFetchFileExcessiveSlashesAreIgnored() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue(Promise\reject())); $client = new Client($this->url . '/', $this->browser); $promise = $client->fetchFile('/README.md'); @@ -51,7 +52,7 @@ public function testFetchFileExcessiveSlashesAreIgnored() public function testFetchFileRevision() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co&pathrev=1.0'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co&pathrev=1.0'))->will($this->returnValue(Promise\reject())); $promise = $this->client->fetchFile('/README.md', '1.0'); @@ -60,7 +61,7 @@ public function testFetchFileRevision() public function testFetchDirectoryRevision() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?pathrev=1.0'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?pathrev=1.0'))->will($this->returnValue(Promise\reject())); $promise = $this->client->fetchDirectory('/directory/', '1.0'); @@ -69,7 +70,7 @@ public function testFetchDirectoryRevision() public function testFetchDirectoryAttic() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?hideattic=0'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?hideattic=0'))->will($this->returnValue(Promise\reject())); $promise = $this->client->fetchDirectory('/directory/', null, true); @@ -78,7 +79,7 @@ public function testFetchDirectoryAttic() public function testFetchDirectoryRevisionAttic() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?pathrev=1.1&hideattic=0'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?pathrev=1.1&hideattic=0'))->will($this->returnValue(Promise\reject())); $promise = $this->client->fetchDirectory('/directory/', '1.1', true); @@ -87,7 +88,7 @@ public function testFetchDirectoryRevisionAttic() public function testFetchLogRevision() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=log&pathrev=1.0'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=log&pathrev=1.0'))->will($this->returnValue(Promise\reject())); $promise = $this->client->fetchLog('/README.md', '1.0'); @@ -96,7 +97,7 @@ public function testFetchLogRevision() public function testFetchPatch() { - $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=patch&r1=1.0&r2=1.1'))->will($this->returnValue($this->createPromiseRejected())); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=patch&r1=1.0&r2=1.1'))->will($this->returnValue(Promise\reject())); $promise = $this->client->fetchPatch('/README.md', '1.0', '1.1'); diff --git a/tests/FunctionalApacheClientTest.php b/tests/FunctionalApacheClientTest.php index 2556b0f..df5943e 100644 --- a/tests/FunctionalApacheClientTest.php +++ b/tests/FunctionalApacheClientTest.php @@ -3,21 +3,18 @@ use Clue\React\ViewVcApi\Client; use React\EventLoop\Factory as LoopFactory; use Clue\React\Buzz\Browser; -use React\Promise\PromiseInterface; -use Clue\React\Block\Blocker; +use Clue\React\Block; class FunctionalApacheClientTest extends TestCase { private $loop; private $viewvc; - private $blocker; public function setUp() { $url = 'http://svn.apache.org/viewvc/'; $this->loop = LoopFactory::create(); - $this->blocker = new Blocker($this->loop); $browser = new Browser($this->loop); $this->viewvc = new Client($url, $browser); @@ -28,7 +25,7 @@ public function testFetchDirectory() $path = 'jakarta/ecs/'; $promise = $this->viewvc->fetchDirectory($path); - $files = $this->waitFor($promise); + $files = Block\await($promise, $this->loop); $this->assertEquals(array('branches/', 'tags/', 'trunk/'), $files); } @@ -39,7 +36,7 @@ public function testFetchFile() $revision = '168703'; $promise = $this->viewvc->fetchFile($file, $revision); - $recipe = $this->waitFor($promise); + $recipe = Block\await($promise, $this->loop); $this->assertStringStartsWith('/*', $recipe); } @@ -50,7 +47,7 @@ public function testFetchFileOldFileNowDeletedButRevisionAvailable() $revision = '1'; $promise = $this->viewvc->fetchFile($file, $revision); - $contents = $this->waitFor($promise); + $contents = Block\await($promise, $this->loop); $this->assertStringStartsWith('APACHE COMMONS', $contents); } @@ -64,7 +61,7 @@ public function testFetchFileInvalid() $revision = '123'; $promise = $this->viewvc->fetchFile($file, $revision); - $this->waitFor($promise); + Block\await($promise, $this->loop); } public function testFetchRevisionPrevious() @@ -73,7 +70,7 @@ public function testFetchRevisionPrevious() $revision = '168703'; $promise = $this->viewvc->fetchRevisionPrevious($file, $revision); - $revision = $this->waitFor($promise); + $revision = Block\await($promise, $this->loop); $this->assertEquals('168695', $revision); } @@ -87,11 +84,6 @@ public function testFetchRevisionUnknownBase() $revision = 'xyz'; $promise = $this->viewvc->fetchRevisionPrevious($file, $revision); - $this->waitFor($promise); - } - - private function waitFor(PromiseInterface $promise) - { - return $this->blocker->awaitOne($promise); + Block\await($promise, $this->loop); } } diff --git a/tests/FunctionalGentooClientTest.php b/tests/FunctionalGentooClientTest.php index 802b15f..772455c 100644 --- a/tests/FunctionalGentooClientTest.php +++ b/tests/FunctionalGentooClientTest.php @@ -3,14 +3,12 @@ use Clue\React\ViewVcApi\Client; use React\EventLoop\Factory as LoopFactory; use Clue\React\Buzz\Browser; -use React\Promise\PromiseInterface; -use Clue\React\Block\Blocker; +use Clue\React\Block; class FunctionalGentooClientTest extends TestCase { private $loop; private $viewvc; - private $blocker; public function setUp() { @@ -21,14 +19,13 @@ public function setUp() $url = 'https://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo/'; $this->loop = LoopFactory::create(); - $this->blocker = new Blocker($this->loop); $browser = new Browser($this->loop); // check connectivity to given URL only once static $checked = null; if ($checked === null) { try { - $this->waitFor($browser->head($url)); + Block\await($browser->head($url), $this->loop); $checked = true; } catch (Exception $e) { $checked = false; @@ -47,7 +44,7 @@ public function testFetchDirectoryAttic() $path = '/'; $promise = $this->viewvc->fetchDirectory($path, null, true); - $files = $this->waitFor($promise); + $files = Block\await($promise, $this->loop); $this->assertEquals(array('misc/', 'src/', 'users/', 'xml/', '.frozen'), $files); } @@ -57,13 +54,8 @@ public function testFetchFileDeletedShowsLastState() $file = '.frozen'; $promise = $this->viewvc->fetchFile($file); - $contents = $this->waitFor($promise); + $contents = Block\await($promise, $this->loop); $this->assertEquals("robbat2\n", $contents); } - - private function waitFor(PromiseInterface $promise) - { - return $this->blocker->awaitOne($promise); - } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 237f0b4..ff38480 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,7 +1,5 @@ resolve($value); - - return $deferred->promise(); - } - - protected function createPromiseRejected($value = null) - { - $deferred = new Deferred(); - $deferred->reject($value); - - return $deferred->promise(); - } } class CallableStub