function TransactionTest::testRollbackAfterDdlStatementForNonTransactionalDdlDatabase

Tests rollback after a DDL statement when no transactional DDL supported.

File

core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php, line 647

Class

TransactionTest
Tests the transactions, using the explicit ::commitOrRelease method.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testRollbackAfterDdlStatementForNonTransactionalDdlDatabase() : void {
    if ($this->connection
        ->supportsTransactionalDDL()) {
        $this->markTestSkipped('This test only works for database that do not support transactional DDL.');
    }
    // For database servers that do not support transactional DDL,
    // the DDL statement should commit the transaction stack.
    $this->cleanUp();
    $transaction = $this->createRootTransaction('', FALSE);
    $reflectionMethod = new \ReflectionMethod(get_class($this->connection
        ->transactionManager()), 'getConnectionTransactionState');
    $this->assertSame(1, $this->connection
        ->transactionManager()
        ->stackDepth());
    $this->assertEquals(ClientConnectionTransactionState::Active, $reflectionMethod->invoke($this->connection
        ->transactionManager()));
    $this->insertRow('row');
    $this->executeDDLStatement();
    $this->assertSame(0, $this->connection
        ->transactionManager()
        ->stackDepth());
    $this->assertEquals(ClientConnectionTransactionState::Voided, $reflectionMethod->invoke($this->connection
        ->transactionManager()));
    // Try to rollback the root transaction. Since the DDL already committed
    // it, it should fail.
    set_error_handler(static function (int $errno, string $errstr) : bool {
        throw new \ErrorException($errstr);
    });
    try {
        $transaction->rollBack();
    } catch (\ErrorException $e) {
        $this->assertSame('Transaction::rollBack() failed because of a prior execution of a DDL statement.', $e->getMessage());
    } finally {
        restore_error_handler();
    }
    try {
        $transaction->commitOrRelease();
        $this->fail('TransactionOutOfOrderException was expected, but did not throw.');
    } catch (TransactionOutOfOrderException) {
        // Just continue, the attempted rollback made the overall state to
        // ClientConnectionTransactionState::RollbackFailed.
    }
    $manager = $this->connection
        ->transactionManager();
    $this->assertSame(0, $manager->stackDepth());
    $reflectedTransactionState = new \ReflectionMethod($manager, 'getConnectionTransactionState');
    $this->assertSame(ClientConnectionTransactionState::RollbackFailed, $reflectedTransactionState->invoke($manager));
    $this->assertRowPresent('row');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.