update to current promise APIs async
authorMichael Wallner <mike@php.net>
Wed, 20 May 2015 15:41:51 +0000 (17:41 +0200)
committerMichael Wallner <mike@php.net>
Wed, 20 May 2015 15:41:51 +0000 (17:41 +0200)
composer.json
lib/pq/Query/AsyncExecutor.php
tests/lib/pq/Query/AsyncExecutorTest.php [new file with mode: 0644]
tests/setup.inc

index 07c3d537bb6ae87eeb93df83aa37da44ff103e3f..2fd2af6f4e95bb0ecaf9999d696ec05ff5b95f59 100644 (file)
@@ -17,7 +17,8 @@
                        "pq\\Query": "lib"
                }
        },
-       "suggest": {
-               "react/promise": "1.0.*"
+       "require-dev": {
+               "react/promise": "~2.2",
+               "amphp/amp": "^1.0-beta"
        }
 }
index a309ce62799cf535810870868b1dc38d10459834..a61f87a868cca08df0729562ba35cd43a29ad6b3 100644 (file)
@@ -33,7 +33,7 @@ class AsyncExecutor extends Executor
         * 
         * Example with reactphp:
         * <code>
-        * 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){});
         * </code>
         * 
         * Example with amphp:
         * <code>
-        * 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 (file)
index 0000000..ad68a91
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+namespace pq\Query;
+
+include_once __DIR__."/../../../setup.inc";
+
+use pq\Connection;
+use React\Promise\Deferred as Reacted;
+use Amp\Deferred as Amped;
+
+class AsyncExecutorTest extends \PHPUnit_Framework_TestCase {
+       private $conn;
+       private $query;
+       
+       protected function setUp() {
+               $this->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());
+       }
+}
index fa7af13428ec71522b02d220dd6d81eb3ff0eb8e..ec2236bf19f9d0131bbf7ecf3dc6511311c72354 100644 (file)
@@ -39,7 +39,7 @@ const PQ_TEST_TEARDOWN_SQL = <<<SQL
        drop table if exists reftest cascade;
 SQL;
 
-include_once __DIR__ . "/../lib/autoload.php";
+require_once __DIR__ . "/../vendor/autoload.php";
 
 function executeInConcurrentTransaction(\pq\Query\ExecutorInterface $exec, $sql, array $params = null) {
        $conn = $exec->getConnection();