Skip to content

Commit 5a5b75c

Browse files
committed
Implement comparator for Server objects
1 parent 6d3c41d commit 5a5b75c

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

tests/Comparator/ServerComparator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Comparator;
4+
5+
use MongoDB\Driver\Server;
6+
use SebastianBergmann\Comparator\Comparator;
7+
use SebastianBergmann\Comparator\ComparisonFailure;
8+
9+
use function sprintf;
10+
11+
class ServerComparator extends Comparator
12+
{
13+
public function accepts($expected, $actual)
14+
{
15+
return $expected instanceof Server && $actual instanceof Server;
16+
}
17+
18+
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false): void
19+
{
20+
if ($expected == $actual) {
21+
return;
22+
}
23+
24+
throw new ComparisonFailure(
25+
$expected,
26+
$actual,
27+
'',
28+
'',
29+
false,
30+
sprintf(
31+
'Failed asserting that Server("%s:%d") matches expected Server("%s:%d").',
32+
$actual->getHost(),
33+
$actual->getPort(),
34+
$expected->getHost(),
35+
$expected->getPort(),
36+
),
37+
);
38+
}
39+
}

tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testRetryOnDifferentMongos(): void
4343
*/
4444
$servers = $client->getManager()->getServers();
4545
assert(count($servers) === 2);
46+
$this->assertNotEquals($servers[0], $servers[1]);
4647

4748
// Step 2: Configure the following fail point on each mongos
4849
foreach ($servers as $server) {
@@ -55,7 +56,7 @@ public function testRetryOnDifferentMongos(): void
5556
'errorCode' => self::HOST_UNREACHABLE,
5657
],
5758
],
58-
$server
59+
$server,
5960
);
6061
}
6162

tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testRetryOnDifferentMongos(): void
4444
*/
4545
$servers = $client->getManager()->getServers();
4646
assert(count($servers) === 2);
47+
$this->assertNotEquals($servers[0], $servers[1]);
4748

4849
// Step 2: Configure the following fail point on each mongos
4950
foreach ($servers as $server) {

tests/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

33
use MongoDB\Tests\Comparator\Int64Comparator;
4+
use MongoDB\Tests\Comparator\ServerComparator;
45
use MongoDB\Tests\TestCase;
56
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
67

78
require __DIR__ . '/../vendor/autoload.php';
89

910
// Register custom comparators
1011
ComparatorFactory::getInstance()->register(new Int64Comparator());
12+
ComparatorFactory::getInstance()->register(new ServerComparator());
1113

1214
/* Ugly workaround for event system changes in PHPUnit
1315
* PHPUnit 10 introduces a new event system, and at the same time removes the

0 commit comments

Comments
 (0)