CommandMonitoringSpecTest.php 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php

namespace MongoDB\Tests\SpecTests;

use stdClass;

/**
 * Command monitoring spec tests.
 *
 * @see https://github.com/mongodb/specifications/tree/master/source/command-monitoring
 */
class CommandMonitoringSpecTest extends FunctionalTestCase
{
    /**
     * Assert that the expected and actual command documents match.
     *
     * Note: this method may modify the $expected object.
     *
     * @param stdClass $expected Expected command document
     * @param stdClass $actual   Actual command document
     */
    public static function assertCommandMatches(stdClass $expected, stdClass $actual)
    {
        if (isset($expected->getMore) && $expected->getMore === 42) {
            static::assertObjectHasAttribute('getMore', $actual);
            static::assertThat($actual->getMore, static::logicalOr(
                static::isInstanceOf(Int64::class),
                static::isType('integer')
            ));
            unset($expected->getMore);
        }

        if (isset($expected->killCursors) && isset($expected->cursors) && is_array($expected->cursors)) {
            static::assertObjectHasAttribute('cursors', $actual);
35
            static::assertIsArray($actual->cursors);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

            foreach ($expected->cursors as $i => $cursorId) {
                static::assertArrayHasKey($i, $actual->cursors);

                if ($cursorId === 42) {
                    static::assertThat($actual->cursors[$i], static::logicalOr(
                        static::isInstanceOf(Int64::class),
                        static::isType('integer')
                    ));
                }
            }

            unset($expected->cursors);
        }

        static::assertDocumentsMatch($expected, $actual);
    }

    /**
     * Assert that the expected and actual command reply documents match.
     *
     * Note: this method may modify the $expectedReply object.
     *
     * @param stdClass $expected Expected command reply document
     * @param stdClass $actual   Actual command reply document
     */
    public static function assertCommandReplyMatches(stdClass $expected, stdClass $actual)
    {
        if (isset($expected->cursor->id) && $expected->cursor->id === 42) {
            static::assertObjectHasAttribute('cursor', $actual);
66
            static::assertIsObject($actual->cursor);
67 68 69 70 71 72 73 74 75 76
            static::assertObjectHasAttribute('id', $actual->cursor);
            static::assertThat($actual->cursor->id, static::logicalOr(
                static::isInstanceOf(Int64::class),
                static::isType('integer')
            ));
            unset($expected->cursor->id);
        }

        if (isset($expected->cursorsUnknown) && is_array($expected->cursorsUnknown)) {
            static::assertObjectHasAttribute('cursorsUnknown', $actual);
77
            static::assertIsArray($actual->cursorsUnknown);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

            foreach ($expected->cursorsUnknown as $i => $cursorId) {
                static::assertArrayHasKey($i, $actual->cursorsUnknown);

                if ($cursorId === 42) {
                    static::assertThat($actual->cursorsUnknown[$i], static::logicalOr(
                        static::isInstanceOf(Int64::class),
                        static::isType('integer')
                    ));
                }
            }

            unset($expected->cursorsUnknown);
        }

        if (isset($expected->ok) && is_numeric($expected->ok)) {
            static::assertObjectHasAttribute('ok', $actual);
95
            static::assertIsNumeric($actual->ok);
96 97 98 99 100 101
            static::assertEquals($expected->ok, $actual->ok);
            unset($expected->ok);
        }

        if (isset($expected->writeErrors) && is_array($expected->writeErrors)) {
            static::assertObjectHasAttribute('writeErrors', $actual);
102
            static::assertIsArray($actual->writeErrors);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

            foreach ($expected->writeErrors as $i => $expectedWriteError) {
                static::assertArrayHasKey($i, $actual->writeErrors);
                $actualWriteError = $actual->writeErrors[$i];

                if (isset($expectedWriteError->code) && $expectedWriteError->code === 42) {
                    static::assertObjectHasAttribute('code', $actualWriteError);
                    static::assertThat($actualWriteError->code, static::logicalOr(
                        static::isInstanceOf(Int64::class),
                        static::isType('integer')
                    ));
                    unset($expected->writeErrors[$i]->code);
                }

                if (isset($expectedWriteError->errmsg) && $expectedWriteError->errmsg === '') {
                    static::assertObjectHasAttribute('errmsg', $actualWriteError);
119
                    static::assertIsString($actualWriteError->errmsg);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                    static::assertNotEmpty($actualWriteError->errmsg);
                    unset($expected->writeErrors[$i]->errmsg);
                }
            }
        }

        static::assertDocumentsMatch($expected, $actual);
    }

    /**
     * Execute an individual test case from the specification.
     *
     * @dataProvider provideTests
     * @param stdClass  $test           Individual "tests[]" document
     * @param array     $data           Top-level "data" array to initialize collection
     * @param string    $databaseName   Name of database under test
     * @param string    $collectionName Name of collection under test
     */
138
    public function testCommandMonitoring(stdClass $test, array $data, $databaseName = null, $collectionName = null)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    {
        $this->checkServerRequirements($this->createRunOn($test));

        $databaseName = isset($databaseName) ? $databaseName : $this->getDatabaseName();
        $collectionName = isset($collectionName) ? $collectionName : $this->getCollectionName();

        $context = Context::fromCommandMonitoring($test, $databaseName, $collectionName);
        $this->setContext($context);

        $this->dropTestAndOutcomeCollections();
        $this->insertDataFixtures($data);

        if (isset($test->expectations)) {
            $commandExpectations = CommandExpectations::fromCommandMonitoring($test->expectations);
            $commandExpectations->startMonitoring();
        }

        Operation::fromCommandMonitoring($test->operation)->assert($this, $context);

        if (isset($commandExpectations)) {
            $commandExpectations->stopMonitoring();
            $commandExpectations->assert($this, $context);
        }
    }

    public function provideTests()
    {
        $testArgs = [];

        foreach (glob(__DIR__ . '/command-monitoring/*.json') as $filename) {
            $json = $this->decodeJson(file_get_contents($filename));
            $group = basename($filename, '.json');
            $data = isset($json->data) ? $json->data : [];
            $databaseName = isset($json->database_name) ? $json->database_name : null;
            $collectionName = isset($json->collection_name) ? $json->collection_name : null;

            foreach ($json->tests as $test) {
                $name = $group . ': ' . $test->description;
177
                $testArgs[$name] = [$test, $data, $databaseName, $collectionName];
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
            }
        }

        return $testArgs;
    }

    /**
     * Convert the server and topology requirements to a standard "runOn" array
     * used by other specifications.
     *
     * @param stdClass $test
     * @return array
     */
    private function createRunOn(stdClass $test)
    {
        $req = new stdClass;

        $topologies = [
            self::TOPOLOGY_SINGLE,
            self::TOPOLOGY_REPLICASET,
            self::TOPOLOGY_SHARDED
        ];

        /* Append ".99" as patch version, since command monitoring tests expect
         * the minor version to be an inclusive upper bound. */
        if (isset($test->ignore_if_server_version_greater_than)) {
            $req->maxServerVersion = $test->ignore_if_server_version_greater_than . '.99';
        }

        if (isset($test->ignore_if_server_version_less_than)) {
            $req->minServerVersion = $test->ignore_if_server_version_less_than;
        }

        if (isset($test->ignore_if_topology_type)) {
            $req->topology = array_diff($topologies, $test->ignore_if_topology_type);
        }

        return [$req];
    }
}