You are here

public function BalanceTransactionTest::testBalanceTransactionExecution in Transaction 8

Tests balance transaction execution.

File

tests/src/Kernel/BalanceTransactionTest.php, line 120

Class

BalanceTransactionTest
Tests the balance transactor.

Namespace

Drupal\Tests\transaction\Kernel

Code

public function testBalanceTransactionExecution() {
  $transaction = $this->transaction;

  // Set an initial balance.
  $transaction
    ->set('field_balance', 10);
  $transaction
    ->set('field_amount', 10);
  $this
    ->assertTrue($transaction
    ->execute());

  // Checks the transaction status after its execution.
  $this
    ->assertEquals($transaction
    ->id(), $this->targetEntity
    ->get('field_last_transaction')->target_id);
  $this
    ->assertGreaterThan(0, $transaction
    ->getResultCode());
  $this
    ->assertEquals('Transaction executed successfully.', $transaction
    ->getResultMessage());
  $this
    ->assertEquals('Credit transaction', trim($transaction
    ->getDescription()));

  // Checks the result balance.
  $this
    ->assertEquals(20, $transaction
    ->get('field_balance')->value);
  $this
    ->assertEquals(20, $this->targetEntity
    ->get('field_balance')->value);
  $second_transaction = Transaction::create([
    'type' => $this->transactionType
      ->id(),
    'target_entity' => $this->targetEntity,
    'field_log_message' => $this->logMessage,
    // Initial balance must be ignored when at least one transactions was
    // previously executed.
    'field_balance' => 50,
    'field_amount' => -10,
  ]);
  $this->targetEntity
    ->set('field_balance', 50);
  $this
    ->assertTrue($second_transaction
    ->execute());

  // Checks the new transaction status.
  $this
    ->assertEquals('Debit transaction', trim($second_transaction
    ->getDescription()));

  // Checks the result balance.
  $this
    ->assertEquals(10, $second_transaction
    ->get('field_balance')->value);
  $this
    ->assertEquals(10, $this->targetEntity
    ->get('field_balance')->value);
  $this
    ->assertEquals($second_transaction
    ->id(), $this->targetEntity
    ->get('field_last_transaction')->target_id);
}