You are here

public function PaymentTest::testPreSave in Commerce Core 8.2

Tests the preSave logic.

@covers ::preSave

File

modules/payment/tests/src/Kernel/Entity/PaymentTest.php, line 220

Class

PaymentTest
Tests the payment entity.

Namespace

Drupal\Tests\commerce_payment\Kernel\Entity

Code

public function testPreSave() {
  $request_time = \Drupal::time()
    ->getRequestTime();

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = Payment::create([
    'type' => 'payment_default',
    'payment_gateway' => 'example',
    'order_id' => $this->order
      ->id(),
    'amount' => new Price('30', 'USD'),
    'state' => 'authorization',
  ]);
  $this
    ->assertEmpty($payment
    ->getPaymentGatewayMode());
  $this
    ->assertEmpty($payment
    ->getRefundedAmount());
  $this
    ->assertEmpty($payment
    ->getAuthorizedTime());
  $this
    ->assertEmpty($payment
    ->getCompletedTime());

  // Confirm that getBalance() works before the payment is saved.
  $this
    ->assertEquals(new Price('30', 'USD'), $payment
    ->getBalance());
  $payment
    ->save();
  $this
    ->assertEquals('test', $payment
    ->getPaymentGatewayMode());
  $this
    ->assertEquals(new Price('0', 'USD'), $payment
    ->getRefundedAmount());
  $this
    ->assertEquals($request_time, $payment
    ->getAuthorizedTime());
  $this
    ->assertEmpty($payment
    ->getCompletedTime());
  $payment
    ->setState('completed');
  $payment
    ->save();
  $this
    ->assertEquals($request_time, $payment
    ->getCompletedTime());
}