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