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

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\CreateIndexes;
6 7
use MongoDB\Operation\ModifyCollection;
use function array_key_exists;
8 9 10 11 12

class ModifyCollectionFunctionalTest extends FunctionalTestCase
{
    public function testCollMod()
    {
13
        $this->createCollection();
14 15 16 17 18 19 20 21 22

        $indexes = [['key' => ['lastAccess' => 1], 'expireAfterSeconds' => 3]];
        $createIndexes = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createIndexes->execute($this->getPrimaryServer());

        $modifyCollection = new ModifyCollection(
            $this->getDatabaseName(),
            $this->getCollectionName(),
            ['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]],
23
            ['typeMap' => ['root' => 'array', 'document' => 'array']]
24 25 26
        );
        $result = $modifyCollection->execute($this->getPrimaryServer());

27 28 29 30 31 32 33 34 35 36 37 38 39 40
        if (array_key_exists('raw', $result)) {
            /* Sharded environment, where we only assert if a shard had a successful update. For
             * non-primary shards that don't have chunks for the collection, the result contains a
             * "ns does not exist" error. */
            foreach ($result['raw'] as $shard) {
                if (array_key_exists('ok', $shard) && $shard['ok'] == 1) {
                    $this->assertSame(3, $shard['expireAfterSeconds_old']);
                    $this->assertSame(1000, $shard['expireAfterSeconds_new']);
                }
            }
        } else {
            $this->assertSame(3, $result['expireAfterSeconds_old']);
            $this->assertSame(1000, $result['expireAfterSeconds_new']);
        }
41 42
    }
}