Skip to content

Replace normalizer with message formatter #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2015
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"symfony/options-resolver": "^2.6|^3.0"
},
"require-dev": {
"php-http/message": "^0.1",
"php-http/message": "^0.1.1",
"php-http/cookie": "^0.1@dev",
"symfony/stopwatch": "^2.3",
"psr/log": "^1.0",
Expand Down
59 changes: 26 additions & 33 deletions spec/LoggerPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
use Http\Client\Exception\NetworkException;
use Http\Promise\FulfilledPromise;
use Http\Promise\RejectedPromise;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Http\Message\Formatter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use PhpSpec\ObjectBehavior;

class LoggerPluginSpec extends ObjectBehavior
{
function let(LoggerInterface $logger)
function let(LoggerInterface $logger, Formatter $formatter)
{
$this->beConstructedWith($logger);
$this->beConstructedWith($logger, $formatter);
}

function it_is_initializable()
Expand All @@ -30,19 +30,17 @@ function it_is_a_plugin()
$this->shouldImplement('Http\Client\Plugin\Plugin');
}

function it_logs_request_and_response(LoggerInterface $logger, RequestInterface $request, ResponseInterface $response, UriInterface $uri)
{
$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
$logger->info('Receive response: "200 Ok 1.1" for request: "GET / 1.1"', ['request' => $request, 'response' => $response])->shouldBeCalled();

$uri->__toString()->willReturn('/');
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
$request->getProtocolVersion()->willReturn('1.1');
function it_logs_request_and_response(
LoggerInterface $logger,
Formatter $formatter,
RequestInterface $request,
ResponseInterface $response
) {
$formatter->formatRequest($request)->willReturn('GET / 1.1');
$formatter->formatResponse($response)->willReturn('200 OK 1.1');

$response->getReasonPhrase()->willReturn('Ok');
$response->getProtocolVersion()->willReturn('1.1');
$response->getStatusCode()->willReturn('200');
$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
$logger->info('Receive response: "200 OK 1.1" for request: "GET / 1.1"', ['request' => $request, 'response' => $response])->shouldBeCalled();

$next = function () use ($response) {
return new FulfilledPromise($response->getWrappedObject());
Expand All @@ -51,27 +49,31 @@ function it_logs_request_and_response(LoggerInterface $logger, RequestInterface
$this->handleRequest($request, $next, function () {});
}

function it_logs_exception(LoggerInterface $logger, RequestInterface $request, UriInterface $uri)
function it_logs_exception(LoggerInterface $logger, Formatter $formatter, RequestInterface $request)
{
$formatter->formatRequest($request)->willReturn('GET / 1.1');

$exception = new NetworkException('Cannot connect', $request->getWrappedObject());

$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
$logger->error('Error: "Cannot connect" when emitting request: "GET / 1.1"', ['request' => $request, 'exception' => $exception])->shouldBeCalled();

$uri->__toString()->willReturn('/');
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
$request->getProtocolVersion()->willReturn('1.1');

$next = function () use ($exception) {
return new RejectedPromise($exception);
};

$this->handleRequest($request, $next, function () {});
}

function it_logs_response_within_exception(LoggerInterface $logger, RequestInterface $request, ResponseInterface $response, UriInterface $uri)
{
function it_logs_response_within_exception(
LoggerInterface $logger,
Formatter $formatter,
RequestInterface $request,
ResponseInterface $response
) {
$formatter->formatRequest($request)->willReturn('GET / 1.1');
$formatter->formatResponse($response)->willReturn('403 Forbidden 1.1');

$exception = new HttpException('Forbidden', $request->getWrappedObject(), $response->getWrappedObject());

$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
Expand All @@ -81,15 +83,6 @@ function it_logs_response_within_exception(LoggerInterface $logger, RequestInter
'exception' => $exception
])->shouldBeCalled();

$uri->__toString()->willReturn('/');
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
$request->getProtocolVersion()->willReturn('1.1');

$response->getReasonPhrase()->willReturn('Forbidden');
$response->getProtocolVersion()->willReturn('1.1');
$response->getStatusCode()->willReturn('403');

$next = function () use ($exception) {
return new RejectedPromise($exception);
};
Expand Down
37 changes: 0 additions & 37 deletions spec/Normalizer/NormalizerSpec.php

This file was deleted.

21 changes: 11 additions & 10 deletions src/LoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Http\Client\Plugin;

use Http\Client\Exception;
use Http\Client\Plugin\Normalizer\Normalizer;
use Http\Message\Formatter;
use Http\Message\Formatter\SimpleFormatter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -23,31 +24,31 @@ class LoggerPlugin implements Plugin
private $logger;

/**
* Normalize request and response to string or array.
* Formats a request/response as string.
*
* @var Normalizer
* @var Formatter
*/
private $normalizer;
private $formatter;

/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
public function __construct(LoggerInterface $logger, Formatter $formatter = null)
{
$this->logger = $logger;
$this->normalizer = new Normalizer();
$this->formatter = $formatter ?: new SimpleFormatter();
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$this->logger->info(sprintf('Emit request: "%s"', $this->normalizer->normalizeRequestToString($request)), ['request' => $request]);
$this->logger->info(sprintf('Emit request: "%s"', $this->formatter->formatRequest($request)), ['request' => $request]);

return $next($request)->then(function (ResponseInterface $response) use ($request) {
$this->logger->info(
sprintf('Receive response: "%s" for request: "%s"', $this->normalizer->normalizeResponseToString($response), $this->normalizer->normalizeRequestToString($request)),
sprintf('Receive response: "%s" for request: "%s"', $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
[
'request' => $request,
'response' => $response,
Expand All @@ -58,7 +59,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}, function (Exception $exception) use ($request) {
if ($exception instanceof Exception\HttpException) {
$this->logger->error(
sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->normalizer->normalizeResponseToString($exception->getResponse()), $this->normalizer->normalizeRequestToString($request)),
sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
[
'request' => $request,
'response' => $exception->getResponse(),
Expand All @@ -67,7 +68,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
);
} else {
$this->logger->error(
sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->normalizer->normalizeRequestToString($request)),
sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request)),
[
'request' => $request,
'exception' => $exception,
Expand Down
40 changes: 0 additions & 40 deletions src/Normalizer/Normalizer.php

This file was deleted.