CrudSpecTest.php 3.65 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests\SpecTests;

use stdClass;
6 7 8
use function basename;
use function file_get_contents;
use function glob;
9 10 11 12 13 14 15 16

/**
 * Crud spec tests.
 *
 * @see https://github.com/mongodb/specifications/tree/master/source/crud
 */
class CrudSpecTest extends FunctionalTestCase
{
17
    /** @var array */
18
    private static $incompleteTests = [];
19

20 21 22 23 24 25 26 27
    /**
     * Assert that the expected and actual command documents match.
     *
     * @param stdClass $expected Expected command document
     * @param stdClass $actual   Actual command document
     */
    public static function assertCommandMatches(stdClass $expected, stdClass $actual)
    {
28 29 30 31 32 33 34
        foreach ($expected as $key => $value) {
            if ($value === null) {
                static::assertObjectNotHasAttribute($key, $actual);
                unset($expected->{$key});
            }
        }

35 36 37 38 39 40 41 42 43 44 45 46 47
        static::assertDocumentsMatch($expected, $actual);
    }

    /**
     * Execute an individual test case from the specification.
     *
     * @dataProvider provideTests
     * @param stdClass $test           Individual "tests[]" document
     * @param array    $runOn          Top-level "runOn" array with server requirements
     * @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
     */
48
    public function testCrud(stdClass $test, array $runOn = null, array $data, $databaseName = null, $collectionName = null)
49
    {
50 51 52 53
        if (isset(self::$incompleteTests[$this->dataDescription()])) {
            $this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);
        }

54 55 56 57 58 59 60 61
        if (isset($runOn)) {
            $this->checkServerRequirements($runOn);
        }

        if (isset($test->skipReason)) {
            $this->markTestSkipped($test->skipReason);
        }

62 63
        $databaseName = $databaseName ?? $this->getDatabaseName();
        $collectionName = $collectionName ?? $this->getCollectionName();
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

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

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

        if (isset($test->failPoint)) {
            $this->configureFailPoint($test->failPoint);
        }

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

        foreach ($test->operations as $operation) {
            Operation::fromCrud($operation)->assert($this, $context);
        }

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

        if (isset($test->outcome->collection->data)) {
            $this->assertOutcomeCollectionData($test->outcome->collection->data);
        }
    }

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

        foreach (glob(__DIR__ . '/crud/*.json') as $filename) {
            $json = $this->decodeJson(file_get_contents($filename));
            $group = basename($filename, '.json');
101 102 103 104
            $runOn = $json->runOn ?? null;
            $data = $json->data ?? [];
            $databaseName = $json->database_name ?? null;
            $collectionName = $json->collection_name ?? null;
105 106 107

            foreach ($json->tests as $test) {
                $name = $group . ': ' . $test->description;
108
                $testArgs[$name] = [$test, $runOn, $data, $databaseName, $collectionName];
109 110 111 112 113 114
            }
        }

        return $testArgs;
    }
}