From fb903060c2901060cd319cd76ce4a6a3445302e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 6 Sep 2023 22:03:58 +0200 Subject: [PATCH] Add fixtures to run atlas-search example in CI --- examples/atlas-search.php | 8 +++++-- tests/ExamplesTest.php | 46 +++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/examples/atlas-search.php b/examples/atlas-search.php index 3bcfc7cbc..938751256 100644 --- a/examples/atlas-search.php +++ b/examples/atlas-search.php @@ -35,12 +35,16 @@ // They usually take less than 5 minutes to complete. define('WAIT_TIMEOUT_SEC', 300); +// The sample dataset is loaded into the "sample_airbnb.listingsAndReviews" collection. +$databaseName = getenv('MONGODB_DATABASE') ?: 'sample_airbnb'; +$collectionName = getenv('MONGODB_COLLECTION') ?: 'listingsAndReviews'; + $client = new Client($uri); -$collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews'); +$collection = $client->selectCollection($databaseName, $collectionName); $count = $collection->estimatedDocumentCount(); if ($count === 0) { - echo 'This example requires the sample_airbnb database with the listingsAndReviews collection.', "\n"; + echo 'This example requires the "', $databaseName, '" database with the "', $collectionName, '" collection.', "\n"; echo 'Load the sample dataset in your MongoDB Atlas cluster before running this example:', "\n"; echo ' https://www.mongodb.com/docs/atlas/sample-data/', "\n"; exit(1); diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index 7ceaeb5c2..b1623577b 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -3,9 +3,12 @@ namespace MongoDB\Tests; use Generator; -use MongoDB\Client; +use function bin2hex; use function getenv; +use function putenv; +use function random_bytes; +use function sprintf; /** @runTestsInSeparateProcesses */ final class ExamplesTest extends FunctionalTestCase @@ -197,22 +200,37 @@ public function testAtlasSearch(): void $this->skipIfServerVersion('<', '7.0', 'Atlas Search examples require MongoDB 7.0 or later'); - $client = new Client($uri); - $collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews'); - $count = $collection->estimatedDocumentCount(); - if ($count === 0) { - $this->markTestSkipped('Atlas Search examples require the sample_airbnb database with the listingsAndReviews collection'); - } + // Generate random collection name to avoid conflicts with consecutive runs as the index creation is asynchronous + $collectionName = sprintf('%s.%s', $this->getCollectionName(), bin2hex(random_bytes(5))); + $databaseName = $this->getDatabaseName(); + $collection = $this->createCollection($databaseName, $collectionName); + $collection->insertMany([ + ['name' => 'Ribeira Charming Duplex'], + ['name' => 'Ocean View Bondi Beach'], + ['name' => 'Luxury ocean view Beach Villa 622'], + ['name' => 'Ocean & Beach View Condo WBR H204'], + ['name' => 'Bondi Beach Spacious Studio With Ocean View'], + ['name' => 'New York City - Upper West Side Apt'], + ]); + putenv(sprintf('MONGODB_DATABASE=%s', $databaseName)); + putenv(sprintf('MONGODB_COLLECTION=%s', $collectionName)); + + $expectedOutput = <<<'OUTPUT' - // Clean variables to avoid conflict with example - unset($uri, $client, $collection, $count); +Creating the index. +%s +Performing a text search... + - Ocean View Bondi Beach + - Luxury ocean view Beach Villa 622 + - Ocean & Beach View Condo WBR H204 + - Bondi Beach Spacious Studio With Ocean View - require __DIR__ . '/../examples/atlas-search.php'; +Enjoy MongoDB Atlas Search! + + +OUTPUT; - $output = $this->getActualOutputForAssertion(); - $this->assertStringContainsString("\nCreating the index.\n...", $output); - $this->assertStringContainsString("\nPerforming a text search...\n - ", $output); - $this->assertStringContainsString("\nEnjoy MongoDB Atlas Search!\n", $output); + $this->assertExampleOutput(__DIR__ . '/../examples/atlas-search.php', $expectedOutput); } public function testChangeStream(): void