8
8
use Doctrine \DBAL \Driver \Connection ;
9
9
use Doctrine \DBAL \Logging \Middleware ;
10
10
use Doctrine \DBAL \ParameterType ;
11
- use PHPUnit \Framework \MockObject \MockObject ;
12
11
use PHPUnit \Framework \TestCase ;
13
- use Psr \Log \LoggerInterface ;
12
+ use Psr \Log \Test \ TestLogger ;
14
13
15
14
class MiddlewareTest extends TestCase
16
15
{
17
16
private Driver $ driver ;
18
-
19
- /** @var LoggerInterface&MockObject */
20
- private LoggerInterface $ logger ;
17
+ private TestLogger $ logger ;
21
18
22
19
public function setUp (): void
23
20
{
@@ -27,104 +24,99 @@ public function setUp(): void
27
24
$ driver ->method ('connect ' )
28
25
->willReturn ($ connection );
29
26
30
- $ this ->logger = $ this -> createMock (LoggerInterface::class );
27
+ $ this ->logger = new TestLogger ( );
31
28
32
29
$ middleware = new Middleware ($ this ->logger );
33
30
$ this ->driver = $ middleware ->wrap ($ driver );
34
31
}
35
32
36
33
public function testConnectAndDisconnect (): void
37
34
{
38
- $ this ->logger ->expects (self ::exactly (2 ))
39
- ->method ('info ' )
40
- ->withConsecutive (
41
- [
42
- 'Connecting with parameters {params} ' ,
43
- [
44
- 'params ' => [
45
- 'username ' => 'admin ' ,
46
- 'password ' => '<redacted> ' ,
47
- 'url ' => '<redacted> ' ,
48
- ],
49
- ],
50
- ],
51
- ['Disconnecting ' , []],
52
- );
53
-
54
35
$ this ->driver ->connect ([
55
36
'username ' => 'admin ' ,
56
37
'password ' => 'Passw0rd! ' ,
57
38
'url ' => 'mysql://user:secret@localhost/mydb ' ,
58
39
]);
40
+
41
+ self ::assertTrue ($ this ->logger ->hasInfo ([
42
+ 'message ' => 'Connecting with parameters {params} ' ,
43
+ 'context ' => [
44
+ 'params ' => [
45
+ 'username ' => 'admin ' ,
46
+ 'password ' => '<redacted> ' ,
47
+ 'url ' => '<redacted> ' ,
48
+ ],
49
+ ],
50
+ ]));
59
51
}
60
52
61
53
public function testQuery (): void
62
54
{
63
- $ this ->logger ->expects (self ::once ())
64
- ->method ('debug ' )
65
- ->with ('Executing query: {sql} ' , ['sql ' => 'SELECT 1 ' ]);
66
-
67
55
$ connection = $ this ->driver ->connect ([]);
68
56
$ connection ->query ('SELECT 1 ' );
57
+
58
+ self ::assertTrue ($ this ->logger ->hasDebug ([
59
+ 'message ' => 'Executing query: {sql} ' ,
60
+ 'context ' => ['sql ' => 'SELECT 1 ' ],
61
+ ]));
69
62
}
70
63
71
64
public function testExec (): void
72
65
{
73
- $ this ->logger ->expects (self ::once ())
74
- ->method ('debug ' )
75
- ->with ('Executing statement: {sql} ' , ['sql ' => 'DROP DATABASE doctrine ' ]);
76
-
77
66
$ connection = $ this ->driver ->connect ([]);
78
67
$ connection ->exec ('DROP DATABASE doctrine ' );
68
+
69
+ self ::assertTrue ($ this ->logger ->hasDebug ([
70
+ 'message ' => 'Executing statement: {sql} ' ,
71
+ 'context ' => ['sql ' => 'DROP DATABASE doctrine ' ],
72
+ ]));
79
73
}
80
74
81
75
public function testBeginCommitRollback (): void
82
76
{
83
- $ this ->logger ->expects (self ::exactly (3 ))
84
- ->method ('debug ' )
85
- ->withConsecutive (
86
- ['Beginning transaction ' ],
87
- ['Committing transaction ' ],
88
- ['Rolling back transaction ' ],
89
- );
90
-
91
77
$ connection = $ this ->driver ->connect ([]);
92
78
$ connection ->beginTransaction ();
93
79
$ connection ->commit ();
94
80
$ connection ->rollBack ();
81
+
82
+ self ::assertTrue ($ this ->logger ->hasDebug ('Beginning transaction ' ));
83
+ self ::assertTrue ($ this ->logger ->hasDebug ('Committing transaction ' ));
84
+ self ::assertTrue ($ this ->logger ->hasDebug ('Rolling back transaction ' ));
95
85
}
96
86
97
87
public function testExecuteStatementWithParameters (): void
98
88
{
99
- $ this ->logger ->expects (self ::once ())
100
- ->method ('debug ' )
101
- ->with ('Executing statement: {sql} (parameters: {params}, types: {types}) ' , [
102
- 'sql ' => 'SELECT ?, ? ' ,
103
- 'params ' => [1 => 42 ],
104
- 'types ' => [1 => ParameterType::INTEGER ],
105
- ]);
106
-
107
89
$ connection = $ this ->driver ->connect ([]);
108
90
$ statement = $ connection ->prepare ('SELECT ?, ? ' );
109
91
$ statement ->bindValue (1 , 42 , ParameterType::INTEGER );
110
92
111
93
$ statement ->execute ();
94
+
95
+ self ::assertTrue ($ this ->logger ->hasDebug ([
96
+ 'message ' => 'Executing statement: {sql} (parameters: {params}, types: {types}) ' ,
97
+ 'context ' => [
98
+ 'sql ' => 'SELECT ?, ? ' ,
99
+ 'params ' => [1 => 42 ],
100
+ 'types ' => [1 => ParameterType::INTEGER ],
101
+ ],
102
+ ]));
112
103
}
113
104
114
105
public function testExecuteStatementWithNamedParameters (): void
115
106
{
116
- $ this ->logger ->expects (self ::once ())
117
- ->method ('debug ' )
118
- ->with ('Executing statement: {sql} (parameters: {params}, types: {types}) ' , [
119
- 'sql ' => 'SELECT :value ' ,
120
- 'params ' => ['value ' => 'Test ' ],
121
- 'types ' => ['value ' => ParameterType::STRING ],
122
- ]);
123
-
124
107
$ connection = $ this ->driver ->connect ([]);
125
108
$ statement = $ connection ->prepare ('SELECT :value ' );
126
109
$ statement ->bindValue ('value ' , 'Test ' , ParameterType::STRING );
127
110
128
111
$ statement ->execute ();
112
+
113
+ self ::assertTrue ($ this ->logger ->hasDebug ([
114
+ 'message ' => 'Executing statement: {sql} (parameters: {params}, types: {types}) ' ,
115
+ 'context ' => [
116
+ 'sql ' => 'SELECT :value ' ,
117
+ 'params ' => ['value ' => 'Test ' ],
118
+ 'types ' => ['value ' => ParameterType::STRING ],
119
+ ],
120
+ ]));
129
121
}
130
122
}
0 commit comments