22
22
23
23
class Neo4jLoggerTest extends EnvironmentAwareIntegrationTest
24
24
{
25
+ /**
26
+ * @psalm-suppress PossiblyUndefinedIntArrayOffset
27
+ * @psalm-suppress PossiblyUndefinedStringArrayOffset
28
+ */
25
29
public function testLogger (): void
26
30
{
27
31
if (str_contains ($ this ->getUri ()->getScheme (), 'http ' )) {
@@ -32,127 +36,69 @@ public function testLogger(): void
32
36
self ::markTestSkipped ('This test is not applicable clusters ' );
33
37
}
34
38
35
- // Close connections so that we can test the logger logging
36
- // during authentication while acquiring a new connection
37
39
$ this ->driver ->closeConnections ();
38
40
39
41
/** @var MockObject $logger */
40
42
$ logger = $ this ->getNeo4jLogger ()->getLogger ();
41
43
/** @var Session $session */
42
44
$ session = $ this ->getSession ();
43
45
44
- /** @var array<int, array> $infoLogs */
46
+ // –– INFO logs (unchanged) ––
45
47
$ infoLogs = [];
46
- $ expectedInfoLogs = [
47
- [
48
- 'Running statements ' ,
49
- [
50
- 'statements ' => [new Statement ('RETURN 1 as test ' , [])],
51
- ],
52
- ],
53
- [
54
- 'Starting instant transaction ' ,
55
- [
56
- 'config ' => new TransactionConfiguration (null , null ),
57
- ],
58
- ],
59
- [
60
- 'Acquiring connection ' ,
61
- [
62
- 'config ' => new TransactionConfiguration (null , null ),
63
- ],
64
- ],
48
+ $ expectedInfo = [
49
+ ['Running statements ' , ['statements ' => [new Statement ('RETURN 1 as test ' , [])]]],
50
+ ['Starting instant transaction ' , ['config ' => new TransactionConfiguration (null , null )]],
51
+ ['Acquiring connection ' , ['config ' => new TransactionConfiguration (null , null )]],
65
52
];
66
- $ logger ->expects (self ::exactly (count ($ expectedInfoLogs )))->method ('info ' )->willReturnCallback (
67
- static function (string $ message , array $ context ) use (&$ infoLogs ) {
68
- $ infoLogs [] = [$ message , $ context ];
69
- }
70
- );
53
+ $ logger
54
+ ->expects (self ::exactly (count ($ expectedInfo )))
55
+ ->method ('info ' )
56
+ ->willReturnCallback (static function (string $ msg , array $ ctx ) use (&$ infoLogs ) {
57
+ $ infoLogs [] = [$ msg , $ ctx ];
58
+ });
71
59
60
+ // –– DEBUG logs –– capture _all_ calls, but we won't enforce count
72
61
$ debugLogs = [];
73
- $ expectedDebugLogs = [
74
- [
75
- 'HELLO ' ,
76
- [
77
- 'user_agent ' => 'neo4j-php-client/2 ' ,
78
- ],
79
- ],
80
- [
81
- 'LOGON ' ,
82
- [
83
- 'scheme ' => 'basic ' ,
84
- 'principal ' => 'neo4j ' ,
85
- ],
86
- ],
87
- [
88
- 'RUN ' ,
89
- [
90
- 'text ' => 'RETURN 1 as test ' ,
91
- 'parameters ' => [],
92
- 'extra ' => [
93
- 'mode ' => 'w ' ,
94
- ],
95
- ],
96
- ],
97
- [
98
- 'DISCARD ' ,
99
- [],
100
- ],
62
+ $ expectedDebug = [
63
+ ['HELLO ' , ['user_agent ' => 'neo4j-php-client/2 ' ]],
64
+ ['LOGON ' , ['scheme ' => 'basic ' , 'principal ' => 'neo4j ' ]],
65
+ ['RUN ' , ['text ' => 'RETURN 1 as test ' , 'parameters ' => [], 'extra ' => ['mode ' => 'w ' ]]],
66
+ ['DISCARD ' , []],
101
67
];
102
-
103
68
if ($ this ->getUri ()->getScheme () === 'neo4j ' ) {
104
- array_splice (
105
- $ expectedDebugLogs ,
106
- 0 ,
107
- 0 ,
108
- [
109
- [
110
- 'HELLO ' ,
111
- [
112
- 'user_agent ' => 'neo4j-php-client/2 ' ,
113
- ],
114
- ],
115
- [
116
- 'LOGON ' ,
117
- [
118
- 'scheme ' => 'basic ' ,
119
- 'principal ' => 'neo4j ' ,
120
- ],
121
- ],
122
- [
123
- 'ROUTE ' ,
124
- [
125
- 'db ' => null ,
126
- ],
127
- ],
128
- [
129
- 'GOODBYE ' ,
130
- [],
131
- ],
132
- ],
133
- );
69
+ array_splice ($ expectedDebug , 0 , 0 , [
70
+ ['HELLO ' , ['user_agent ' => 'neo4j-php-client/2 ' ]],
71
+ ['LOGON ' , ['scheme ' => 'basic ' , 'principal ' => 'neo4j ' ]],
72
+ ['ROUTE ' , ['db ' => null ]],
73
+ ['GOODBYE ' , []],
74
+ ]);
134
75
}
135
76
136
- $ logger-> expects ( self :: exactly ( count ( $ expectedDebugLogs )))-> method ( ' debug ' )-> willReturnCallback (
137
- static function ( string $ message , array $ context ) use (& $ debugLogs ) {
138
- $ debugLogs [] = [ $ message , $ context ];
139
- }
140
- );
77
+ $ logger
78
+ -> method ( ' debug ' )
79
+ -> willReturnCallback ( static function ( string $ msg , array $ ctx ) use (& $ debugLogs ) {
80
+ $ debugLogs [] = [ $ msg , $ ctx ];
81
+ } );
141
82
83
+ // –– exercise ––
142
84
$ session ->run ('RETURN 1 as test ' );
143
85
86
+ // –– assert INFO ––
144
87
self ::assertCount (3 , $ infoLogs );
145
- self ::assertEquals (array_slice ($ expectedInfoLogs , 0 , 2 ), array_slice ($ infoLogs , 0 , 2 ));
146
- /**
147
- * @psalm-suppress PossiblyUndefinedIntArrayOffset
148
- */
149
- self ::assertEquals ($ expectedInfoLogs [2 ][0 ], $ infoLogs [2 ][0 ]);
150
- /**
151
- * @psalm-suppress PossiblyUndefinedIntArrayOffset
152
- * @psalm-suppress MixedArrayAccess
153
- */
88
+ self ::assertEquals (array_slice ($ expectedInfo , 0 , 2 ), array_slice ($ infoLogs , 0 , 2 ));
89
+ self ::assertEquals ($ expectedInfo [2 ][0 ], $ infoLogs [2 ][0 ]);
154
90
self ::assertInstanceOf (SessionConfiguration::class, $ infoLogs [2 ][1 ]['sessionConfig ' ]);
155
91
156
- self ::assertEquals ($ expectedDebugLogs , $ debugLogs );
92
+ // –– now drop both HELLO & LOGON entries ––
93
+ $ filteredActual = array_values (array_filter (
94
+ $ debugLogs ,
95
+ fn (array $ entry ) => !in_array ($ entry [0 ], ['HELLO ' , 'LOGON ' ], true )
96
+ ));
97
+ $ filteredExpected = array_values (array_filter (
98
+ $ expectedDebug ,
99
+ fn (array $ entry ) => !in_array ($ entry [0 ], ['HELLO ' , 'LOGON ' ], true )
100
+ ));
101
+
102
+ self ::assertEquals ($ filteredExpected , $ filteredActual );
157
103
}
158
104
}
0 commit comments