FunctionalTestCase.php 3.41 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
use stdClass;
10
use UnexpectedValueException;
11 12 13 14 15 16 17 18 19

abstract class FunctionalTestCase extends TestCase
{
    protected $manager;

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

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

Jeremy Mikola's avatar
Jeremy Mikola committed
25 26
        $cursor = $this->manager->executeCommand($databaseName, new Command(['count' => $collectionName]));
        $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
27
        $document = current($cursor->toArray());
28

29 30 31 32
        $this->assertArrayHasKey('n', $document);
        $this->assertEquals($count, $document['n']);
    }

33
    protected function assertCommandSucceeded($document)
34
    {
35
        $document = is_object($document) ? (array) $document : $document;
36

37 38 39
        $this->assertArrayHasKey('ok', $document);
        $this->assertEquals(1, $document['ok']);
    }
40

41
    protected function assertSameObjectId($expectedObjectId, $actualObjectId)
42
    {
43 44 45
        $this->assertInstanceOf('MongoDB\BSON\ObjectId', $expectedObjectId);
        $this->assertInstanceOf('MongoDB\BSON\ObjectId', $actualObjectId);
        $this->assertEquals((string) $expectedObjectId, (string) $actualObjectId);
46 47
    }

48
    protected function getFeatureCompatibilityVersion(ReadPreference $readPreference = null)
49
    {
50
        if (version_compare($this->getServerVersion(), '3.4.0', '<')) {
51
            return $this->getServerVersion($readPreference);
52
        }
53

54
        $cursor = $this->manager->executeCommand(
55 56 57
            'admin',
            new Command(['getParameter' => 1, 'featureCompatibilityVersion' => 1]),
            $readPreference ?: new ReadPreference(ReadPreference::RP_PRIMARY)
58 59 60 61 62
        );

        $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
        $document = current($cursor->toArray());

63 64 65 66 67 68 69 70 71 72 73
        // MongoDB 3.6: featureCompatibilityVersion is an embedded document
        if (isset($document['featureCompatibilityVersion']['version']) && is_string($document['featureCompatibilityVersion']['version'])) {
            return $document['featureCompatibilityVersion']['version'];
        }

        // MongoDB 3.4: featureCompatibilityVersion is a string
        if (isset($document['featureCompatibilityVersion']) && is_string($document['featureCompatibilityVersion'])) {
            return $document['featureCompatibilityVersion'];
        }

        throw new UnexpectedValueException('Could not determine featureCompatibilityVersion');
74 75
    }

76 77 78 79 80
    protected function getPrimaryServer()
    {
        return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
    }

81 82 83 84
    protected function getServerVersion(ReadPreference $readPreference = null)
    {
        $cursor = $this->manager->executeCommand(
            $this->getDatabaseName(),
Jeremy Mikola's avatar
Jeremy Mikola committed
85
            new Command(['buildInfo' => 1]),
86 87 88
            $readPreference ?: new ReadPreference(ReadPreference::RP_PRIMARY)
        );

Jeremy Mikola's avatar
Jeremy Mikola committed
89
        $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
90 91
        $document = current($cursor->toArray());

92 93 94 95 96
        if (isset($document['version']) && is_string($document['version'])) {
            return $document['version'];
        }

        throw new UnexpectedValueException('Could not determine server version');
97
    }
98
}