From 173e2a2967b3884de059f205b17dcaaf70ab675a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 4 Sep 2015 20:14:51 +0200 Subject: [PATCH 1/3] Update clue/buzz-react to v0.4 --- composer.json | 2 +- tests/ClientTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8b4d6ff..603150f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "require": { "php": ">=5.3", "react/event-loop": "~0.4.0|~0.3.0", - "clue/buzz-react": "~0.2.0", + "clue/buzz-react": "~0.4.0", "ext-simplexml": "*", "neitanod/forceutf8": "~1.4" }, diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 4684f13..5fa5769 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -31,7 +31,7 @@ public function testInvalidFile() public function testFetchFile() { - $response = new Response('HTTP/1.0', 200, 'OK', null, new Body('# hello')); + $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($this->createPromiseResolved($response))); $promise = $this->client->fetchFile('README.md'); From 76b4813a1d794153fa4541fc0f431fd8c1b5c6fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 4 Sep 2015 20:20:23 +0200 Subject: [PATCH 2/3] Update clue/block-react to v0.3 --- README.md | 5 +++-- composer.json | 2 +- tests/FunctionalApacheClientTest.php | 21 +++++++-------------- tests/FunctionalGentooClientTest.php | 15 ++++----------- 4 files changed, 15 insertions(+), 28 deletions(-) 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 603150f..0f1889f 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,6 @@ "neitanod/forceutf8": "~1.4" }, "require-dev": { - "clue/block-react": "~0.1.0" + "clue/block-react": "~0.3.0" } } diff --git a/tests/FunctionalApacheClientTest.php b/tests/FunctionalApacheClientTest.php index 2556b0f..e29934d 100644 --- a/tests/FunctionalApacheClientTest.php +++ b/tests/FunctionalApacheClientTest.php @@ -4,20 +4,18 @@ 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 +26,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 +37,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 +48,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 +62,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 +71,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 +85,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..ffd1f6a 100644 --- a/tests/FunctionalGentooClientTest.php +++ b/tests/FunctionalGentooClientTest.php @@ -4,13 +4,12 @@ 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 +20,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 +45,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 +55,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); - } } From 7c5842af5d4c9e477bc93d187406b4683c6a72fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 4 Sep 2015 20:25:56 +0200 Subject: [PATCH 3/3] Update to new Promise API (keeping BC) --- composer.json | 1 + src/Client.php | 14 +++----------- tests/ClientTest.php | 17 +++++++++-------- tests/FunctionalApacheClientTest.php | 1 - tests/FunctionalGentooClientTest.php | 1 - tests/bootstrap.php | 18 ------------------ 6 files changed, 13 insertions(+), 39 deletions(-) diff --git a/composer.json b/composer.json index 0f1889f..92f258c 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "require": { "php": ">=5.3", "react/event-loop": "~0.4.0|~0.3.0", + "react/promise": "~2.1|~1.1", "clue/buzz-react": "~0.4.0", "ext-simplexml": "*", "neitanod/forceutf8": "~1.4" 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 5fa5769..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 { @@ -32,7 +33,7 @@ public function testInvalidFile() public function testFetchFile() { $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($this->createPromiseResolved($response))); + $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 e29934d..df5943e 100644 --- a/tests/FunctionalApacheClientTest.php +++ b/tests/FunctionalApacheClientTest.php @@ -3,7 +3,6 @@ use Clue\React\ViewVcApi\Client; use React\EventLoop\Factory as LoopFactory; use Clue\React\Buzz\Browser; -use React\Promise\PromiseInterface; use Clue\React\Block; class FunctionalApacheClientTest extends TestCase diff --git a/tests/FunctionalGentooClientTest.php b/tests/FunctionalGentooClientTest.php index ffd1f6a..772455c 100644 --- a/tests/FunctionalGentooClientTest.php +++ b/tests/FunctionalGentooClientTest.php @@ -3,7 +3,6 @@ use Clue\React\ViewVcApi\Client; use React\EventLoop\Factory as LoopFactory; use Clue\React\Buzz\Browser; -use React\Promise\PromiseInterface; use Clue\React\Block; class FunctionalGentooClientTest extends TestCase 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