@@ -1605,6 +1605,66 @@ public function testVersionedApi(): void
1605
1605
// phpcs:enable
1606
1606
}
1607
1607
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
+
1608
1668
/**
1609
1669
* @doesNotPerformAssertions
1610
1670
*/
0 commit comments