|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Collection; |
| 4 | + |
| 5 | +use MongoDB\Collection; |
| 6 | + |
| 7 | +/** |
| 8 | + * Functional tests from the CRUD specification. |
| 9 | + * |
| 10 | + * @see https://github.com/mongodb/specifications/tree/master/source/crud/tests |
| 11 | + */ |
| 12 | +class CrudSpecFunctionalTest extends FunctionalTestCase |
| 13 | +{ |
| 14 | + public function testAggregateWithMultipleStages() |
| 15 | + { |
| 16 | + $this->assertEquals(3, $this->collection->insertMany(array( |
| 17 | + array('_id' => 1, 'x' => 11), |
| 18 | + array('_id' => 2, 'x' => 22), |
| 19 | + array('_id' => 3, 'x' => 33), |
| 20 | + ))->getInsertedCount()); |
| 21 | + |
| 22 | + $cursor = $this->collection->aggregate( |
| 23 | + array( |
| 24 | + array('$sort' => array('x' => 1)), |
| 25 | + array('$match' => array('_id' => array('$gt' => 1))), |
| 26 | + ), |
| 27 | + array('batchSize' => 2) |
| 28 | + ); |
| 29 | + |
| 30 | + $expected = array( |
| 31 | + array('_id' => 2, 'x' => 22), |
| 32 | + array('_id' => 3, 'x' => 33), |
| 33 | + ); |
| 34 | + |
| 35 | + $this->assertEquals($expected, $cursor->toArray()); |
| 36 | + } |
| 37 | + |
| 38 | + public function testAggregateWithOut() |
| 39 | + { |
| 40 | + $outputCollection = new Collection($this->manager, $this->getNamespace() . '_output'); |
| 41 | + $this->dropCollectionIfItExists($outputCollection); |
| 42 | + |
| 43 | + $this->assertEquals(3, $this->collection->insertMany(array( |
| 44 | + array('_id' => 1, 'x' => 11), |
| 45 | + array('_id' => 2, 'x' => 22), |
| 46 | + array('_id' => 3, 'x' => 33), |
| 47 | + ))->getInsertedCount()); |
| 48 | + |
| 49 | + $this->collection->aggregate( |
| 50 | + array( |
| 51 | + array('$sort' => array('x' => 1)), |
| 52 | + array('$match' => array('_id' => array('$gt' => 1))), |
| 53 | + array('$out' => $outputCollection->getCollectionName()), |
| 54 | + ) |
| 55 | + ); |
| 56 | + |
| 57 | + $expected = array( |
| 58 | + array('_id' => 2, 'x' => 22), |
| 59 | + array('_id' => 3, 'x' => 33), |
| 60 | + ); |
| 61 | + |
| 62 | + $this->assertEquals($expected, $outputCollection->find()->toArray()); |
| 63 | + } |
| 64 | +} |
0 commit comments