From 9bd5a8cb9d435e306c9bdad6c8bf708b479ab279 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 21:03:04 +0200 Subject: [PATCH 1/6] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c5d2f99..c7af144 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,4 @@ Middleware should works with: * [Slim 3.x](https://github.com/slimphp/Slim) * [zend-log 2.6](https://github.com/zendframework/zend-log) -And any other modern framework [supported middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html) and [PSR-3 implementation](http://www.php-fig.org/psr/psr-3/) logger. +And any other modern framework [supported PSR-15 middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html) and [PSR-3 implementation](http://www.php-fig.org/psr/psr-3/) logger. From ba936f7ecfee7d28a8f5f17ec4e9e0b14b44ee76 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Sat, 29 Apr 2017 21:13:55 +0200 Subject: [PATCH 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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