Skip to content

Commit ff7feef

Browse files
committed
Falling test for single pass middlewares
1 parent 59d8428 commit ff7feef

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

test/LogMiddlewareTest.php

+38-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpMiddlewareTest\LogHttpMessages;
44

5+
use Interop\Http\ServerMiddleware\DelegateInterface;
56
use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter;
67
use PhpMiddleware\LogHttpMessages\LogMiddleware;
78
use PHPUnit_Framework_TestCase;
@@ -13,13 +14,15 @@
1314

1415
class LogMiddlewareTest extends PHPUnit_Framework_TestCase
1516
{
16-
protected $middleware;
17+
public $middleware;
1718
protected $formatter;
1819
protected $logger;
1920
protected $request;
2021
protected $response;
2122
protected $next;
2223
protected $level;
24+
protected $delegate;
25+
protected $nextResponse;
2326

2427
protected function setUp()
2528
{
@@ -29,6 +32,8 @@ protected function setUp()
2932
$this->next = function () {
3033
return $this->nextResponse;
3134
};
35+
$this->delegate = $this->getMock(DelegateInterface::class);
36+
$this->delegate->method('process')->willReturn($this->nextResponse);
3237

3338
$this->formatter = $this->getMock(HttpMessagesFormatter::class);
3439
$this->logger = $this->getMock(LoggerInterface::class);
@@ -37,28 +42,48 @@ protected function setUp()
3742
$this->middleware = new LogMiddleware($this->formatter, $this->logger, $this->level);
3843
}
3944

40-
public function testLogMessage()
45+
/**
46+
* @dataProvider middlewareProvider
47+
*/
48+
public function testLogFormattedMessages($middlewareExecutor)
4149
{
42-
$this->formatter->expects($this->once())->method('format')->with($this->request, $this->nextResponse)->willReturn('boo');
43-
$this->logger->expects($this->once())->method('log')->with($this->level, 'boo');
44-
45-
$response = $this->executeDoublePassMiddleware();
50+
$this->formatter->method('format')->with($this->request, $this->nextResponse)->willReturn('formattedMessages');
51+
$this->logger->expects($this->once())->method('log')->with($this->level, 'formattedMessages');
4652

47-
$this->assertSame($this->nextResponse, $response);
53+
$middlewareExecutor($this);
4854
}
4955

5056
/**
51-
* @expectedException UnexpectedValueException
57+
* @dataProvider middlewareProvider
5258
*/
53-
public function testTryToLogNullMessage()
59+
public function testTryToLogNullMessage($middlewareExecutor)
5460
{
55-
$this->formatter->expects($this->once())->method('format')->willReturn(null);
61+
$this->formatter->method('format')->willReturn(null);
62+
63+
$this->setExpectedException(UnexpectedValueException::class);
64+
65+
$middlewareExecutor($this);
66+
}
5667

57-
$this->executeDoublePassMiddleware();
68+
public function middlewareProvider()
69+
{
70+
return [
71+
'double pass' => [function ($test) {
72+
return $test->executeDoublePassMiddleware();
73+
}],
74+
'single pass' => [function ($test) {
75+
return $test->executeSinglePassMiddleware();
76+
}],
77+
];
5878
}
5979

60-
public function executeDoublePassMiddleware()
80+
protected function executeDoublePassMiddleware()
6181
{
6282
return call_user_func($this->middleware, $this->request, $this->response, $this->next);
6383
}
64-
}
84+
85+
protected function executeSinglePassMiddleware()
86+
{
87+
return $this->middleware->process($this->request, $this->delegate);
88+
}
89+
}

0 commit comments

Comments
 (0)