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); } }