Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/ErrorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 18 additions & 3 deletions src/MiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,41 @@ 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,
* but never turn it into a successful promise again.
*
* @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;
}
12 changes: 9 additions & 3 deletions src/MiddlewareRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ final class MiddlewareRunner
*/
private $middlewares;

/**
* @var string
*/
private $id;

/**
* MiddlewareRunner constructor.
* @param array $options
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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);
});
}

Expand All @@ -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));
});
}

Expand Down
7 changes: 5 additions & 2 deletions src/PostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 5 additions & 2 deletions src/PreTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
48 changes: 27 additions & 21 deletions tests/MiddlewareRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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)
);
}
}