You are here

public function PaymentTest::testOrderIntegration in Commerce Core 8.2

Tests the order integration (total_paid field).

@covers ::postSave @covers ::postDelete

File

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

Class

PaymentTest
Tests the payment entity.

Namespace

Drupal\Tests\commerce_payment\Kernel\Entity

Code

public function testOrderIntegration() {
  $this
    ->assertEquals(new Price('0', 'USD'), $this->order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('30', 'USD'), $this->order
    ->getBalance());

  /** @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' => 'completed',
  ]);
  $payment
    ->save();
  $this->order
    ->save();
  $this
    ->assertEquals(new Price('30', 'USD'), $this->order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('0', 'USD'), $this->order
    ->getBalance());
  $payment
    ->setRefundedAmount(new Price('15', 'USD'));
  $payment
    ->setState('partially_refunded');
  $payment
    ->save();
  $this->order
    ->save();
  $this
    ->assertEquals(new Price('15', 'USD'), $this->order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('15', 'USD'), $this->order
    ->getBalance());
  $payment
    ->delete();

  // Confirm that if the order isn't explicitly saved, it will be saved
  // at the end of the request.
  $request = $this->container
    ->get('request_stack')
    ->getCurrentRequest();
  $kernel = $this->container
    ->get('kernel');
  $kernel
    ->terminate($request, new Response());
  $this->order = $this
    ->reloadEntity($this->order);
  $this
    ->assertEquals(new Price('0', 'USD'), $this->order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('30', 'USD'), $this->order
    ->getBalance());
}