diff --git a/tests/DocumentationExamplesTest.php b/tests/DocumentationExamplesTest.php index 2c8c54996..28d914b90 100644 --- a/tests/DocumentationExamplesTest.php +++ b/tests/DocumentationExamplesTest.php @@ -1605,6 +1605,66 @@ public function testVersionedApi(): void // phpcs:enable } + public function testVersionedApiMigration(): void + { + if (version_compare($this->getServerVersion(), '5.0.0', '<')) { + $this->markTestSkipped('Versioned API is not supported'); + } + + $uriString = static::getUri(true); + + // phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly + $serverApi = new \MongoDB\Driver\ServerApi('1', true); + $client = new \MongoDB\Client($uriString, [], ['serverApi' => $serverApi]); + $db = $client->selectDatabase($this->getDatabaseName()); + $db->dropCollection('sales'); + + // Start Versioned API Example 5 + $strtoutc = function (string $datetime) { + return new \MongoDB\BSON\UTCDateTime(new \DateTime($datetime)); + }; + + $db->sales->insertMany([ + ['_id' => 1, 'item' => 'abc', 'price' => 10, 'quantity' => 2, 'date' => $strtoutc('2021-01-01T08:00:00Z')], + ['_id' => 2, 'item' => 'jkl', 'price' => 20, 'quantity' => 1, 'date' => $strtoutc('2021-02-03T09:00:00Z')], + ['_id' => 3, 'item' => 'xyz', 'price' => 5, 'quantity' => 5, 'date' => $strtoutc('2021-02-03T09:05:00Z')], + ['_id' => 4, 'item' => 'abc', 'price' => 10, 'quantity' => 10, 'date' => $strtoutc('2021-02-15T08:00:00Z')], + ['_id' => 5, 'item' => 'xyz', 'price' => 5, 'quantity' => 10, 'date' => $strtoutc('2021-02-15T09:05:00Z')], + ['_id' => 6, 'item' => 'xyz', 'price' => 5, 'quantity' => 5, 'date' => $strtoutc('2021-02-15T12:05:10Z')], + ['_id' => 7, 'item' => 'xyz', 'price' => 5, 'quantity' => 10, 'date' => $strtoutc('2021-02-15T14:12:12Z')], + ['_id' => 8, 'item' => 'abc', 'price' => 10, 'quantity' => 5, 'date' => $strtoutc('2021-03-16T20:20:13Z')], + ]); + // End Versioned API Example 5 + + ob_start(); + + // Start Versioned API Example 6 + try { + $count = $db->sales->count(); + } catch (\MongoDB\Driver\Exception\CommandException $e) { + echo json_encode($e->getResultDocument()); + // { "ok": 0, "errmsg": "Provided apiStrict:true, but the command count is not in API Version 1", "code": 323, "codeName": "APIStrictError" } + } + + // End Versioned API Example 6 + + ob_end_clean(); + + $this->assertStringContainsString('Provided apiStrict:true, but the command count is not in API Version 1', $e->getMessage()); + $this->assertEquals(323 /* APIStrictError */, $e->getCode()); + + // Start Versioned API Example 7 + $count = $db->sales->countDocuments(); + // End Versioned API Example 7 + + $this->assertSame($count, $db->sales->countDocuments()); + + // Start Versioned API Example 8 + // 8 + // End Versioned API Example 8 + // phpcs:enable + } + /** * @doesNotPerformAssertions */