From da6247f74327f47b2e9346bede6bbc531d5b94d3 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 13 Jun 2017 23:41:06 +0200 Subject: [PATCH] Pass a transaction ID into pre, post, and error methods so middlwares can store information between pre, post, and errors calls --- src/ErrorTrait.php | 7 +++-- src/MiddlewareInterface.php | 21 ++++++++++++--- src/MiddlewareRunner.php | 12 ++++++--- src/PostTrait.php | 7 +++-- src/PreTrait.php | 7 +++-- tests/MiddlewareRunnerTest.php | 48 +++++++++++++++++++--------------- 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/src/ErrorTrait.php b/src/ErrorTrait.php index 2cbd25c..3a2a2a8 100644 --- a/src/ErrorTrait.php +++ b/src/ErrorTrait.php @@ -14,8 +14,11 @@ trait ErrorTrait * @param array $options * @return CancellablePromiseInterface */ - public function error(Throwable $throwable, array $options = []): CancellablePromiseInterface - { + public function error( + Throwable $throwable, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface { return reject($throwable); } } diff --git a/src/MiddlewareInterface.php b/src/MiddlewareInterface.php index 00ff8ae..f1519ce 100644 --- a/src/MiddlewareInterface.php +++ b/src/MiddlewareInterface.php @@ -27,18 +27,28 @@ public function priority(): int; * * @param RequestInterface $request * @param array $options + * @param string $transactionId * @return CancellablePromiseInterface */ - public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface; + public function pre( + RequestInterface $request, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface; /** * Return the processed $response via a promise. * * @param ResponseInterface $response * @param array $options + * @param string $transactionId * @return CancellablePromiseInterface */ - public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface; + public function post( + ResponseInterface $response, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface; /** * Transform the throwable into another throwable or exception, @@ -46,7 +56,12 @@ public function post(ResponseInterface $response, array $options = []): Cancella * * @param Throwable $throwable * @param array $options + * @param string $transactionId * @return CancellablePromiseInterface */ - public function error(Throwable $throwable, array $options = []): CancellablePromiseInterface; + public function error( + Throwable $throwable, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface; } diff --git a/src/MiddlewareRunner.php b/src/MiddlewareRunner.php index 854aeae..f5d221c 100644 --- a/src/MiddlewareRunner.php +++ b/src/MiddlewareRunner.php @@ -21,6 +21,11 @@ final class MiddlewareRunner */ private $middlewares; + /** + * @var string + */ + private $id; + /** * MiddlewareRunner constructor. * @param array $options @@ -30,6 +35,7 @@ public function __construct(array $options, MiddlewareInterface ...$middlewares) { $this->options = $options; $this->middlewares = $this->orderMiddlewares(...$middlewares); + $this->id = bin2hex(random_bytes(32)); } /** @@ -59,7 +65,7 @@ public function pre( foreach ($this->middlewares as $middleware) { $requestMiddleware = $middleware; $promise = $promise->then(function (RequestInterface $request) use ($requestMiddleware) { - return $requestMiddleware->pre($request, $this->options); + return $requestMiddleware->pre($request, $this->options, $this->id); }); } @@ -80,7 +86,7 @@ public function post( foreach ($this->middlewares as $middleware) { $responseMiddleware = $middleware; $promise = $promise->then(function (ResponseInterface $response) use ($responseMiddleware) { - return $responseMiddleware->post($response, $this->options); + return $responseMiddleware->post($response, $this->options, $this->id); }); } @@ -102,7 +108,7 @@ public function error( foreach ($this->middlewares as $middleware) { $errorMiddleware = $middleware; $promise = $promise->then(null, function (Throwable $throwable) use ($errorMiddleware) { - return reject($errorMiddleware->error($throwable, $this->options)); + return reject($errorMiddleware->error($throwable, $this->options, $this->id)); }); } diff --git a/src/PostTrait.php b/src/PostTrait.php index 28fac29..19afa27 100644 --- a/src/PostTrait.php +++ b/src/PostTrait.php @@ -13,8 +13,11 @@ trait PostTrait * @param array $options * @return CancellablePromiseInterface */ - public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface - { + public function post( + ResponseInterface $response, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface { return resolve($response); } } diff --git a/src/PreTrait.php b/src/PreTrait.php index b1baaac..233e1e2 100644 --- a/src/PreTrait.php +++ b/src/PreTrait.php @@ -13,8 +13,11 @@ trait PreTrait * @param array $options * @return CancellablePromiseInterface */ - public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface - { + public function pre( + RequestInterface $request, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface { return resolve($request); } } diff --git a/tests/MiddlewareRunnerTest.php b/tests/MiddlewareRunnerTest.php index 55cf12d..05b6a7f 100644 --- a/tests/MiddlewareRunnerTest.php +++ b/tests/MiddlewareRunnerTest.php @@ -5,6 +5,7 @@ use ApiClients\Foundation\Middleware\MiddlewareInterface; use ApiClients\Foundation\Middleware\MiddlewareRunner; use ApiClients\Tools\TestUtilities\TestCase; +use Closure; use Exception; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; @@ -27,22 +28,10 @@ public function testAll() $middlewareOne = Phake::mock(MiddlewareInterface::class); Phake::when($middlewareOne)->priority()->thenReturn(1000); - Phake::when($middlewareOne)->pre($request, $options)->thenReturn(resolve($request)); - Phake::when($middlewareOne)->post($response, $options)->thenReturn(resolve($response)); - Phake::when($middlewareOne)->error($exception, $options)->thenReturn(reject($exception)); - $middlewareTwo = Phake::mock(MiddlewareInterface::class); Phake::when($middlewareTwo)->priority()->thenReturn(500); - Phake::when($middlewareTwo)->pre($request, $options)->thenReturn(resolve($request)); - Phake::when($middlewareTwo)->post($response, $options)->thenReturn(resolve($response)); - Phake::when($middlewareTwo)->error($exception, $options)->thenReturn(reject($exception)); - $middlewareThree = Phake::mock(MiddlewareInterface::class); Phake::when($middlewareThree)->priority()->thenReturn(0); - Phake::when($middlewareThree)->pre($request, $options)->thenReturn(resolve($request)); - Phake::when($middlewareThree)->post($response, $options)->thenReturn(resolve($response)); - Phake::when($middlewareThree)->error($exception, $options)->thenReturn(reject($exception)); - $args = [ $options, $middlewareThree, @@ -51,6 +40,23 @@ public function testAll() ]; $executioner = new MiddlewareRunner(...$args); + $id = Closure::bind(function ($executioner) { + return $executioner->id; + }, null, MiddlewareRunner::class)($executioner); + + Phake::when($middlewareOne)->pre($request, $options, $id)->thenReturn(resolve($request)); + Phake::when($middlewareOne)->post($response, $options, $id)->thenReturn(resolve($response)); + Phake::when($middlewareOne)->error($exception, $options, $id)->thenReturn(reject($exception)); + + Phake::when($middlewareTwo)->pre($request, $options, $id)->thenReturn(resolve($request)); + Phake::when($middlewareTwo)->post($response, $options, $id)->thenReturn(resolve($response)); + Phake::when($middlewareTwo)->error($exception, $options, $id)->thenReturn(reject($exception)); + + Phake::when($middlewareThree)->pre($request, $options, $id)->thenReturn(resolve($request)); + Phake::when($middlewareThree)->post($response, $options, $id)->thenReturn(resolve($response)); + Phake::when($middlewareThree)->error($exception, $options, $id)->thenReturn(reject($exception)); + + self::assertSame($request, await($executioner->pre($request), $loop)); self::assertSame($response, await($executioner->post($response), $loop)); try { @@ -60,15 +66,15 @@ public function testAll() } Phake::inOrder( - Phake::verify($middlewareOne)->pre($request, $options), - Phake::verify($middlewareTwo)->pre($request, $options), - Phake::verify($middlewareThree)->pre($request, $options), - Phake::verify($middlewareThree)->post($response, $options), - Phake::verify($middlewareTwo)->post($response, $options), - Phake::verify($middlewareOne)->post($response, $options), - Phake::verify($middlewareOne)->error($exception, $options), - Phake::verify($middlewareTwo)->error($exception, $options), - Phake::verify($middlewareThree)->error($exception, $options) + Phake::verify($middlewareOne)->pre($request, $options, $id), + Phake::verify($middlewareTwo)->pre($request, $options, $id), + Phake::verify($middlewareThree)->pre($request, $options, $id), + Phake::verify($middlewareThree)->post($response, $options, $id), + Phake::verify($middlewareTwo)->post($response, $options, $id), + Phake::verify($middlewareOne)->post($response, $options, $id), + Phake::verify($middlewareOne)->error($exception, $options, $id), + Phake::verify($middlewareTwo)->error($exception, $options, $id), + Phake::verify($middlewareThree)->error($exception, $options, $id) ); } }