From: Michael Wallner Date: Wed, 20 May 2015 15:41:51 +0000 (+0200) Subject: update to current promise APIs X-Git-Tag: v2.1.0~1^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=refs%2Fheads%2Fasync;p=m6w6%2Fpq-gateway update to current promise APIs --- diff --git a/composer.json b/composer.json index 07c3d53..2fd2af6 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "pq\\Query": "lib" } }, - "suggest": { - "react/promise": "1.0.*" + "require-dev": { + "react/promise": "~2.2", + "amphp/amp": "^1.0-beta" } } diff --git a/lib/pq/Query/AsyncExecutor.php b/lib/pq/Query/AsyncExecutor.php index a309ce6..a61f87a 100644 --- a/lib/pq/Query/AsyncExecutor.php +++ b/lib/pq/Query/AsyncExecutor.php @@ -33,7 +33,7 @@ class AsyncExecutor extends Executor * * Example with reactphp: * - * use \React\Promise\Deferred; + * use React\Promise\Deferred; * * $exec = new pq\Query\AsyncExecutor(new pq\Connection); * $exec->setCallbacks( @@ -43,33 +43,32 @@ class AsyncExecutor extends Executor * }, * # done * function(Deferred $context, $result) { - * $context->resolver()->resolve($result); + * $context->resolve($result); * }, * # then * function(Deferred $context, callable $cb) { - * return $context->then($cb); + * return $context->promise()->then($cb); * }); * $exec->execute($queryWriter, function($result){}); * * * Example with amphp: * - * use Amp\Future; - * use function Amp\reactor; + * use Amp\Deferred; * * $exec = new pq\Query\AsyncExecutor(new pq\Connection); * $exec->setCallbacks( * # init context * function() { - * return new Future(reactor()); + * return new Deferred; * }, * # done - * function(Future $context, $result) { + * function(Deferred $context, $result) { * $context->succeed($result); * }, * # then - * function(Future $context, callable $cb) { - * return $context->when(function ($error, $result) use ($cb) { + * function(Deferred $context, callable $cb) { + * return $context->promise()->when(function($error, $result) use ($cb) { * $cb($result); * }); * }); @@ -127,6 +126,7 @@ class AsyncExecutor extends Executor function(\pq\Result $result) { $this->result = $result; $this->notify(); + return $result; }, $callback); $this->getConnection()->execParamsAsync($query, $query->getParams(), $query->getTypes(), $resolver); diff --git a/tests/lib/pq/Query/AsyncExecutorTest.php b/tests/lib/pq/Query/AsyncExecutorTest.php new file mode 100644 index 0000000..ad68a91 --- /dev/null +++ b/tests/lib/pq/Query/AsyncExecutorTest.php @@ -0,0 +1,72 @@ +conn = new Connection(PQ_TEST_DSN); + $this->query = new Writer("SELECT \$1::int,\$2::int", [1,2]); + } + + function testReact() { + $exec = new AsyncExecutor($this->conn); + $exec->setCallbacks( + # init context + function() { + return new Reacted; + }, + # done + function(Reacted $context, $result) { + $context->resolve($result); + }, + # then + function(Reacted $context, callable $cb) { + return $context->promise()->then($cb); + }); + + $guard = new \stdClass; + $exec->execute($this->query, function($result) use($guard) { + $guard->result = $result; + }); + $this->conn->getResult(); + $this->assertTrue(!empty($guard->result), "guard is empty"); + $this->assertInstanceOf("pq\\Result", $guard->result); + $this->assertSame([[1,2]], $guard->result->fetchAll()); + } + + function testAmp() { + $exec = new AsyncExecutor($this->conn); + $exec->setCallbacks( + # init context + function() { + return new Amped; + }, + # done + function(Amped $context, $result) { + $context->succeed($result); + }, + # then + function(Amped $context, callable $cb) { + return $context->promise()->when(function($error, $result) use ($cb) { + $cb($result); + }); + }); + $guard = new \stdClass; + $exec->execute($this->query, function($result) use($guard) { + $guard->result = $result; + }); + $this->conn->getResult(); + $this->assertTrue(!empty($guard->result), "guard is empty"); + $this->assertInstanceOf("pq\\Result", $guard->result); + $this->assertSame([[1,2]], $guard->result->fetchAll()); + } +} diff --git a/tests/setup.inc b/tests/setup.inc index fa7af13..ec2236b 100644 --- a/tests/setup.inc +++ b/tests/setup.inc @@ -39,7 +39,7 @@ const PQ_TEST_TEARDOWN_SQL = <<getConnection();