2
2
3
3
namespace PhpMiddlewareTest \LogHttpMessages ;
4
4
5
+ use Interop \Http \ServerMiddleware \DelegateInterface ;
5
6
use PhpMiddleware \LogHttpMessages \Formatter \HttpMessagesFormatter ;
6
7
use PhpMiddleware \LogHttpMessages \LogMiddleware ;
7
8
use PHPUnit_Framework_TestCase ;
13
14
14
15
class LogMiddlewareTest extends PHPUnit_Framework_TestCase
15
16
{
16
- protected $ middleware ;
17
+ public $ middleware ;
17
18
protected $ formatter ;
18
19
protected $ logger ;
19
20
protected $ request ;
20
21
protected $ response ;
21
22
protected $ next ;
22
23
protected $ level ;
24
+ protected $ delegate ;
25
+ protected $ nextResponse ;
23
26
24
27
protected function setUp ()
25
28
{
@@ -29,6 +32,8 @@ protected function setUp()
29
32
$ this ->next = function () {
30
33
return $ this ->nextResponse ;
31
34
};
35
+ $ this ->delegate = $ this ->getMock (DelegateInterface::class);
36
+ $ this ->delegate ->method ('process ' )->willReturn ($ this ->nextResponse );
32
37
33
38
$ this ->formatter = $ this ->getMock (HttpMessagesFormatter::class);
34
39
$ this ->logger = $ this ->getMock (LoggerInterface::class);
@@ -37,28 +42,48 @@ protected function setUp()
37
42
$ this ->middleware = new LogMiddleware ($ this ->formatter , $ this ->logger , $ this ->level );
38
43
}
39
44
40
- public function testLogMessage ()
45
+ /**
46
+ * @dataProvider middlewareProvider
47
+ */
48
+ public function testLogFormattedMessages ($ middlewareExecutor )
41
49
{
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 ' );
46
52
47
- $ this -> assertSame ($ this -> nextResponse , $ response );
53
+ $ middlewareExecutor ($ this );
48
54
}
49
55
50
56
/**
51
- * @expectedException UnexpectedValueException
57
+ * @dataProvider middlewareProvider
52
58
*/
53
- public function testTryToLogNullMessage ()
59
+ public function testTryToLogNullMessage ($ middlewareExecutor )
54
60
{
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
+ }
56
67
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
+ ];
58
78
}
59
79
60
- public function executeDoublePassMiddleware ()
80
+ protected function executeDoublePassMiddleware ()
61
81
{
62
82
return call_user_func ($ this ->middleware , $ this ->request , $ this ->response , $ this ->next );
63
83
}
64
- }
84
+
85
+ protected function executeSinglePassMiddleware ()
86
+ {
87
+ return $ this ->middleware ->process ($ this ->request , $ this ->delegate );
88
+ }
89
+ }
0 commit comments