CommandMonitoringSpecTest.php 8.12 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests\SpecTests;

use stdClass;
6 7 8 9 10 11
use function array_diff;
use function basename;
use function file_get_contents;
use function glob;
use function is_array;
use function is_numeric;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 * 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);
41
            static::assertIsArray($actual->cursors);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

            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);
72
            static::assertIsObject($actual->cursor);
73 74 75 76 77 78 79 80 81 82
            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);
83
            static::assertIsArray($actual->cursorsUnknown);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

            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);
101
            static::assertIsNumeric($actual->ok);
102 103 104 105 106 107
            static::assertEquals($expected->ok, $actual->ok);
            unset($expected->ok);
        }

        if (isset($expected->writeErrors) && is_array($expected->writeErrors)) {
            static::assertObjectHasAttribute('writeErrors', $actual);
108
            static::assertIsArray($actual->writeErrors);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

            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);
125
                    static::assertIsString($actualWriteError->errmsg);
126 127 128 129 130 131 132 133 134 135 136 137 138
                    static::assertNotEmpty($actualWriteError->errmsg);
                    unset($expected->writeErrors[$i]->errmsg);
                }
            }
        }

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

    /**
     * Execute an individual test case from the specification.
     *
     * @dataProvider provideTests
139 140 141 142
     * @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
143
     */
144
    public function testCommandMonitoring(stdClass $test, array $data, $databaseName = null, $collectionName = null)
145 146 147
    {
        $this->checkServerRequirements($this->createRunOn($test));

148 149
        $databaseName = $databaseName ?? $this->getDatabaseName();
        $collectionName = $collectionName ?? $this->getCollectionName();
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

        $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');
177 178 179
            $data = $json->data ?? [];
            $databaseName = $json->database_name ?? null;
            $collectionName = $json->collection_name ?? null;
180 181 182

            foreach ($json->tests as $test) {
                $name = $group . ': ' . $test->description;
183
                $testArgs[$name] = [$test, $data, $databaseName, $collectionName];
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
            }
        }

        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)
    {
199
        $req = new stdClass();
200 201 202 203

        $topologies = [
            self::TOPOLOGY_SINGLE,
            self::TOPOLOGY_REPLICASET,
204
            self::TOPOLOGY_SHARDED,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        ];

        /* 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];
    }
}