class TransactionManagerTest

Tests transactions.

Attributes

#[CoversClass(TransactionManagerBase::class)] #[Group('Database')]

Hierarchy

Expanded class hierarchy of TransactionManagerTest

File

core/tests/Drupal/Tests/Core/Database/TransactionManagerTest.php, line 19

Namespace

Drupal\Tests\Core\Database
View source
class TransactionManagerTest extends UnitTestCase {
  
  /**
   * A database connection.
   */
  protected Connection $connection;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->connection = new StubConnection($this->createStub(\PDO::class), []);
  }
  
  /**
   * Tests starting a transaction.
   */
  public function testStartTransaction() : void {
    $tx = $this->connection
      ->startTransaction();
    $this->assertSame(1, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertTrue($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertTrue($this->connection
      ->inTransaction());
    $tx->commitOrRelease();
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests failing to start a transaction.
   */
  public function testStartTransactionFailure() : void {
    $this->connection
      ->transactionManager()->success = FALSE;
    $this->expectException(TransactionException::class);
    $this->expectExceptionMessage('Failed start of transaction');
    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    $tx = $this->connection
      ->startTransaction();
  }
  
  /**
   * Checks TransactionManager after failing to start a transaction.
   */
  public function testStateAfterStartTransactionFailure() : void {
    try {
      $this->connection
        ->transactionManager()->success = FALSE;
      // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
      $tx = $this->connection
        ->startTransaction();
    } catch (TransactionException) {
      // Continue.
    }
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests starting a savepoint.
   */
  public function testStartSavepoint() : void {
    $tx = $this->connection
      ->startTransaction();
    $savepoint = $this->connection
      ->startTransaction('foo');
    $this->assertSame(2, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertTrue($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertTrue($this->connection
      ->inTransaction());
    $savepoint->commitOrRelease();
    $tx->commitOrRelease();
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests failing to start a savepoint.
   */
  public function testStartSavepointFailure() : void {
    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    $tx = $this->connection
      ->startTransaction();
    $this->connection
      ->transactionManager()->success = FALSE;
    $this->expectException(TransactionException::class);
    $this->expectExceptionMessage('Failed start of savepoint foo');
    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    $savepoint = $this->connection
      ->startTransaction('foo');
  }
  
  /**
   * Checks TransactionManager after failing to start a savepoint.
   */
  public function testStateAfterStartSavepointFailure() : void {
    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    $tx = $this->connection
      ->startTransaction();
    try {
      $this->connection
        ->transactionManager()->success = FALSE;
      // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
      $savepoint = $this->connection
        ->startTransaction('foo');
    } catch (TransactionException) {
      // Continue.
    }
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests committing a transaction.
   */
  public function testTransactionCommit() : void {
    $tx = $this->connection
      ->startTransaction();
    $tx->commitOrRelease();
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests a transaction commit failure.
   */
  public function testTransactionCommitFailure() : void {
    $tx = $this->connection
      ->startTransaction();
    $this->connection
      ->transactionManager()->success = FALSE;
    $this->expectException(TransactionCommitFailedException::class);
    $this->expectExceptionMessage('Failed commit of transaction');
    $tx->commitOrRelease();
  }
  
  /**
   * Checks TransactionManager after transaction commit failure.
   */
  public function testStateAfterTransactionCommitFailure() : void {
    try {
      $tx = $this->connection
        ->startTransaction();
      $this->connection
        ->transactionManager()->success = FALSE;
      $tx->commitOrRelease();
    } catch (TransactionCommitFailedException) {
      // Continue.
    }
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests releasing a savepoint.
   */
  public function testTransactionReleaseSavepoint() : void {
    $tx = $this->connection
      ->startTransaction();
    $savepoint = $this->connection
      ->startTransaction('foo');
    $savepoint->commitOrRelease();
    $this->assertSame(1, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertTrue($this->connection
      ->inTransaction());
    $tx->commitOrRelease();
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests a transaction release savepoint failure.
   */
  public function testTransactionReleaseSavepointFailure() : void {
    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    $tx = $this->connection
      ->startTransaction();
    $savepoint = $this->connection
      ->startTransaction('foo');
    $this->connection
      ->transactionManager()->success = FALSE;
    $this->expectException(TransactionCommitFailedException::class);
    $this->expectExceptionMessage('Failed release of savepoint foo');
    $savepoint->commitOrRelease();
  }
  
  /**
   * Checks TransactionManager after release savepoint failure.
   */
  public function testStateAfterTransactionReleaseSavepointFailure() : void {
    try {
      // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
      $tx = $this->connection
        ->startTransaction();
      $savepoint = $this->connection
        ->startTransaction('foo');
      $this->connection
        ->transactionManager()->success = FALSE;
      $savepoint->commitOrRelease();
    } catch (TransactionCommitFailedException) {
      // Continue.
    }
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests rolling back a transaction.
   */
  public function testTransactionRollback() : void {
    $tx = $this->connection
      ->startTransaction();
    $tx->rollback();
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests a transaction rollback failure.
   */
  public function testTransactionRollbackFailure() : void {
    $tx = $this->connection
      ->startTransaction();
    $this->connection
      ->transactionManager()->success = FALSE;
    $this->expectException(TransactionException::class);
    $this->expectExceptionMessage('Failed rollback of transaction');
    $tx->rollback();
  }
  
  /**
   * Checks TransactionManager after transaction rollback failure.
   */
  public function testStateAfterTransactionRollbackFailure() : void {
    try {
      $tx = $this->connection
        ->startTransaction();
      $this->connection
        ->transactionManager()->success = FALSE;
      $tx->rollback();
    } catch (TransactionException) {
      // Continue.
    }
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests rolling back a transaction to a savepoint.
   */
  public function testTransactionRollbackToSavepoint() : void {
    $tx = $this->connection
      ->startTransaction();
    $savepoint = $this->connection
      ->startTransaction('foo');
    $savepoint->rollback();
    $this->assertSame(1, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertTrue($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertTrue($this->connection
      ->inTransaction());
    $tx->commitOrRelease();
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }
  
  /**
   * Tests failure when rolling back a transaction to a savepoint.
   */
  public function testTransactionRollbackToSavepointFailure() : void {
    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    $tx = $this->connection
      ->startTransaction();
    $savepoint = $this->connection
      ->startTransaction('foo');
    $this->connection
      ->transactionManager()->success = FALSE;
    $this->expectException(TransactionException::class);
    $this->expectExceptionMessage('Failed rollback of savepoint foo');
    $savepoint->rollback();
  }
  
  /**
   * Checks TransactionManager after failure on rolling back to a savepoint.
   */
  public function testStateAfterTransactionRollbackToSavepointFailure() : void {
    try {
      // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
      $tx = $this->connection
        ->startTransaction();
      $savepoint = $this->connection
        ->startTransaction('foo');
      $this->connection
        ->transactionManager()->success = FALSE;
      $savepoint->rollback();
    } catch (TransactionException) {
      // Continue.
    }
    $this->assertSame(0, $this->connection
      ->transactionManager()
      ->stackDepth());
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('foo'));
    $this->assertFalse($this->connection
      ->transactionManager()
      ->has('drupal_transaction'));
    $this->assertFalse($this->connection
      ->inTransaction());
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DrupalTestCase::$root protected property The Drupal root directory.
DrupalTestCase::getDrupalRoot Deprecated protected function Returns the Drupal root directory. 1
DrupalTestCase::setUpRoot final protected function Ensure that the $root property is set initially.
DrupalTestCase::skipTestWithAttribute final protected function Supports skipping tests with the Skip attribute.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
ForwardCompatibilityTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
ForwardCompatibilityTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
TransactionManagerTest::$connection protected property A database connection.
TransactionManagerTest::setUp protected function Overrides UnitTestCase::setUp
TransactionManagerTest::testStartSavepoint public function Tests starting a savepoint.
TransactionManagerTest::testStartSavepointFailure public function Tests failing to start a savepoint.
TransactionManagerTest::testStartTransaction public function Tests starting a transaction.
TransactionManagerTest::testStartTransactionFailure public function Tests failing to start a transaction.
TransactionManagerTest::testStateAfterStartSavepointFailure public function Checks TransactionManager after failing to start a savepoint.
TransactionManagerTest::testStateAfterStartTransactionFailure public function Checks TransactionManager after failing to start a transaction.
TransactionManagerTest::testStateAfterTransactionCommitFailure public function Checks TransactionManager after transaction commit failure.
TransactionManagerTest::testStateAfterTransactionReleaseSavepointFailure public function Checks TransactionManager after release savepoint failure.
TransactionManagerTest::testStateAfterTransactionRollbackFailure public function Checks TransactionManager after transaction rollback failure.
TransactionManagerTest::testStateAfterTransactionRollbackToSavepointFailure public function Checks TransactionManager after failure on rolling back to a savepoint.
TransactionManagerTest::testTransactionCommit public function Tests committing a transaction.
TransactionManagerTest::testTransactionCommitFailure public function Tests a transaction commit failure.
TransactionManagerTest::testTransactionReleaseSavepoint public function Tests releasing a savepoint.
TransactionManagerTest::testTransactionReleaseSavepointFailure public function Tests a transaction release savepoint failure.
TransactionManagerTest::testTransactionRollback public function Tests rolling back a transaction.
TransactionManagerTest::testTransactionRollbackFailure public function Tests a transaction rollback failure.
TransactionManagerTest::testTransactionRollbackToSavepoint public function Tests rolling back a transaction to a savepoint.
TransactionManagerTest::testTransactionRollbackToSavepointFailure public function Tests failure when rolling back a transaction to a savepoint.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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