Skip to content

Commit

Permalink
Add fixtures to run atlas-search example in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 13, 2023
1 parent a13741e commit ef5f19d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
8 changes: 6 additions & 2 deletions examples/atlas-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 33 additions & 15 deletions tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,7 +186,7 @@ public static function provideExamples(): Generator
}

/**
* MongoDB Atlas Search example requires a MongoDB Atlas M10+ cluster with MongoDB 7.0+ and sample data loaded.
* MongoDB Atlas Search example requires a MongoDB Atlas M10+ cluster with MongoDB 7.0+
* Tips for insiders: if using a cloud-dev server, append ".mongodb.net" to the MONGODB_URI.
*
* @group atlas
Expand All @@ -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
Expand Down

0 comments on commit ef5f19d

Please sign in to comment.