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

namespace MongoDB\Tests\Collection\CrudSpec;

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

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

18 19 20 21 22 23 24 25 26 27
    public function setUp()
    {
        parent::setUp();

        $this->createFixtures(3);
    }

    public function testAggregateWithMultipleStages()
    {
        $cursor = $this->collection->aggregate(
Jeremy Mikola's avatar
Jeremy Mikola committed
28 29 30 31 32
            [
                ['$sort' => ['x' => 1]],
                ['$match' => ['_id' => ['$gt' => 1]]],
            ],
            ['batchSize' => 2]
33 34
        );

Jeremy Mikola's avatar
Jeremy Mikola committed
35 36 37 38
        $expected = [
            ['_id' => 2, 'x' => 22],
            ['_id' => 3, 'x' => 33],
        ];
39

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

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

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

51
        $outputCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '_output');
52 53
        $operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
        $operation->execute($this->getPrimaryServer());
54 55

        $this->collection->aggregate(
Jeremy Mikola's avatar
Jeremy Mikola committed
56 57 58 59 60
            [
                ['$sort' => ['x' => 1]],
                ['$match' => ['_id' => ['$gt' => 1]]],
                ['$out' => $outputCollection->getCollectionName()],
            ]
61 62
        );

Jeremy Mikola's avatar
Jeremy Mikola committed
63 64 65 66
        $expected = [
            ['_id' => 2, 'x' => 22],
            ['_id' => 3, 'x' => 33],
        ];
67

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

        // Manually clean up our output collection
71 72
        $operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
        $operation->execute($this->getPrimaryServer());
73
    }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    public function testAggregateWithoutCursorDoesNotApplyTypemap()
    {
        $pipeline = [
            ['$group' => [
                '_id' => null,
                'count' => ['$sum' => 1]
            ]]
        ];
        $options = ['useCursor' => false];
        $result = $this->collection->aggregate($pipeline, $options);

        $expected = [
            [
                '_id' => null,
                'count' => 3,
            ],
        ];

        $this->assertSameDocuments($expected, $result);
    }
95
}