|
2 | 2 |
|
3 | 3 | namespace PhpMiddlewareTest\LogHttpMessages; |
4 | 4 |
|
5 | | -use Interop\Http\ServerMiddleware\DelegateInterface; |
6 | 5 | use PhpMiddleware\LogHttpMessages\Formatter\EmptyMessageFormatter; |
7 | 6 | use PhpMiddleware\LogHttpMessages\LogMiddleware; |
| 7 | +use PHPUnit\Framework\MockObject\MockObject; |
8 | 8 | use PHPUnit\Framework\TestCase; |
9 | 9 | use Psr\Http\Message\ResponseInterface; |
10 | 10 | use Psr\Http\Message\ServerRequestInterface; |
| 11 | +use Psr\Http\Server\RequestHandlerInterface; |
11 | 12 | use Psr\Log\LoggerInterface; |
12 | 13 | use Psr\Log\LogLevel; |
13 | 14 |
|
14 | 15 | class LogMiddlewareTest extends TestCase |
15 | 16 | { |
| 17 | + /** @var LogMiddleware */ |
16 | 18 | private $middleware; |
| 19 | + /** @var LoggerInterface|MockObject */ |
17 | 20 | private $logger; |
18 | | - private $request; |
19 | | - private $response; |
20 | | - private $next; |
21 | | - private $level; |
22 | | - private $delegate; |
23 | | - private $nextResponse; |
| 21 | + private $handler; |
24 | 22 |
|
25 | 23 | protected function setUp() |
26 | 24 | { |
27 | | - $this->request = $this->createMock(ServerRequestInterface::class); |
28 | | - $this->response = $this->createMock(ResponseInterface::class); |
29 | | - $this->nextResponse = clone $this->response; |
30 | | - $this->next = function () { |
31 | | - return $this->nextResponse; |
32 | | - }; |
33 | | - $this->delegate = $this->createMock(DelegateInterface::class); |
34 | | - $this->delegate->method('process')->willReturn($this->nextResponse); |
| 25 | + $response = $this->createMock(ResponseInterface::class); |
| 26 | + |
| 27 | + $this->handler = $this->createMock(RequestHandlerInterface::class); |
| 28 | + $this->handler->method('handle')->willReturn($response); |
35 | 29 |
|
36 | 30 | $formatter = new EmptyMessageFormatter(); |
37 | 31 | $this->logger = $this->createMock(LoggerInterface::class); |
38 | | - $this->level = LogLevel::ALERT; |
39 | 32 |
|
40 | | - $this->middleware = new LogMiddleware($formatter, $formatter, $this->logger, $this->level); |
| 33 | + $this->middleware = new LogMiddleware($formatter, $formatter, $this->logger, LogLevel::ALERT); |
41 | 34 | } |
42 | 35 |
|
43 | | - /** |
44 | | - * @dataProvider middlewareProvider |
45 | | - */ |
46 | | - public function testLogFormattedMessages($middlewareExecutor) |
| 36 | + public function testLogFormattedMessages() |
47 | 37 | { |
48 | | - $this->logger->expects($this->once())->method('log')->with($this->level, LogMiddleware::LOG_MESSAGE, ['request' => null, 'response' => null]); |
49 | | - |
50 | | - $middlewareExecutor($this); |
51 | | - } |
| 38 | + /** @var ServerRequestInterface|MockObject $request */ |
| 39 | + $request = $this->createMock(ServerRequestInterface::class); |
52 | 40 |
|
53 | | - public function middlewareProvider() |
54 | | - { |
55 | | - return [ |
56 | | - 'double pass' => [function ($test) { |
57 | | - return $test->executeDoublePassMiddleware(); |
58 | | - }], |
59 | | - 'single pass' => [function ($test) { |
60 | | - return $test->executeSinglePassMiddleware(); |
61 | | - }], |
62 | | - ]; |
63 | | - } |
| 41 | + $this->logger->expects($this->once())->method('log') |
| 42 | + ->with(LogLevel::ALERT, LogMiddleware::LOG_MESSAGE, ['request' => null, 'response' => null]); |
64 | 43 |
|
65 | | - protected function executeDoublePassMiddleware() |
66 | | - { |
67 | | - return call_user_func($this->middleware, $this->request, $this->response, $this->next); |
68 | | - } |
69 | | - |
70 | | - protected function executeSinglePassMiddleware() |
71 | | - { |
72 | | - return $this->middleware->process($this->request, $this->delegate); |
| 44 | + $this->middleware->process($request, $this->handler); |
73 | 45 | } |
74 | 46 | } |
0 commit comments