Skip to content

Commit

Permalink
Merge pull request #2 from php-middleware/psr-15
Browse files Browse the repository at this point in the history
Add support for psr-15
  • Loading branch information
snapshotpl authored Apr 29, 2017
2 parents b588404 + 387c14c commit 43198c0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 45 deletions.
27 changes: 13 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
language: php

matrix:
fast_finish: true
include:
- php: 5.5
- php: 5.6
- php: 7
- php: hhvm
allow_failures:
- php: 7
- php: hhvm
php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
- composer info -i
env:
- COMPOSER_FLAGS=--prefer-lowest
- COMPOSER_FLAGS=

before_script:
- composer update --no-interaction --no-suggest --prefer-dist $COMPOSER_FLAGS

script:
- ./vendor/bin/phpunit
- vendor/bin/phpunit
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# log-http-messages middleware [![Build Status](https://travis-ci.org/php-middleware/log-http-messages.svg)](https://travis-ci.org/php-middleware/log-http-messages)
Middleware for log PSR-7 HTTP messages using PSR-3 logger
PSR-15 middleware for log PSR-7 HTTP messages using PSR-3 logger

This middleware provide framework-agnostic possibility to log request and response messages to PSR-3 logger.
Support double and single (PSR-15) pass middleware.

## Installation

```json
{
"require": {
"php-middleware/log-http-messages": "^2.0.0"
}
}
```
composer require php-middleware/log-http-messages
```

To log http messages you need pass into `LogRequestMiddleware` implementation of `PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter`,
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "php-middleware/log-http-messages",
"description": "Request and response middleware logger with PSR-7 and PSR-3",
"description": "PSR-15 middleware for log PSR-7 HTTP messages using PSR-3 logger",
"type": "library",
"keywords": [
"debug",
"middleware",
"psr",
"psr-7",
"psr-15",
"log",
"logger",
"psr-3"
Expand All @@ -15,10 +16,11 @@
"php": ">=5.5",
"psr/log": "^1.0.0",
"psr/http-message": "^1.0",
"zendframework/zend-diactoros": "^1.1.3"
"zendframework/zend-diactoros": "^1.1.3",
"http-interop/http-middleware": "^0.4.1"
},
"require-dev": {
"phpunit/phpunit": "^4.8.6"
"phpunit/phpunit": "^4.8"
},
"autoload": {
"psr-4": {
Expand Down
45 changes: 38 additions & 7 deletions src/LogMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace PhpMiddleware\LogHttpMessages;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
use Psr\Log\LoggerInterface as Logger;
use Psr\Log\LogLevel;
use UnexpectedValueException;

class LogMiddleware
class LogMiddleware implements MiddlewareInterface
{
/**
* @var Logger
Expand All @@ -26,7 +29,8 @@ class LogMiddleware
protected $formatter;

/**
* @param LoggerInterface $logger
* @param HttpMessagesFormatter $formatter
* @param Logger $logger
* @param int $level
*/
public function __construct(HttpMessagesFormatter $formatter, Logger $logger, $level = LogLevel::INFO)
Expand All @@ -41,20 +45,47 @@ public function __construct(HttpMessagesFormatter $formatter, Logger $logger, $l
* @param Response $response
* @param callable $next
*
* @return ResponseInterface
* @return Response
*/
public function __invoke(ServerRequest $request, Response $response, callable $next)
{
$outResponse = $next($request, $response);

$messages = $this->formatter->format($request, $outResponse);
$this->logMessages($request, $outResponse);

return $outResponse;
}

/**
* @param ServerRequest $request
* @param DelegateInterface $delegate
*
* @return Response
*/
public function process(ServerRequest $request, DelegateInterface $delegate)
{
$response = $delegate->process($request);

$this->logMessages($request, $response);

return $response;
}

/**
* @param ServerRequest $request
* @param Response $response
*
* @throws UnexpectedValueException
*/
private function logMessages(ServerRequest $request, Response $response)
{
$messages = $this->formatter->format($request, $response);

if (!is_string($messages)) {
throw new \UnexpectedValueException(sprintf('%s must return string', HttpMessagesFormatter::class));
throw new UnexpectedValueException(sprintf('%s must return string', HttpMessagesFormatter::class));
}

$this->logger->log($this->level, $messages);

return $outResponse;
}

}
2 changes: 1 addition & 1 deletion test/Formatter/BothFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function testFormatter()

$result = $this->formatter->format($request, $response);

$this->assertSame("Request: GET /php-middleware/log-http-messages HTTP/1.1\r\nAccept: text/html; Response HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html", $result);
$this->assertContains('; Response ', $result);
}
}
52 changes: 39 additions & 13 deletions test/LogMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

namespace PhpMiddlewareTest\LogHttpMessages;

use Interop\Http\ServerMiddleware\DelegateInterface;
use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter;
use PhpMiddleware\LogHttpMessages\LogMiddleware;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use UnexpectedValueException;

class LogMiddlewareTest extends PHPUnit_Framework_TestCase
{
protected $middleware;
public $middleware;
protected $formatter;
protected $logger;
protected $request;
protected $response;
protected $next;
protected $level;
protected $delegate;
protected $nextResponse;

protected function setUp()
{
Expand All @@ -28,6 +32,8 @@ protected function setUp()
$this->next = function () {
return $this->nextResponse;
};
$this->delegate = $this->getMock(DelegateInterface::class);
$this->delegate->method('process')->willReturn($this->nextResponse);

$this->formatter = $this->getMock(HttpMessagesFormatter::class);
$this->logger = $this->getMock(LoggerInterface::class);
Expand All @@ -36,28 +42,48 @@ protected function setUp()
$this->middleware = new LogMiddleware($this->formatter, $this->logger, $this->level);
}

public function testLogMessage()
/**
* @dataProvider middlewareProvider
*/
public function testLogFormattedMessages($middlewareExecutor)
{
$this->formatter->expects($this->once())->method('format')->with($this->request, $this->nextResponse)->willReturn('boo');
$this->logger->expects($this->once())->method('log')->with($this->level, 'boo');

$response = $this->executeMiddleware();
$this->formatter->method('format')->with($this->request, $this->nextResponse)->willReturn('formattedMessages');
$this->logger->expects($this->once())->method('log')->with($this->level, 'formattedMessages');

$this->assertSame($this->nextResponse, $response);
$middlewareExecutor($this);
}

/**
* @expectedException \UnexpectedValueException
* @dataProvider middlewareProvider
*/
public function testTryToLogNullMessage()
public function testTryToLogNullMessage($middlewareExecutor)
{
$this->formatter->expects($this->once())->method('format')->willReturn(null);
$this->formatter->method('format')->willReturn(null);

$this->setExpectedException(UnexpectedValueException::class);

$middlewareExecutor($this);
}

$this->executeMiddleware();
public function middlewareProvider()
{
return [
'double pass' => [function ($test) {
return $test->executeDoublePassMiddleware();
}],
'single pass' => [function ($test) {
return $test->executeSinglePassMiddleware();
}],
];
}

public function executeMiddleware()
protected function executeDoublePassMiddleware()
{
return call_user_func($this->middleware, $this->request, $this->response, $this->next);
}
}

protected function executeSinglePassMiddleware()
{
return $this->middleware->process($this->request, $this->delegate);
}
}

0 comments on commit 43198c0

Please sign in to comment.