From ba936f7ecfee7d28a8f5f17ec4e9e0b14b44ee76 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 21:13:55 +0200 Subject: [PATCH 01/10] Remove php5 support --- .travis.yml | 5 +---- composer.json | 4 ++-- src/LogMiddleware.php | 18 +++--------------- test/Formatter/BothFormatterTest.php | 4 ++-- test/LogMiddlewareTest.php | 16 ++++++++-------- 5 files changed, 16 insertions(+), 31 deletions(-) diff --git a/.travis.yml b/.travis.yml index 30f4ed9..4f28114 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,13 @@ language: php php: - - 5.5 - - 5.6 - 7.0 - 7.1 - - hhvm env: - COMPOSER_FLAGS=--prefer-lowest - COMPOSER_FLAGS= - + before_script: - composer update --no-interaction --no-suggest --prefer-dist $COMPOSER_FLAGS diff --git a/composer.json b/composer.json index 4b42dc4..201c166 100644 --- a/composer.json +++ b/composer.json @@ -13,14 +13,14 @@ "psr-3" ], "require": { - "php": ">=5.5", + "php": ">=7.0", "psr/log": "^1.0.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.1.3", "http-interop/http-middleware": "^0.4.1" }, "require-dev": { - "phpunit/phpunit": "^4.8" + "phpunit/phpunit": "^6.1" }, "autoload": { "psr-4": { diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index 5a81f15..ce281e8 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -1,5 +1,7 @@ formatter = $formatter; diff --git a/test/Formatter/BothFormatterTest.php b/test/Formatter/BothFormatterTest.php index 586c9d3..093e175 100644 --- a/test/Formatter/BothFormatterTest.php +++ b/test/Formatter/BothFormatterTest.php @@ -5,11 +5,11 @@ use PhpMiddleware\LogHttpMessages\Formatter\BothFormatter; use PhpMiddleware\LogHttpMessages\Formatter\RequestFormatter; use PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequest; -class BothFormatterTest extends PHPUnit_Framework_TestCase +class BothFormatterTest extends TestCase { protected $formatter; diff --git a/test/LogMiddlewareTest.php b/test/LogMiddlewareTest.php index ecc4b1e..1984cdd 100644 --- a/test/LogMiddlewareTest.php +++ b/test/LogMiddlewareTest.php @@ -5,14 +5,14 @@ use Interop\Http\ServerMiddleware\DelegateInterface; use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter; use PhpMiddleware\LogHttpMessages\LogMiddleware; -use PHPUnit_Framework_TestCase; +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 +class LogMiddlewareTest extends TestCase { public $middleware; protected $formatter; @@ -26,17 +26,17 @@ class LogMiddlewareTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->request = $this->getMock(ServerRequestInterface::class); - $this->response = $this->getMock(ResponseInterface::class); + $this->request = $this->createMock(ServerRequestInterface::class); + $this->response = $this->createMock(ResponseInterface::class); $this->nextResponse = clone $this->response; $this->next = function () { return $this->nextResponse; }; - $this->delegate = $this->getMock(DelegateInterface::class); + $this->delegate = $this->createMock(DelegateInterface::class); $this->delegate->method('process')->willReturn($this->nextResponse); - $this->formatter = $this->getMock(HttpMessagesFormatter::class); - $this->logger = $this->getMock(LoggerInterface::class); + $this->formatter = $this->createMock(HttpMessagesFormatter::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->level = LogLevel::ALERT; $this->middleware = new LogMiddleware($this->formatter, $this->logger, $this->level); @@ -60,7 +60,7 @@ public function testTryToLogNullMessage($middlewareExecutor) { $this->formatter->method('format')->willReturn(null); - $this->setExpectedException(UnexpectedValueException::class); + $this->expectException(UnexpectedValueException::class); $middlewareExecutor($this); } From 81bb78bf6410446de1e4ed5e1f0fe180aacd8303 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 22:31:47 +0200 Subject: [PATCH 02/10] Refactored formatting --- src/Formatter/BothFormatter.php | 36 ---------- src/Formatter/EmptyMessageFormatter.php | 24 +++++++ src/Formatter/FormattedMessage.php | 39 +++++++++++ src/Formatter/HttpMessagesFormatter.php | 17 ----- src/Formatter/RequestFormatter.php | 15 ----- src/Formatter/ResponseFormatter.php | 11 ++- src/Formatter/ServerRequestFormatter.php | 12 ++++ .../ZendDiactorosToArrayMessageFormatter.php | 28 ++++++++ .../ZendDiactorosToStringMessageFormatter.php | 27 ++++++++ src/LogMiddleware.php | 67 ++++++++----------- test/Formatter/BothFormatterTest.php | 33 --------- test/Formatter/FormattedMessageTest.php | 30 +++++++++ ...ndDiactorosToArrayMessageFormatterTest.php | 31 +++++++++ ...dDiactorosToStringMessageFormatterTest.php | 31 +++++++++ test/LogMiddlewareTest.php | 39 ++++------- 15 files changed, 266 insertions(+), 174 deletions(-) delete mode 100644 src/Formatter/BothFormatter.php create mode 100644 src/Formatter/EmptyMessageFormatter.php create mode 100644 src/Formatter/FormattedMessage.php delete mode 100644 src/Formatter/HttpMessagesFormatter.php delete mode 100644 src/Formatter/RequestFormatter.php create mode 100644 src/Formatter/ServerRequestFormatter.php create mode 100644 src/Formatter/ZendDiactorosToArrayMessageFormatter.php create mode 100644 src/Formatter/ZendDiactorosToStringMessageFormatter.php delete mode 100644 test/Formatter/BothFormatterTest.php create mode 100644 test/Formatter/FormattedMessageTest.php create mode 100644 test/Formatter/ZendDiactorosToArrayMessageFormatterTest.php create mode 100644 test/Formatter/ZendDiactorosToStringMessageFormatterTest.php diff --git a/src/Formatter/BothFormatter.php b/src/Formatter/BothFormatter.php deleted file mode 100644 index ab95a2f..0000000 --- a/src/Formatter/BothFormatter.php +++ /dev/null @@ -1,36 +0,0 @@ -requestFormatter = $requestFormatter; - $this->responseFormatter = $responseFormatter; - } - - /** - * @param ServerRequestInterface $request - * @param ResponseInterface $response - * - * @return string - */ - public function format(ServerRequestInterface $request, ResponseInterface $response) - { - $requestString = $this->requestFormatter->format($request, $response); - $reponseString = $this->responseFormatter->format($request, $response); - - return sprintf('Request: %s; Response %s', $requestString, $reponseString); - } -} \ No newline at end of file diff --git a/src/Formatter/EmptyMessageFormatter.php b/src/Formatter/EmptyMessageFormatter.php new file mode 100644 index 0000000..94b13cf --- /dev/null +++ b/src/Formatter/EmptyMessageFormatter.php @@ -0,0 +1,24 @@ +value = $value; + + return $instance; + } + + public static function fromArray(array $value) : self + { + $instance = new self(); + $instance->value = $value; + + return $instance; + } + + public static function createEmpty() : self + { + return new self(); + } + + /** + * @return array|string|null + */ + public function getValue() + { + return $this->value; + } +} diff --git a/src/Formatter/HttpMessagesFormatter.php b/src/Formatter/HttpMessagesFormatter.php deleted file mode 100644 index df21cb1..0000000 --- a/src/Formatter/HttpMessagesFormatter.php +++ /dev/null @@ -1,17 +0,0 @@ -formatter = $formatter; + const LOG_MESSAGE = 'Request/Response'; + + private $logger; + private $level; + private $requestFormatter; + private $responseFormatter; + private $logMessage; + + public function __construct( + ServerRequestFormatter $requestFormatter, + ResponseFormatter $responseFormatter, + Logger $logger, + string $level = LogLevel::INFO, + string $logMessage = self::LOG_MESSAGE + ) { + $this->requestFormatter = $requestFormatter; + $this->responseFormatter = $responseFormatter; $this->logger = $logger; $this->level = $level; + $this->logMessage = $logMessage; } - /** - * @param ServerRequest $request - * @param Response $response - * @param callable $next - * - * @return Response - */ - public function __invoke(ServerRequest $request, Response $response, callable $next) + public function __invoke(ServerRequest $request, Response $response, callable $next) : Response { $outResponse = $next($request, $response); @@ -44,13 +46,7 @@ public function __invoke(ServerRequest $request, Response $response, callable $n return $outResponse; } - /** - * @param ServerRequest $request - * @param DelegateInterface $delegate - * - * @return Response - */ - public function process(ServerRequest $request, DelegateInterface $delegate) + public function process(ServerRequest $request, DelegateInterface $delegate) : Response { $response = $delegate->process($request); @@ -59,21 +55,14 @@ public function process(ServerRequest $request, DelegateInterface $delegate) 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)); - } + $formattedRequest = $this->requestFormatter->formatServerRequest($request); + $formattedResponse = $this->responseFormatter->formatResponse($response); - $this->logger->log($this->level, $messages); + $this->logger->log($this->level, $this->logMessage, [ + 'request' => $formattedRequest->getValue(), + 'response' => $formattedResponse->getValue(), + ]); } - } diff --git a/test/Formatter/BothFormatterTest.php b/test/Formatter/BothFormatterTest.php deleted file mode 100644 index 093e175..0000000 --- a/test/Formatter/BothFormatterTest.php +++ /dev/null @@ -1,33 +0,0 @@ -formatter = new BothFormatter($requestFormatter, $responseFormatter); - } - - public function testFormatter() - { - $request = new ServerRequest([], [], 'https://github.com/php-middleware/log-http-messages', 'GET', 'php://input', ['Accept' => 'text/html']); - $response = new Response('php://memory', 500, ['Content-Type' => 'text/html']); - - $result = $this->formatter->format($request, $response); - - $this->assertContains('; Response ', $result); - } -} \ No newline at end of file diff --git a/test/Formatter/FormattedMessageTest.php b/test/Formatter/FormattedMessageTest.php new file mode 100644 index 0000000..d17bebf --- /dev/null +++ b/test/Formatter/FormattedMessageTest.php @@ -0,0 +1,30 @@ +assertSame('foo', $formattedMessage->getValue()); + } + + public function testCanCreateFromArray() + { + $formattedMessage = FormattedMessage::fromArray(['boo' => 'baz']); + + $this->assertSame(['boo' => 'baz'], $formattedMessage->getValue()); + } + + public function testCanCreateEmpty() + { + $formattedMessage = FormattedMessage::createEmpty(); + + $this->assertNull($formattedMessage->getValue()); + } +} diff --git a/test/Formatter/ZendDiactorosToArrayMessageFormatterTest.php b/test/Formatter/ZendDiactorosToArrayMessageFormatterTest.php new file mode 100644 index 0000000..722ae5d --- /dev/null +++ b/test/Formatter/ZendDiactorosToArrayMessageFormatterTest.php @@ -0,0 +1,31 @@ +formatServerRequest($request); + + $this->assertInternalType('array', $formattedMessage->getValue()); + } + + public function testFormatResponeToArray() + { + $response = new Response(); + $formatter = new ZendDiactorosToArrayMessageFormatter(); + + $formattedMessage = $formatter->formatResponse($response); + + $this->assertInternalType('array', $formattedMessage->getValue()); + } +} diff --git a/test/Formatter/ZendDiactorosToStringMessageFormatterTest.php b/test/Formatter/ZendDiactorosToStringMessageFormatterTest.php new file mode 100644 index 0000000..3c4c511 --- /dev/null +++ b/test/Formatter/ZendDiactorosToStringMessageFormatterTest.php @@ -0,0 +1,31 @@ +formatServerRequest($request); + + $this->assertInternalType('string', $formattedMessage->getValue()); + } + + public function testFormatResponeToArray() + { + $response = new Response(); + $formatter = new ZendDiactorosToStringMessageFormatter(); + + $formattedMessage = $formatter->formatResponse($response); + + $this->assertInternalType('string', $formattedMessage->getValue()); + } +} diff --git a/test/LogMiddlewareTest.php b/test/LogMiddlewareTest.php index 1984cdd..68af43b 100644 --- a/test/LogMiddlewareTest.php +++ b/test/LogMiddlewareTest.php @@ -3,26 +3,24 @@ namespace PhpMiddlewareTest\LogHttpMessages; use Interop\Http\ServerMiddleware\DelegateInterface; -use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter; +use PhpMiddleware\LogHttpMessages\Formatter\EmptyMessageFormatter; 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 TestCase { - public $middleware; - protected $formatter; - protected $logger; - protected $request; - protected $response; - protected $next; - protected $level; - protected $delegate; - protected $nextResponse; + private $middleware; + private $logger; + private $request; + private $response; + private $next; + private $level; + private $delegate; + private $nextResponse; protected function setUp() { @@ -35,11 +33,11 @@ protected function setUp() $this->delegate = $this->createMock(DelegateInterface::class); $this->delegate->method('process')->willReturn($this->nextResponse); - $this->formatter = $this->createMock(HttpMessagesFormatter::class); + $formatter = new EmptyMessageFormatter(); $this->logger = $this->createMock(LoggerInterface::class); $this->level = LogLevel::ALERT; - $this->middleware = new LogMiddleware($this->formatter, $this->logger, $this->level); + $this->middleware = new LogMiddleware($formatter, $formatter, $this->logger, $this->level); } /** @@ -47,20 +45,7 @@ protected function setUp() */ public function testLogFormattedMessages($middlewareExecutor) { - $this->formatter->method('format')->with($this->request, $this->nextResponse)->willReturn('formattedMessages'); - $this->logger->expects($this->once())->method('log')->with($this->level, 'formattedMessages'); - - $middlewareExecutor($this); - } - - /** - * @dataProvider middlewareProvider - */ - public function testTryToLogNullMessage($middlewareExecutor) - { - $this->formatter->method('format')->willReturn(null); - - $this->expectException(UnexpectedValueException::class); + $this->logger->expects($this->once())->method('log')->with($this->level, LogMiddleware::LOG_MESSAGE, ['request' => null, 'response' => null]); $middlewareExecutor($this); } From 0a6a12eda8c2846726b492c199619700563ee3ca Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 23:17:03 +0200 Subject: [PATCH 03/10] Restrict diactoros version --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 201c166..3606221 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,9 @@ ], "require": { "php": ">=7.0", - "psr/log": "^1.0.0", + "psr/log": "^1.0", "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.1.3", + "zendframework/zend-diactoros": "^1.4", "http-interop/http-middleware": "^0.4.1" }, "require-dev": { From 67f1d7b0161d1d3f1fd5d9fad2ef080b44ae7f6f Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sun, 30 Apr 2017 00:10:08 +0200 Subject: [PATCH 04/10] Update readme --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c7af144..e8dc37c 100644 --- a/README.md +++ b/README.md @@ -10,21 +10,21 @@ Support double and single (PSR-15) pass middleware. composer require php-middleware/log-http-messages ``` -To log http messages you need pass into `LogRequestMiddleware` implementation of `PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter`, -instance `Psr\Log\LoggerInterface` and add middleware to your middleware runner. -Third parameter is log level and it's optional (`Psr\Log\LogLevel::INFO` as default). +To log http messages you need pass into `LogRequestMiddleware` implementation of +`PhpMiddleware\LogHttpMessages\Formatter\ServerRequestFormatter`, +`PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter`, +instance `Psr\Log\LoggerInterface` and add this middleware to your middleware runner. +You can also set log level (`Psr\Log\LogLevel::INFO` as default) and log message (`Request/Response` as default). -There are tree implementation of `PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter`: +Provided implementation of formatters: -* `PhpMiddleware\LogHttpMessages\Formatter\RequestFormatter` to log request message, -* `PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter` to log response message, -* `PhpMiddleware\LogHttpMessages\Formatter\BothFormatter` to log request and response message. +* `PhpMiddleware\LogHttpMessages\Formatter\EmptyMessageFormatter`, +* `PhpMiddleware\LogHttpMessages\Formatter\ZendDiactorosToArrayMessageFormatter`, +* `PhpMiddleware\LogHttpMessages\Formatter\ZendDiactorosToStringMessageFormatter`. ```php -$requestFormatter = PhpMiddleware\LogHttpMessages\Formatter\RequestFormatter(); -$responseFormatter = PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter(); -$formatter = new PhpMiddleware\LogHttpMessages\Formatter\BothFormatter(requestFormatter, responseFormatter); -$logMiddleware = new PhpMiddleware\LogHttpMessages\LogMiddleware(formatter, $logger); +$formatter = PhpMiddleware\LogHttpMessages\Formatter\ZendDiactorosToArrayMessageFormatter(); +$logMiddleware = new PhpMiddleware\LogHttpMessages\LogMiddleware($formatter, $formatter, $logger); $app = new MiddlewareRunner(); $app->add($logMiddleware); From 8baad8bff07d143820429665f7fa55b2d77ef683 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sun, 30 Apr 2017 00:14:08 +0200 Subject: [PATCH 05/10] Speedup travis --- .travis.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4f28114..a7f1afe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,16 @@ env: - COMPOSER_FLAGS=--prefer-lowest - COMPOSER_FLAGS= -before_script: +before_install: + - phpenv config-rm xdebug.ini + +install: - composer update --no-interaction --no-suggest --prefer-dist $COMPOSER_FLAGS script: - vendor/bin/phpunit + +cache: + directories: + - $HOME/.composer/cache + - vendor From 9a3ef23f472beea6065aa1399a213efd2dbbe948 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sun, 30 Apr 2017 00:22:32 +0200 Subject: [PATCH 06/10] Add license --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..301f924 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 PHP Middleware + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From c5896cd47add54b04242efcfc954a42464c61342 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Thu, 11 May 2017 19:52:07 +0200 Subject: [PATCH 07/10] Use double-pass-compatibility --- composer.json | 3 ++- src/LogMiddleware.php | 21 +++++---------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/composer.json b/composer.json index 3606221..1f88730 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "psr/log": "^1.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.4", - "http-interop/http-middleware": "^0.4.1" + "http-interop/http-middleware": "^0.4.1", + "php-middleware/double-pass-compatibility": "^1.0" }, "require-dev": { "phpunit/phpunit": "^6.1" diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index 08cdc00..a95f3e5 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -6,6 +6,7 @@ use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface; +use PhpMiddleware\DoublePassCompatibilityTrait; use PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter; use PhpMiddleware\LogHttpMessages\Formatter\ServerRequestFormatter; use Psr\Http\Message\ResponseInterface as Response; @@ -15,6 +16,8 @@ final class LogMiddleware implements MiddlewareInterface { + use DoublePassCompatibilityTrait; + const LOG_MESSAGE = 'Request/Response'; private $logger; @@ -37,26 +40,10 @@ public function __construct( $this->logMessage = $logMessage; } - public function __invoke(ServerRequest $request, Response $response, callable $next) : Response - { - $outResponse = $next($request, $response); - - $this->logMessages($request, $outResponse); - - return $outResponse; - } - public function process(ServerRequest $request, DelegateInterface $delegate) : Response { $response = $delegate->process($request); - $this->logMessages($request, $response); - - return $response; - } - - private function logMessages(ServerRequest $request, Response $response) - { $formattedRequest = $this->requestFormatter->formatServerRequest($request); $formattedResponse = $this->responseFormatter->formatResponse($response); @@ -64,5 +51,7 @@ private function logMessages(ServerRequest $request, Response $response) 'request' => $formattedRequest->getValue(), 'response' => $formattedResponse->getValue(), ]); + + return $response; } } From 0dbc4a5f91ba7306889aeba97bc8f325e508fcf0 Mon Sep 17 00:00:00 2001 From: Krzysztof Gardziejewski Date: Fri, 16 Nov 2018 19:07:59 +0100 Subject: [PATCH 08/10] Support zend-diacrotos v2 --- .travis.yml | 1 + composer.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a7f1afe..23364b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 7.0 - 7.1 + - 7.2 env: - COMPOSER_FLAGS=--prefer-lowest diff --git a/composer.json b/composer.json index 1f88730..f47643a 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "php": ">=7.0", "psr/log": "^1.0", "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.4", + "zendframework/zend-diactoros": "^1.4 || ^2.0", "http-interop/http-middleware": "^0.4.1", "php-middleware/double-pass-compatibility": "^1.0" }, From f5b2a9fe282d46294d896f9bf5a03cc2ce970b12 Mon Sep 17 00:00:00 2001 From: Krzysztof Gardziejewski Date: Sat, 17 Nov 2018 10:05:15 +0100 Subject: [PATCH 09/10] Added support PSR-15 middleware Dropped support http-interop/http-middleware --- composer.json | 6 ++-- src/LogMiddleware.php | 12 ++++---- test/LogMiddlewareTest.php | 60 ++++++++++---------------------------- 3 files changed, 24 insertions(+), 54 deletions(-) diff --git a/composer.json b/composer.json index f47643a..813df82 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,9 @@ "php": ">=7.0", "psr/log": "^1.0", "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.4 || ^2.0", - "http-interop/http-middleware": "^0.4.1", - "php-middleware/double-pass-compatibility": "^1.0" + "psr/http-server-handler": "^1.0", + "psr/http-server-middleware": "^1.0", + "zendframework/zend-diactoros": "^1.4 || ^2.0" }, "require-dev": { "phpunit/phpunit": "^6.1" diff --git a/src/LogMiddleware.php b/src/LogMiddleware.php index a95f3e5..2ba6ee4 100644 --- a/src/LogMiddleware.php +++ b/src/LogMiddleware.php @@ -4,20 +4,17 @@ namespace PhpMiddleware\LogHttpMessages; -use Interop\Http\ServerMiddleware\DelegateInterface; -use Interop\Http\ServerMiddleware\MiddlewareInterface; -use PhpMiddleware\DoublePassCompatibilityTrait; use PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter; use PhpMiddleware\LogHttpMessages\Formatter\ServerRequestFormatter; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as ServerRequest; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface as Logger; use Psr\Log\LogLevel; final class LogMiddleware implements MiddlewareInterface { - use DoublePassCompatibilityTrait; - const LOG_MESSAGE = 'Request/Response'; private $logger; @@ -40,9 +37,10 @@ public function __construct( $this->logMessage = $logMessage; } - public function process(ServerRequest $request, DelegateInterface $delegate) : Response + /** @inheritdoc */ + public function process(ServerRequest $request, RequestHandlerInterface $handler): Response { - $response = $delegate->process($request); + $response = $handler->handle($request); $formattedRequest = $this->requestFormatter->formatServerRequest($request); $formattedResponse = $this->responseFormatter->formatResponse($response); diff --git a/test/LogMiddlewareTest.php b/test/LogMiddlewareTest.php index 68af43b..cf0596a 100644 --- a/test/LogMiddlewareTest.php +++ b/test/LogMiddlewareTest.php @@ -2,73 +2,45 @@ namespace PhpMiddlewareTest\LogHttpMessages; -use Interop\Http\ServerMiddleware\DelegateInterface; use PhpMiddleware\LogHttpMessages\Formatter\EmptyMessageFormatter; use PhpMiddleware\LogHttpMessages\LogMiddleware; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; class LogMiddlewareTest extends TestCase { + /** @var LogMiddleware */ private $middleware; + /** @var LoggerInterface|MockObject */ private $logger; - private $request; - private $response; - private $next; - private $level; - private $delegate; - private $nextResponse; + private $handler; protected function setUp() { - $this->request = $this->createMock(ServerRequestInterface::class); - $this->response = $this->createMock(ResponseInterface::class); - $this->nextResponse = clone $this->response; - $this->next = function () { - return $this->nextResponse; - }; - $this->delegate = $this->createMock(DelegateInterface::class); - $this->delegate->method('process')->willReturn($this->nextResponse); + $response = $this->createMock(ResponseInterface::class); + + $this->handler = $this->createMock(RequestHandlerInterface::class); + $this->handler->method('handle')->willReturn($response); $formatter = new EmptyMessageFormatter(); $this->logger = $this->createMock(LoggerInterface::class); - $this->level = LogLevel::ALERT; - $this->middleware = new LogMiddleware($formatter, $formatter, $this->logger, $this->level); + $this->middleware = new LogMiddleware($formatter, $formatter, $this->logger, LogLevel::ALERT); } - /** - * @dataProvider middlewareProvider - */ - public function testLogFormattedMessages($middlewareExecutor) + public function testLogFormattedMessages() { - $this->logger->expects($this->once())->method('log')->with($this->level, LogMiddleware::LOG_MESSAGE, ['request' => null, 'response' => null]); - - $middlewareExecutor($this); - } + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); - public function middlewareProvider() - { - return [ - 'double pass' => [function ($test) { - return $test->executeDoublePassMiddleware(); - }], - 'single pass' => [function ($test) { - return $test->executeSinglePassMiddleware(); - }], - ]; - } + $this->logger->expects($this->once())->method('log') + ->with(LogLevel::ALERT, LogMiddleware::LOG_MESSAGE, ['request' => null, 'response' => null]); - 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); + $this->middleware->process($request, $this->handler); } } From 3c1b0bf70fbbbdd39944a047bafffce0dd98d573 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 17 Nov 2018 11:14:08 +0100 Subject: [PATCH 10/10] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index e8dc37c..e3379b2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ 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