Skip to content

Commit 50d1db7

Browse files
authored
Fix CursorId deprecation in tests (#1423)
1 parent b20703f commit 50d1db7

File tree

5 files changed

+19
-28
lines changed

5 files changed

+19
-28
lines changed

tests/Operation/WatchFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ public function testInitialCursorIsNotClosed(): void
720720
* reports the cursor as alive. While the cursor ID is accessed through
721721
* ChangeStream, we'll need to use reflection to access the internal
722722
* Cursor and call isDead(). */
723-
$this->assertNotEquals('0', (string) $changeStream->getCursorId());
723+
$this->assertNotEquals('0', (string) $changeStream->getCursorId(true));
724724

725725
$rc = new ReflectionClass(ChangeStream::class);
726726
$rp = $rc->getProperty('iterator');
@@ -1370,11 +1370,11 @@ public function testOriginalReadPreferenceIsPreservedOnResume(): void
13701370
}
13711371

13721372
$changeStream = $operation->execute($secondary);
1373-
$previousCursorId = $changeStream->getCursorId();
1373+
$previousCursorId = $changeStream->getCursorId(true);
13741374
$this->forceChangeStreamResume();
13751375

13761376
$changeStream->next();
1377-
$this->assertNotSame($previousCursorId, $changeStream->getCursorId());
1377+
$this->assertNotSame($previousCursorId, $changeStream->getCursorId(true));
13781378

13791379
$getCursor = Closure::bind(
13801380
fn () => $this->iterator->getInnerIterator(),

tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\Monitoring\CommandStartedEvent;
88
use MongoDB\Driver\Monitoring\CommandSubscriber;
99
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
10-
use MongoDB\Driver\Server;
1110
use MongoDB\Tests\SpecTests\FunctionalTestCase;
1211

1312
use function assert;
@@ -65,7 +64,8 @@ public function testRetryOnDifferentMongos(): void
6564

6665
// Step 4: Enable failed command event monitoring for client
6766
$subscriber = new class implements CommandSubscriber {
68-
public $commandFailedServers = [];
67+
/** @var int[] */
68+
public array $commandFailedServers = [];
6969

7070
public function commandStarted(CommandStartedEvent $event): void
7171
{
@@ -77,7 +77,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void
7777

7878
public function commandFailed(CommandFailedEvent $event): void
7979
{
80-
$this->commandFailedServers[] = $event->getServer();
80+
$this->commandFailedServers[] = $event->getServerConnectionId();
8181
}
8282
};
8383

@@ -130,21 +130,21 @@ public function testRetryOnSameMongos(): void
130130

131131
// Step 4: Enable succeeded and failed command event monitoring
132132
$subscriber = new class implements CommandSubscriber {
133-
public Server $commandSucceededServer;
134-
public Server $commandFailedServer;
133+
public ?int $commandSucceededServer = null;
134+
public ?int $commandFailedServer = null;
135135

136136
public function commandStarted(CommandStartedEvent $event): void
137137
{
138138
}
139139

140140
public function commandSucceeded(CommandSucceededEvent $event): void
141141
{
142-
$this->commandSucceededServer = $event->getServer();
142+
$this->commandSucceededServer = $event->getServerConnectionId();
143143
}
144144

145145
public function commandFailed(CommandFailedEvent $event): void
146146
{
147-
$this->commandFailedServer = $event->getServer();
147+
$this->commandFailedServer = $event->getServerConnectionId();
148148
}
149149
};
150150

tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function testRetryOnDifferentMongos(): void
6767

6868
// Step 4: Enable failed command event monitoring for client
6969
$subscriber = new class implements CommandSubscriber {
70-
public $commandFailedServers = [];
70+
/** @var int[] */
71+
public array $commandFailedServers = [];
7172

7273
public function commandStarted(CommandStartedEvent $event): void
7374
{
@@ -79,7 +80,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void
7980

8081
public function commandFailed(CommandFailedEvent $event): void
8182
{
82-
$this->commandFailedServers[] = $event->getServer();
83+
$this->commandFailedServers[] = $event->getServerConnectionId();
8384
}
8485
};
8586

tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use MongoDB\Driver\Monitoring\CommandStartedEvent;
77
use MongoDB\Driver\Monitoring\CommandSubscriber;
88
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
9-
use MongoDB\Driver\Server;
109
use MongoDB\Tests\SpecTests\FunctionalTestCase;
1110

1211
/**
@@ -50,21 +49,21 @@ public function testRetryOnSameMongos(): void
5049

5150
// Step 4: Enable succeeded and failed command event monitoring
5251
$subscriber = new class implements CommandSubscriber {
53-
public Server $commandSucceededServer;
54-
public Server $commandFailedServer;
52+
public ?int $commandSucceededServer = null;
53+
public ?int $commandFailedServer = null;
5554

5655
public function commandStarted(CommandStartedEvent $event): void
5756
{
5857
}
5958

6059
public function commandSucceeded(CommandSucceededEvent $event): void
6160
{
62-
$this->commandSucceededServer = $event->getServer();
61+
$this->commandSucceededServer = $event->getServerConnectionId();
6362
}
6463

6564
public function commandFailed(CommandFailedEvent $event): void
6665
{
67-
$this->commandFailedServer = $event->getServer();
66+
$this->commandFailedServer = $event->getServerConnectionId();
6867
}
6968
};
7069

@@ -78,7 +77,7 @@ public function commandFailed(CommandFailedEvent $event): void
7877

7978
/* Step 6: Assert that exactly one failed command event and one
8079
* succeeded command event occurred. Assert that both events occurred on
81-
* the same mongos. */
80+
* the same mongos connection. */
8281
$this->assertNotNull($subscriber->commandSucceededServer);
8382
$this->assertNotNull($subscriber->commandFailedServer);
8483
$this->assertEquals($subscriber->commandSucceededServer, $subscriber->commandFailedServer);

tests/UnifiedSpecTests/EventCollector.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use function PHPUnit\Framework\assertIsObject;
1919
use function PHPUnit\Framework\assertIsString;
2020
use function PHPUnit\Framework\assertNotEmpty;
21-
use function sprintf;
2221

2322
/**
2423
* EventCollector handles "storeEventsAsEntities" for client entities.
@@ -120,7 +119,7 @@ private function handleCommandMonitoringEvent($event): void
120119
'name' => self::getEventName($event),
121120
'observedAt' => microtime(true),
122121
'commandName' => $event->getCommandName(),
123-
'connectionId' => self::getConnectionId($event),
122+
'connectionId' => $event->getServerConnectionId(),
124123
'requestId' => $event->getRequestId(),
125124
'operationId' => $event->getOperationId(),
126125
];
@@ -144,14 +143,6 @@ private function handleCommandMonitoringEvent($event): void
144143
$this->eventList[] = $log;
145144
}
146145

147-
/** @param CommandStartedEvent|CommandSucceededEvent|CommandFailedEvent $event */
148-
private static function getConnectionId($event): string
149-
{
150-
$server = $event->getServer();
151-
152-
return sprintf('%s:%d', $server->getHost(), $server->getPort());
153-
}
154-
155146
private static function getEventName(object $event): string
156147
{
157148
static $eventNamesByClass = null;

0 commit comments

Comments
 (0)