Skip to content

Commit 1dc201a

Browse files
committed
PHPLIB-1399: Docs examples for agg expr projection
1 parent cb07dab commit 1dc201a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @see https://jira.mongodb.org/browse/DRIVERS-356
2828
* @see https://jira.mongodb.org/browse/DRIVERS-488
2929
* @see https://jira.mongodb.org/browse/DRIVERS-547
30+
* @see https://jira.mongodb.org/browse/DRIVERS-2838
3031
*/
3132
class DocumentationExamplesTest extends FunctionalTestCase
3233
{
@@ -728,6 +729,45 @@ public function testExample_42_50(): void
728729
$this->assertObjectNotHasAttribute('size', $document);
729730
$this->assertCount(1, $document->instock);
730731
}
732+
733+
// Start Aggregation Projection Example 1
734+
$cursor = $db->inventory->find([], [
735+
'projection' => [
736+
'_id' => 0,
737+
'item' => 1,
738+
'status' => [
739+
'$switch' => [
740+
'branches' => [
741+
['case' => ['$eq' => ['$status', 'A']], 'then' => 'Available'],
742+
['case' => ['$eq' => ['$status', 'D']], 'then' => 'Discontinued'],
743+
],
744+
'default' => 'No status found',
745+
],
746+
],
747+
'area' => [
748+
'$concat' => [
749+
['$toString' => ['$multiply' => ['$size.h', '$size.w']]],
750+
' ',
751+
'$size.uom',
752+
],
753+
],
754+
'reportNumber' => ['$literal' => 1],
755+
],
756+
]);
757+
// End Aggregation Projection Example 1
758+
759+
$documents = $cursor->toArray();
760+
$this->assertCount(5, $documents);
761+
foreach ($documents as $document) {
762+
foreach (['item', 'status', 'area', 'reportNumber'] as $field) {
763+
$this->assertObjectHasAttribute($field, $document);
764+
}
765+
766+
$this->assertObjectNotHasAttribute('_id', $document);
767+
$this->assertIsString($document->status);
768+
$this->assertIsString($document->area);
769+
$this->assertSame(1, $document->reportNumber);
770+
}
731771
}
732772

733773
public function testExample_51_54(): void

0 commit comments

Comments
 (0)