Skip to content

Commit 79ed108

Browse files
committed
make flexible logging test for compatibility between neo4j 4 and 5
1 parent 786b8c1 commit 79ed108

File tree

1 file changed

+47
-97
lines changed

1 file changed

+47
-97
lines changed

tests/Integration/Neo4jLoggerTest.php

Lines changed: 47 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
class Neo4jLoggerTest extends EnvironmentAwareIntegrationTest
2424
{
25+
/**
26+
* @psalm-suppress PossiblyUndefinedIntArrayOffset
27+
* @psalm-suppress PossiblyUndefinedStringArrayOffset
28+
*/
2529
public function testLogger(): void
2630
{
2731
if (str_contains($this->getUri()->getScheme(), 'http')) {
@@ -32,127 +36,73 @@ public function testLogger(): void
3236
self::markTestSkipped('This test is not applicable clusters');
3337
}
3438

35-
// Close connections so that we can test the logger logging
36-
// during authentication while acquiring a new connection
3739
$this->driver->closeConnections();
3840

3941
/** @var MockObject $logger */
4042
$logger = $this->getNeo4jLogger()->getLogger();
4143
/** @var Session $session */
4244
$session = $this->getSession();
4345

44-
/** @var array<int, array> $infoLogs */
46+
// –– INFO logs (unchanged) ––
4547
$infoLogs = [];
4648
$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-
],
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)]],
6552
];
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($expectedInfoLogs)))
55+
->method('info')
56+
->willReturnCallback(static function (string $msg, array $ctx) use (&$infoLogs) {
57+
$infoLogs[] = [$msg, $ctx];
58+
});
7159

60+
// –– DEBUG logs –– we drop expects() and just capture every call
7261
$debugLogs = [];
7362
$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-
],
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', []],
10167
];
10268

69+
// if bolt scheme, prepend extra ops
10370
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-
);
71+
array_splice($expectedDebugLogs, 0, 0, [
72+
['HELLO', ['user_agent' => 'neo4j-php-client/2']],
73+
['LOGON', ['scheme' => 'basic', 'principal' => 'neo4j']],
74+
['ROUTE', ['db' => null]],
75+
['GOODBYE', []],
76+
]);
13477
}
13578

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-
);
79+
// capture everything
80+
$logger
81+
->method('debug')
82+
->willReturnCallback(static function (string $msg, array $ctx) use (&$debugLogs) {
83+
$debugLogs[] = [$msg, $ctx];
84+
});
14185

86+
// –– exercise
14287
$session->run('RETURN 1 as test');
14388

89+
// –– assert INFO ––
14490
self::assertCount(3, $infoLogs);
14591
self::assertEquals(array_slice($expectedInfoLogs, 0, 2), array_slice($infoLogs, 0, 2));
146-
/**
147-
* @psalm-suppress PossiblyUndefinedIntArrayOffset
148-
*/
14992
self::assertEquals($expectedInfoLogs[2][0], $infoLogs[2][0]);
150-
/**
151-
* @psalm-suppress PossiblyUndefinedIntArrayOffset
152-
* @psalm-suppress MixedArrayAccess
153-
*/
154-
self::assertInstanceOf(SessionConfiguration::class, $infoLogs[2][1]['sessionConfig']);
93+
self::assertInstanceOf(
94+
SessionConfiguration::class,
95+
$infoLogs[2][1]['sessionConfig']
96+
);
97+
98+
// –– now filter out any LOGON entries and compare the rest ––
99+
$filteredDebug = array_values(
100+
array_filter($debugLogs, fn (array $entry) => $entry[0] !== 'LOGON')
101+
);
102+
$expectedWithoutLogon = array_values(
103+
array_filter($expectedDebugLogs, fn (array $entry) => $entry[0] !== 'LOGON')
104+
);
155105

156-
self::assertEquals($expectedDebugLogs, $debugLogs);
106+
self::assertEquals($expectedWithoutLogon, $filteredDebug);
157107
}
158108
}

0 commit comments

Comments
 (0)