Skip to content

PHPLIB-1399: Docs examples for agg expr projection #1260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @see https://jira.mongodb.org/browse/DRIVERS-356
* @see https://jira.mongodb.org/browse/DRIVERS-488
* @see https://jira.mongodb.org/browse/DRIVERS-547
* @see https://jira.mongodb.org/browse/DRIVERS-2838
*/
class DocumentationExamplesTest extends FunctionalTestCase
{
Expand Down Expand Up @@ -730,6 +731,71 @@ public function testExample_42_50(): void
}
}

public function testAggregationProjectionExample_1(): void
{
$this->skipIfServerVersion('<', '4.4.0', '$project syntax for find and findAndModify is not supported');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized that the projection syntax requires 4.4+, so I've broken this example out into a separate method and pulled in some data fixtures from the related test method.


$this->dropCollection($this->getDatabaseName(), 'inventory');
$db = new Database($this->manager, $this->getDatabaseName());

$db->inventory->insertMany([
[
'item' => 'journal',
'status' => 'A',
'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
],
[
'item' => 'notebook',
'status' => 'A',
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
],
[
'item' => 'paper',
'status' => 'D',
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
],
]);

// Start Aggregation Projection Example 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples are rendered within Project Fields with Aggregation Expressions, which follows the earlier examples in this method and shares its data fixtures.

$cursor = $db->inventory->find([], [
'projection' => [
'_id' => 0,
'item' => 1,
'status' => [
'$switch' => [
'branches' => [
['case' => ['$eq' => ['$status', 'A']], 'then' => 'Available'],
['case' => ['$eq' => ['$status', 'D']], 'then' => 'Discontinued'],
],
'default' => 'No status found',
],
],
'area' => [
'$concat' => [
['$toString' => ['$multiply' => ['$size.h', '$size.w']]],
' ',
'$size.uom',
],
],
'reportNumber' => ['$literal' => 1],
],
]);
// End Aggregation Projection Example 1

$documents = $cursor->toArray();
$this->assertCount(3, $documents);
foreach ($documents as $document) {
foreach (['item', 'status', 'area', 'reportNumber'] as $field) {
$this->assertObjectHasAttribute($field, $document);
}

$this->assertObjectNotHasAttribute('_id', $document);
$this->assertIsString($document->status);
$this->assertIsString($document->area);
$this->assertSame(1, $document->reportNumber);
}
}

public function testExample_51_54(): void
{
$this->dropCollection($this->getDatabaseName(), 'inventory');
Expand Down