Skip to content

Commit

Permalink
Merge pull request #7 from gardziejewskik/master
Browse files Browse the repository at this point in the history
Support only PSRs standards
  • Loading branch information
snapshotpl authored Nov 17, 2018
2 parents 87fdca7 + f5b2a9f commit 6ef29f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 54 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 5 additions & 7 deletions src/LogMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
60 changes: 16 additions & 44 deletions test/LogMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 6ef29f8

Please sign in to comment.