diff --git a/composer.json b/composer.json index 3872206..4ca4a24 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/spec/LoggerPluginSpec.php b/spec/LoggerPluginSpec.php index 90f77a6..4ab7b75 100644 --- a/spec/LoggerPluginSpec.php +++ b/spec/LoggerPluginSpec.php @@ -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() @@ -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()); @@ -51,18 +49,15 @@ 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); }; @@ -70,8 +65,15 @@ function it_logs_exception(LoggerInterface $logger, RequestInterface $request, U $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(); @@ -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); }; diff --git a/spec/Normalizer/NormalizerSpec.php b/spec/Normalizer/NormalizerSpec.php deleted file mode 100644 index a7f860c..0000000 --- a/spec/Normalizer/NormalizerSpec.php +++ /dev/null @@ -1,37 +0,0 @@ -shouldHaveType('Http\Client\Plugin\Normalizer\Normalizer'); - } - - function it_normalize_request_to_string(RequestInterface $request, UriInterface $uri) - { - $uri->__toString()->willReturn('http://foo.com/bar'); - $request->getMethod()->willReturn('GET'); - $request->getUri()->willReturn($uri); - $request->getProtocolVersion()->willReturn('1.1'); - - $this->normalizeRequestToString($request)->shouldReturn('GET http://foo.com/bar 1.1'); - } - - function it_normalize_response_to_string(ResponseInterface $response) - { - $response->getReasonPhrase()->willReturn('Ok'); - $response->getProtocolVersion()->willReturn('1.1'); - $response->getStatusCode()->willReturn('200'); - - $this->normalizeResponseToString($response)->shouldReturn('200 Ok 1.1'); - } -} diff --git a/src/LoggerPlugin.php b/src/LoggerPlugin.php index 6bd232e..5feec57 100644 --- a/src/LoggerPlugin.php +++ b/src/LoggerPlugin.php @@ -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; @@ -23,19 +24,19 @@ 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(); } /** @@ -43,11 +44,11 @@ public function __construct(LoggerInterface $logger) */ 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, @@ -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(), @@ -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, diff --git a/src/Normalizer/Normalizer.php b/src/Normalizer/Normalizer.php deleted file mode 100644 index 799f1f8..0000000 --- a/src/Normalizer/Normalizer.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * @internal Should not be used outside of the logger plugin - */ -class Normalizer -{ - /** - * Normalize a request to string. - * - * @param RequestInterface $request - * - * @return string - */ - public function normalizeRequestToString(RequestInterface $request) - { - return sprintf('%s %s %s', $request->getMethod(), $request->getUri()->__toString(), $request->getProtocolVersion()); - } - - /** - * Normalize a response to string. - * - * @param ResponseInterface $response - * - * @return string - */ - public function normalizeResponseToString(ResponseInterface $response) - { - return sprintf('%s %s %s', $response->getStatusCode(), $response->getReasonPhrase(), $response->getProtocolVersion()); - } -}