Skip to content

Commit 93ccf29

Browse files
authored
PHPLIB-699: Versioned API strict migration doc examples (#886)
1 parent c631acd commit 93ccf29

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,66 @@ public function testVersionedApi(): void
16051605
// phpcs:enable
16061606
}
16071607

1608+
public function testVersionedApiMigration(): void
1609+
{
1610+
if (version_compare($this->getServerVersion(), '5.0.0', '<')) {
1611+
$this->markTestSkipped('Versioned API is not supported');
1612+
}
1613+
1614+
$uriString = static::getUri(true);
1615+
1616+
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
1617+
$serverApi = new \MongoDB\Driver\ServerApi('1', true);
1618+
$client = new \MongoDB\Client($uriString, [], ['serverApi' => $serverApi]);
1619+
$db = $client->selectDatabase($this->getDatabaseName());
1620+
$db->dropCollection('sales');
1621+
1622+
// Start Versioned API Example 5
1623+
$strtoutc = function (string $datetime) {
1624+
return new \MongoDB\BSON\UTCDateTime(new \DateTime($datetime));
1625+
};
1626+
1627+
$db->sales->insertMany([
1628+
['_id' => 1, 'item' => 'abc', 'price' => 10, 'quantity' => 2, 'date' => $strtoutc('2021-01-01T08:00:00Z')],
1629+
['_id' => 2, 'item' => 'jkl', 'price' => 20, 'quantity' => 1, 'date' => $strtoutc('2021-02-03T09:00:00Z')],
1630+
['_id' => 3, 'item' => 'xyz', 'price' => 5, 'quantity' => 5, 'date' => $strtoutc('2021-02-03T09:05:00Z')],
1631+
['_id' => 4, 'item' => 'abc', 'price' => 10, 'quantity' => 10, 'date' => $strtoutc('2021-02-15T08:00:00Z')],
1632+
['_id' => 5, 'item' => 'xyz', 'price' => 5, 'quantity' => 10, 'date' => $strtoutc('2021-02-15T09:05:00Z')],
1633+
['_id' => 6, 'item' => 'xyz', 'price' => 5, 'quantity' => 5, 'date' => $strtoutc('2021-02-15T12:05:10Z')],
1634+
['_id' => 7, 'item' => 'xyz', 'price' => 5, 'quantity' => 10, 'date' => $strtoutc('2021-02-15T14:12:12Z')],
1635+
['_id' => 8, 'item' => 'abc', 'price' => 10, 'quantity' => 5, 'date' => $strtoutc('2021-03-16T20:20:13Z')],
1636+
]);
1637+
// End Versioned API Example 5
1638+
1639+
ob_start();
1640+
1641+
// Start Versioned API Example 6
1642+
try {
1643+
$count = $db->sales->count();
1644+
} catch (\MongoDB\Driver\Exception\CommandException $e) {
1645+
echo json_encode($e->getResultDocument());
1646+
// { "ok": 0, "errmsg": "Provided apiStrict:true, but the command count is not in API Version 1", "code": 323, "codeName": "APIStrictError" }
1647+
}
1648+
1649+
// End Versioned API Example 6
1650+
1651+
ob_end_clean();
1652+
1653+
$this->assertStringContainsString('Provided apiStrict:true, but the command count is not in API Version 1', $e->getMessage());
1654+
$this->assertEquals(323 /* APIStrictError */, $e->getCode());
1655+
1656+
// Start Versioned API Example 7
1657+
$count = $db->sales->countDocuments();
1658+
// End Versioned API Example 7
1659+
1660+
$this->assertSame($count, $db->sales->countDocuments());
1661+
1662+
// Start Versioned API Example 8
1663+
// 8
1664+
// End Versioned API Example 8
1665+
// phpcs:enable
1666+
}
1667+
16081668
/**
16091669
* @doesNotPerformAssertions
16101670
*/

0 commit comments

Comments
 (0)