Skip to content

Commit c8e816b

Browse files
committed
Merge pull request #518
2 parents 7417ecf + 83dcc99 commit c8e816b

File tree

1 file changed

+160
-2
lines changed

1 file changed

+160
-2
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,6 @@ public function testExample_51_54()
851851
$this->assertCursorCount(1, $cursor);
852852
}
853853

854-
855-
856854
public function testExample_55_58()
857855
{
858856
$db = new Database($this->manager, $this->getDatabaseName());
@@ -1022,6 +1020,166 @@ public function testChangeStreamExample_1_4()
10221020
$this->assertNull($nextChange);
10231021
}
10241022

1023+
public function testAggregation_example_1()
1024+
{
1025+
$db = new Database($this->manager, $this->getDatabaseName());
1026+
1027+
// Start Aggregation Example 1
1028+
$cursor = $db->sales->aggregate([
1029+
['$match' => ['items.fruit' => 'banana']],
1030+
['$sort' => ['date' => 1]],
1031+
]);
1032+
// End Aggregation Example 1
1033+
1034+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
1035+
}
1036+
1037+
public function testAggregation_example_2()
1038+
{
1039+
$db = new Database($this->manager, $this->getDatabaseName());
1040+
1041+
// Start Aggregation Example 2
1042+
$cursor = $db->sales->aggregate([
1043+
['$unwind' => '$items'],
1044+
['$match' => ['items.fruit' => 'banana']],
1045+
[
1046+
'$group' => ['_id' => ['day' => ['$dayOfWeek' => '$date']],
1047+
'count' => ['$sum' => '$items.quantity']],
1048+
],
1049+
[
1050+
'$project' => [
1051+
'dayOfWeek' => '$_id.day',
1052+
'numberSold' => '$count',
1053+
'_id' => 0,
1054+
]
1055+
],
1056+
['$sort' => ['numberSold' => 1]],
1057+
]);
1058+
// End Aggregation Example 2
1059+
1060+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
1061+
}
1062+
1063+
public function testAggregation_example_3()
1064+
{
1065+
$db = new Database($this->manager, $this->getDatabaseName());
1066+
1067+
// Start Aggregation Example 3
1068+
$cursor = $db->sales->aggregate([
1069+
['$unwind' => '$items'],
1070+
['$group' => [
1071+
'_id' => ['day' => ['$dayOfWeek' => '$date']],
1072+
'items_sold' => ['$sum' => '$items.quantity'],
1073+
'revenue' => [
1074+
'$sum' => [
1075+
'$multiply' => ['$items.quantity', '$items.price']
1076+
]
1077+
],
1078+
]],
1079+
['$project' => [
1080+
'day' => '$_id.day',
1081+
'revenue' => 1,
1082+
'items_sold' => 1,
1083+
'discount' => [
1084+
'$cond' => [
1085+
'if' => ['$lte' => ['$revenue', 250]],
1086+
'then' => 25,
1087+
'else' => 0,
1088+
]
1089+
],
1090+
]],
1091+
]);
1092+
// End Aggregation Example 3
1093+
1094+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
1095+
}
1096+
1097+
public function testAggregation_example_4()
1098+
{
1099+
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
1100+
$this->markTestSkipped('$lookup does not support "let" option');
1101+
}
1102+
1103+
$db = new Database($this->manager, $this->getDatabaseName());
1104+
1105+
// Start Aggregation Example 4
1106+
$cursor = $db->air_alliances->aggregate([
1107+
['$lookup' => [
1108+
'from' => 'air_airlines',
1109+
'let' => ['constituents' => '$airlines'],
1110+
'pipeline' => [['$match' => [
1111+
'$expr' => ['$in' => ['$name', '$constituents']]
1112+
]]],
1113+
'as' => 'airlines',
1114+
]],
1115+
['$project' => [
1116+
'_id' => 0,
1117+
'name' => 1,
1118+
'airlines' => [
1119+
'$filter' => [
1120+
'input' => '$airlines',
1121+
'as' => 'airline',
1122+
'cond' => ['$eq' => ['$$airline.country', 'Canada']],
1123+
]
1124+
],
1125+
]],
1126+
]);
1127+
// End Aggregation Example 4
1128+
1129+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
1130+
}
1131+
1132+
public function testRunCommand_example_1()
1133+
{
1134+
$db = new Database($this->manager, $this->getDatabaseName());
1135+
1136+
// Start runCommand Example 1
1137+
$cursor = $db->command(['buildInfo' => 1]);
1138+
$result = $cursor->toArray()[0];
1139+
// End runCommand Example 1
1140+
1141+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
1142+
}
1143+
1144+
public function testRunCommand_example_2()
1145+
{
1146+
$db = new Database($this->manager, $this->getDatabaseName());
1147+
$db->dropCollection('restaurants');
1148+
$db->createCollection('restaurants');
1149+
1150+
// Start runCommand Example 2
1151+
$cursor = $db->command(['collStats' => 'restaurants']);
1152+
$result = $cursor->toArray()[0];
1153+
// End runCommand Example 2
1154+
1155+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
1156+
}
1157+
1158+
public function testIndex_example_1()
1159+
{
1160+
$db = new Database($this->manager, $this->getDatabaseName());
1161+
1162+
// Start Index Example 1
1163+
$indexName = $db->records->createIndex(['score' => 1]);
1164+
// End Index Example 1
1165+
1166+
$this->assertEquals('score_1', $indexName);
1167+
}
1168+
1169+
public function testIndex_example_2()
1170+
{
1171+
$db = new Database($this->manager, $this->getDatabaseName());
1172+
1173+
// Start Index Example 2
1174+
$indexName = $db->restaurants->createIndex(
1175+
['cuisine' => 1, 'name' => 1],
1176+
['partialFilterExpression' => ['rating' => ['$gt' => 5]]]
1177+
);
1178+
// End Index Example 2
1179+
1180+
$this->assertEquals('cuisine_1_name_1', $indexName);
1181+
}
1182+
10251183
/**
10261184
* Return the test collection name.
10271185
*

0 commit comments

Comments
 (0)