RetryableWritesSpecTest.php 2.33 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\SpecTests;

5 6 7
use LogicException;
use stdClass;

8 9 10 11 12 13 14 15 16 17 18
/**
 * Retryable writes spec tests.
 *
 * @see https://github.com/mongodb/specifications/tree/master/source/retryable-writes
 */
class RetryableWritesSpecTest extends FunctionalTestCase
{
    /**
     * Execute an individual test case from the specification.
     *
     * @dataProvider provideTests
19 20 21 22
     * @param string   $name  Test name
     * @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
23
     */
24
    public function testRetryableWrites($name, stdClass $test, array $runOn = null, array $data)
25 26 27
    {
        $this->setName($name);

28 29 30 31 32
        // TODO: Revise this once a test environment with multiple mongos nodes is available (see: PHPLIB-430)
        if (isset($test->useMultipleMongoses) && $test->useMultipleMongoses && $this->isShardedCluster()) {
            $this->markTestSkipped('"useMultipleMongoses" is not supported');
        }

33 34 35 36
        if (isset($runOn)) {
            $this->checkServerRequirements($runOn);
        }

37 38
        $context = Context::fromRetryableWrites($test, $this->getDatabaseName(), $this->getCollectionName());
        $this->setContext($context);
39

40 41
        $this->dropTestAndOutcomeCollections();
        $this->insertDataFixtures($data);
42

43 44
        if (isset($test->failPoint)) {
            $this->configureFailPoint($test->failPoint);
45 46
        }

47
        Operation::fromRetryableWrites($test->operation, $test->outcome)->assert($this, $context);
48

49 50
        if (isset($test->outcome->collection->data)) {
            $this->assertOutcomeCollectionData($test->outcome->collection->data);
51 52 53 54 55 56 57 58
        }
    }

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

        foreach (glob(__DIR__ . '/retryable-writes/*.json') as $filename) {
59
            $json = $this->decodeJson(file_get_contents($filename));
60
            $group = basename($filename, '.json');
61 62
            $runOn = isset($json->runOn) ? $json->runOn : null;
            $data = isset($json->data) ? $json->data : [];
63

64 65
            foreach ($json->tests as $test) {
                $name = $group . ': ' . $test->description;
66 67 68 69 70 71 72
                $testArgs[] = [$name, $test, $runOn, $data];
            }
        }

        return $testArgs;
    }
}