AggregateFunctionalTest.php 2 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests\Collection\CrudSpec;

use MongoDB\Collection;
6
use MongoDB\Driver\ReadPreference;
7 8 9 10 11 12 13 14

/**
 * CRUD spec functional tests for aggregate().
 *
 * @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
 */
class AggregateFunctionalTest extends FunctionalTestCase
{
15 16
    private static $wireVersionForOutOperator = 2;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    public function setUp()
    {
        parent::setUp();

        $this->createFixtures(3);
    }

    public function testAggregateWithMultipleStages()
    {
        $cursor = $this->collection->aggregate(
            array(
                array('$sort' => array('x' => 1)),
                array('$match' => array('_id' => array('$gt' => 1))),
            ),
            array('batchSize' => 2)
        );

        $expected = array(
35 36
            array('_id' => 2, 'x' => 22),
            array('_id' => 3, 'x' => 33),
37 38
        );

39
        $this->assertSameDocuments($expected, $cursor);
40 41 42 43
    }

    public function testAggregateWithOut()
    {
44 45
        $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));

46
        if ( ! \MongoDB\server_supports_feature($server, self::$wireVersionForOutOperator)) {
47 48 49
            $this->markTestSkipped('$out aggregation pipeline operator is not supported');
        }

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        $outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
        $this->dropCollectionIfItExists($outputCollection);

        $this->collection->aggregate(
            array(
                array('$sort' => array('x' => 1)),
                array('$match' => array('_id' => array('$gt' => 1))),
                array('$out' => $outputCollection->getCollectionName()),
            )
        );

        $expected = array(
            array('_id' => 2, 'x' => 22),
            array('_id' => 3, 'x' => 33),
        );

66
        $this->assertSameDocuments($expected, $outputCollection->find());
67 68 69

        // Manually clean up our output collection
        $this->dropCollectionIfItExists($outputCollection);
70 71
    }
}