FunctionalTestCase.php 1.35 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests;

5
use MongoDB\Driver\Command;
6
use MongoDB\Driver\Cursor;
7 8
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
9 10 11 12 13 14 15 16 17

abstract class FunctionalTestCase extends TestCase
{
    protected $manager;

    public function setUp()
    {
        $this->manager = new Manager($this->getUri());
    }
18

19
    protected function assertCollectionCount($namespace, $count)
20 21 22
    {
        list($databaseName, $collectionName) = explode('.', $namespace, 2);

23
        $cursor = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
24

25
        $document = current($cursor->toArray());
26 27 28 29
        $this->assertArrayHasKey('n', $document);
        $this->assertEquals($count, $document['n']);
    }

30
    protected function assertCommandSucceeded(Cursor $cursor)
31
    {
32
        $document = current($cursor->toArray());
33 34 35
        $this->assertArrayHasKey('ok', $document);
        $this->assertEquals(1, $document['ok']);
    }
36 37 38 39 40 41 42 43 44 45 46 47 48

    protected function getServerVersion(ReadPreference $readPreference = null)
    {
        $cursor = $this->manager->executeCommand(
            $this->getDatabaseName(),
            new Command(array('buildInfo' => 1)),
            $readPreference ?: new ReadPreference(ReadPreference::RP_PRIMARY)
        );

        $document = current($cursor->toArray());

        return $document['version'];
    }
49
}