From 82fd719372ef14c66b28ca3a49f57d7e25d16310 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 19:40:05 +0200 Subject: [PATCH 1/9] Add support for psr-15 --- .travis.yml | 27 +++++++++++++-------------- composer.json | 9 +++++---- src/LogMiddleware.php | 16 ++++++++++++++-- test/LogMiddlewareTest.php | 6 +++--- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index a0931eb..30f4ed9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index dc25ecd..2562d3d 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,8 @@ "php": ">=5.5", "psr/log": "^1.0.0", "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.1.3" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.6" + "zendframework/zend-diactoros": "^1.1.3", + "http-interop/http-middleware": "^0.4.1" }, "autoload": { "psr-4": { @@ -29,5 +27,8 @@ "psr-4": { "PhpMiddlewareTest\\LogHttpMessages\\": "test/" } + }, + "require-dev": { + "phpunit/phpunit": "^5.0" } } diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index c1d86e4..1e6109b 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -2,13 +2,19 @@ namespace PhpMiddleware\LogHttpMessages; +use Interop\Http\ServerMiddleware\DelegateInterface; +use Interop\Http\ServerMiddleware\MiddlewareInterface; use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter; +use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface as ServerRequest; +use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface as Logger; use Psr\Log\LogLevel; +use UnexpectedValueException; -class LogMiddleware +class LogMiddleware implements MiddlewareInterface { /** * @var Logger @@ -50,11 +56,17 @@ public function __invoke(ServerRequest $request, Response $response, callable $n $messages = $this->formatter->format($request, $outResponse); 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; } + + public function process(ServerRequestInterface $request, DelegateInterface $delegate) + { + + } + } diff --git a/test/LogMiddlewareTest.php b/test/LogMiddlewareTest.php index f831734..8722688 100644 --- a/test/LogMiddlewareTest.php +++ b/test/LogMiddlewareTest.php @@ -41,7 +41,7 @@ public function testLogMessage() $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(); + $response = $this->executeDoublePassMiddleware(); $this->assertSame($this->nextResponse, $response); } @@ -53,10 +53,10 @@ public function testTryToLogNullMessage() { $this->formatter->expects($this->once())->method('format')->willReturn(null); - $this->executeMiddleware(); + $this->executeDoublePassMiddleware(); } - public function executeMiddleware() + public function executeDoublePassMiddleware() { return call_user_func($this->middleware, $this->request, $this->response, $this->next); } From 74c23dfe8207d98abd3fb304c39b2acf03e9f89c Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 19:42:46 +0200 Subject: [PATCH 2/9] Use old phpunit --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 2562d3d..90060c3 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,9 @@ "zendframework/zend-diactoros": "^1.1.3", "http-interop/http-middleware": "^0.4.1" }, + "require-dev": { + "phpunit/phpunit": "^4.8" + }, "autoload": { "psr-4": { "PhpMiddleware\\LogHttpMessages\\": "src/" @@ -27,8 +30,5 @@ "psr-4": { "PhpMiddlewareTest\\LogHttpMessages\\": "test/" } - }, - "require-dev": { - "phpunit/phpunit": "^5.0" } } From b363726258fecdcef97e0949e024977c36011a82 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 19:45:55 +0200 Subject: [PATCH 3/9] Improve docs and imports --- src/LogMiddleware.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index 1e6109b..3671480 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -5,11 +5,8 @@ use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface; use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter; -use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface as Response; -use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface as ServerRequest; -use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface as Logger; use Psr\Log\LogLevel; use UnexpectedValueException; @@ -32,7 +29,8 @@ class LogMiddleware implements MiddlewareInterface protected $formatter; /** - * @param LoggerInterface $logger + * @param HttpMessagesFormatter $formatter + * @param Logger $logger * @param int $level */ public function __construct(HttpMessagesFormatter $formatter, Logger $logger, $level = LogLevel::INFO) @@ -47,7 +45,7 @@ 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) { @@ -64,7 +62,7 @@ public function __invoke(ServerRequest $request, Response $response, callable $n return $outResponse; } - public function process(ServerRequestInterface $request, DelegateInterface $delegate) + public function process(ServerRequest $request, DelegateInterface $delegate) { } From 9a350d0a2e4a14beb612e2f78c1ee52fdbd1253b Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 19:50:37 +0200 Subject: [PATCH 4/9] Fix tests --- test/Formatter/BothFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Formatter/BothFormatterTest.php b/test/Formatter/BothFormatterTest.php index d112bb3..252b068 100644 --- a/test/Formatter/BothFormatterTest.php +++ b/test/Formatter/BothFormatterTest.php @@ -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->assertSame("Request: GET /php-middleware/log-http-messages HTTP/1.1\r\nAccept: text/html\r\nHost: github.com; Response HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n", $result); } } \ No newline at end of file From 59d8428ef5d43c8ab680c8c938f0459aeb14ad78 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 20:01:15 +0200 Subject: [PATCH 5/9] Fix tests --- src/LogMiddleware.php | 2 +- test/Formatter/BothFormatterTest.php | 2 +- test/LogMiddlewareTest.php | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index 3671480..4b90a74 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -66,5 +66,5 @@ public function process(ServerRequest $request, DelegateInterface $delegate) { } - + } diff --git a/test/Formatter/BothFormatterTest.php b/test/Formatter/BothFormatterTest.php index 252b068..586c9d3 100644 --- a/test/Formatter/BothFormatterTest.php +++ b/test/Formatter/BothFormatterTest.php @@ -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\r\nHost: github.com; Response HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n", $result); + $this->assertContains('; Response ', $result); } } \ No newline at end of file diff --git a/test/LogMiddlewareTest.php b/test/LogMiddlewareTest.php index 8722688..c620a64 100644 --- a/test/LogMiddlewareTest.php +++ b/test/LogMiddlewareTest.php @@ -9,6 +9,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; +use UnexpectedValueException; class LogMiddlewareTest extends PHPUnit_Framework_TestCase { @@ -47,7 +48,7 @@ public function testLogMessage() } /** - * @expectedException \UnexpectedValueException + * @expectedException UnexpectedValueException */ public function testTryToLogNullMessage() { From ff7feef818d9a5ddc527fccf26c9ccc0da0ffe5b Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 20:46:11 +0200 Subject: [PATCH 6/9] Falling test for single pass middlewares --- test/LogMiddlewareTest.php | 51 ++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/test/LogMiddlewareTest.php b/test/LogMiddlewareTest.php index c620a64..ecc4b1e 100644 --- a/test/LogMiddlewareTest.php +++ b/test/LogMiddlewareTest.php @@ -2,6 +2,7 @@ namespace PhpMiddlewareTest\LogHttpMessages; +use Interop\Http\ServerMiddleware\DelegateInterface; use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter; use PhpMiddleware\LogHttpMessages\LogMiddleware; use PHPUnit_Framework_TestCase; @@ -13,13 +14,15 @@ 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() { @@ -29,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); @@ -37,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->executeDoublePassMiddleware(); + $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->executeDoublePassMiddleware(); + public function middlewareProvider() + { + return [ + 'double pass' => [function ($test) { + return $test->executeDoublePassMiddleware(); + }], + 'single pass' => [function ($test) { + return $test->executeSinglePassMiddleware(); + }], + ]; } - public function executeDoublePassMiddleware() + protected function executeDoublePassMiddleware() { return call_user_func($this->middleware, $this->request, $this->response, $this->next); } -} \ No newline at end of file + + protected function executeSinglePassMiddleware() + { + return $this->middleware->process($this->request, $this->delegate); + } +} From e24a82073acaa8322db97d6e5ddfdfbeaf8300fa Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 20:48:08 +0200 Subject: [PATCH 7/9] Working tests with single pass --- src/LogMiddleware.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index 4b90a74..a45e32c 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -64,7 +64,17 @@ public function __invoke(ServerRequest $request, Response $response, callable $n public function process(ServerRequest $request, DelegateInterface $delegate) { - + $outResponse = $delegate->process($request); + + $messages = $this->formatter->format($request, $outResponse); + + if (!is_string($messages)) { + throw new UnexpectedValueException(sprintf('%s must return string', HttpMessagesFormatter::class)); + } + + $this->logger->log($this->level, $messages); + + return $outResponse; } - + } From 7e9e87f6d6412795931c288357b3fa72ab2eaf20 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 20:51:44 +0200 Subject: [PATCH 8/9] Refactor --- src/LogMiddleware.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index a45e32c..5a81f15 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -51,30 +51,41 @@ public function __invoke(ServerRequest $request, Response $response, callable $n { $outResponse = $next($request, $response); - $messages = $this->formatter->format($request, $outResponse); - - if (!is_string($messages)) { - throw new UnexpectedValueException(sprintf('%s must return string', HttpMessagesFormatter::class)); - } - - $this->logger->log($this->level, $messages); + $this->logMessages($request, $outResponse); return $outResponse; } + /** + * @param ServerRequest $request + * @param DelegateInterface $delegate + * + * @return Response + */ public function process(ServerRequest $request, DelegateInterface $delegate) { - $outResponse = $delegate->process($request); + $response = $delegate->process($request); - $messages = $this->formatter->format($request, $outResponse); + $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)); } $this->logger->log($this->level, $messages); - - return $outResponse; } } From 387c14cfea16d309ab4f455e3c66ca77400b0a2b Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 20:56:15 +0200 Subject: [PATCH 9/9] Update readme --- README.md | 11 ++++------- composer.json | 3 ++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c6c0e4a..c5d2f99 100644 --- a/README.md +++ b/README.md @@ -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`, diff --git a/composer.json b/composer.json index 90060c3..4b42dc4 100644 --- a/composer.json +++ b/composer.json @@ -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"