Skip to content

Commit

Permalink
Source
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Dec 7, 2017
1 parent 7fbb96d commit 2ea3345
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/DebugMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

namespace ApiClients\Middleware\Debug;

use ApiClients\Foundation\Middleware\MiddlewareInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;
use React\Stream\WritableResourceStream;
use React\Stream\WritableStreamInterface;
use Throwable;
use function React\Promise\reject;
use function React\Promise\resolve;

final class DebugMiddleware implements MiddlewareInterface
{
const HR = '---------------------------------------' . PHP_EOL;

/**
* @var WritableStreamInterface
*/
private $stdout;

public function __construct(LoopInterface $loop, WritableStreamInterface $stdout = null)
{
if ($stdout === null) {
$stdout = new WritableResourceStream(STDOUT, $loop);
}

$this->stdout = $stdout;
}

/**
* Return the processed $request via a fulfilled promise.
* When implementing cache or other feature that returns a response, do it with a rejected promise.
* If neither is possible, e.g. on some kind of failure, resolve the unaltered request.
*
* @param RequestInterface $request
* @param string $transactionId
* @param array $options
* @return CancellablePromiseInterface
*/
public function pre(RequestInterface $request, string $transactionId, array $options = []): CancellablePromiseInterface
{
$this->stdout->write(self::HR);
$this->stdout->write($transactionId . ': request' . PHP_EOL);
$this->stdout->write(self::HR);
$this->stdout->write('Method: ' . $request->getMethod() . PHP_EOL);
$this->stdout->write('URI: ' . (string)$request->getUri() . PHP_EOL);
$this->stdout->write(self::HR);

return resolve($request);
}

/**
* Return the processed $response via a promise.
*
* @param ResponseInterface $response
* @param string $transactionId
* @param array $options
* @return CancellablePromiseInterface
*/
public function post(ResponseInterface $response, string $transactionId, array $options = []): CancellablePromiseInterface
{
$this->stdout->write(self::HR);
$this->stdout->write($transactionId . ': response' . PHP_EOL);
$this->stdout->write(self::HR);
$this->stdout->write('Status: ' . $response->getStatusCode() . PHP_EOL);
$this->stdout->write(self::HR);

return resolve($response);
}

/**
* Deal with possible errors that occurred during request/response events.
*
* @param Throwable $throwable
* @param string $transactionId
* @param array $options
* @return CancellablePromiseInterface
*/
public function error(Throwable $throwable, string $transactionId, array $options = []): CancellablePromiseInterface
{
$this->stdout->write(self::HR);
$this->stdout->write($transactionId . ': error' . PHP_EOL);
$this->stdout->write(self::HR);
$this->stdout->write((string)$throwable);
$this->stdout->write(self::HR);

return reject($throwable);
}
}
48 changes: 48 additions & 0 deletions tests/DebugMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Middleware\Debug;

use ApiClients\Middleware\Debug\DebugMiddleware;
use ApiClients\Tools\TestUtilities\TestCase;
use React\EventLoop\Factory;
use React\Stream\ThroughStream;
use RingCentral\Psr7\Request;
use RingCentral\Psr7\Response;
use function Clue\React\Block\await;
use function React\Promise\Stream\buffer;

final class DebugMiddlewareTest extends TestCase
{
public function testAllMethods()
{
$expectedStdout = '';
$expectedStdout .= DebugMiddleware::HR;
$expectedStdout .= 'abc: request' . PHP_EOL;
$expectedStdout .= DebugMiddleware::HR;
$expectedStdout .= 'Method: GET' . PHP_EOL;
$expectedStdout .= 'URI: foo.bar' . PHP_EOL;
$expectedStdout .= DebugMiddleware::HR;
$expectedStdout .= DebugMiddleware::HR;
$expectedStdout .= 'abc: response' . PHP_EOL;
$expectedStdout .= DebugMiddleware::HR;
$expectedStdout .= 'Status: 200' . PHP_EOL;
$expectedStdout .= DebugMiddleware::HR;

$request = new Request('GET', 'foo.bar');
$response = new Response();

$throughStream = new ThroughStream();
$promise = buffer($throughStream);

$loop = Factory::create();
$middleware = new DebugMiddleware($loop, $throughStream);

$middleware->pre($request, 'abc', []);
$middleware->post($response, 'abc', []);

$throughStream->close();

$stdout = await($promise, $loop);
self::assertSame($expectedStdout, $stdout);
}
}

0 comments on commit 2ea3345

Please sign in to comment.