22
33namespace PhpMiddlewareTest \LogHttpMessages ;
44
5+ use Interop \Http \ServerMiddleware \DelegateInterface ;
56use PhpMiddleware \LogHttpMessages \Formatter \HttpMessagesFormatter ;
67use PhpMiddleware \LogHttpMessages \LogMiddleware ;
78use PHPUnit_Framework_TestCase ;
89use Psr \Http \Message \ResponseInterface ;
910use Psr \Http \Message \ServerRequestInterface ;
1011use Psr \Log \LoggerInterface ;
1112use Psr \Log \LogLevel ;
13+ use UnexpectedValueException ;
1214
1315class LogMiddlewareTest extends PHPUnit_Framework_TestCase
1416{
15- protected $ middleware ;
17+ public $ middleware ;
1618 protected $ formatter ;
1719 protected $ logger ;
1820 protected $ request ;
1921 protected $ response ;
2022 protected $ next ;
2123 protected $ level ;
24+ protected $ delegate ;
25+ protected $ nextResponse ;
2226
2327 protected function setUp ()
2428 {
@@ -28,6 +32,8 @@ protected function setUp()
2832 $ this ->next = function () {
2933 return $ this ->nextResponse ;
3034 };
35+ $ this ->delegate = $ this ->getMock (DelegateInterface::class);
36+ $ this ->delegate ->method ('process ' )->willReturn ($ this ->nextResponse );
3137
3238 $ this ->formatter = $ this ->getMock (HttpMessagesFormatter::class);
3339 $ this ->logger = $ this ->getMock (LoggerInterface::class);
@@ -36,28 +42,48 @@ protected function setUp()
3642 $ this ->middleware = new LogMiddleware ($ this ->formatter , $ this ->logger , $ this ->level );
3743 }
3844
39- public function testLogMessage ()
45+ /**
46+ * @dataProvider middlewareProvider
47+ */
48+ public function testLogFormattedMessages ($ middlewareExecutor )
4049 {
41- $ this ->formatter ->expects ($ this ->once ())->method ('format ' )->with ($ this ->request , $ this ->nextResponse )->willReturn ('boo ' );
42- $ this ->logger ->expects ($ this ->once ())->method ('log ' )->with ($ this ->level , 'boo ' );
43-
44- $ response = $ this ->executeMiddleware ();
50+ $ this ->formatter ->method ('format ' )->with ($ this ->request , $ this ->nextResponse )->willReturn ('formattedMessages ' );
51+ $ this ->logger ->expects ($ this ->once ())->method ('log ' )->with ($ this ->level , 'formattedMessages ' );
4552
46- $ this -> assertSame ($ this -> nextResponse , $ response );
53+ $ middlewareExecutor ($ this );
4754 }
4855
4956 /**
50- * @expectedException \UnexpectedValueException
57+ * @dataProvider middlewareProvider
5158 */
52- public function testTryToLogNullMessage ()
59+ public function testTryToLogNullMessage ($ middlewareExecutor )
5360 {
54- $ 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+ }
5567
56- $ this ->executeMiddleware ();
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+ ];
5778 }
5879
59- public function executeMiddleware ()
80+ protected function executeDoublePassMiddleware ()
6081 {
6182 return call_user_func ($ this ->middleware , $ this ->request , $ this ->response , $ this ->next );
6283 }
63- }
84+
85+ protected function executeSinglePassMiddleware ()
86+ {
87+ return $ this ->middleware ->process ($ this ->request , $ this ->delegate );
88+ }
89+ }
0 commit comments