You are here

public function PaymentOptionsBuilderTest::testSelectDefaultOption in Commerce Core 8.2

Tests selecting the default option.

@covers ::selectDefaultOption

File

modules/payment/tests/src/Kernel/PaymentOptionsBuilderTest.php, line 293

Class

PaymentOptionsBuilderTest
Tests the payment options builder.

Namespace

Drupal\Tests\commerce_payment\Kernel

Code

public function testSelectDefaultOption() {
  $options = $this->paymentOptionsBuilder
    ->buildOptions($this->order);

  // The order payment method is selected first.
  $default_option = $this->paymentOptionsBuilder
    ->selectDefaultOption($this->order, $options);
  $this
    ->assertEquals($options[3], $default_option);

  // The order payment gateway is selected second.
  $this->order
    ->set('payment_method', NULL);
  $this->order
    ->set('payment_gateway', 'cash_on_delivery');
  $default_option = $this->paymentOptionsBuilder
    ->selectDefaultOption($this->order, $options);
  $this
    ->assertEquals($options['cash_on_delivery'], $default_option);

  // Finally, the method falls back to the first option.
  $this->order
    ->set('payment_gateway', NULL);
  $default_option = $this->paymentOptionsBuilder
    ->selectDefaultOption($this->order, $options);
  $this
    ->assertEquals(reset($options), $default_option);

  // Non-available order payment method is ignored.
  $this->order
    ->set('payment_method', '2');
  $this->order
    ->set('payment_gateway', 'onsite');
  $default_option = $this->paymentOptionsBuilder
    ->selectDefaultOption($this->order, $options);
  $this
    ->assertEquals(reset($options), $default_option);

  // Non-available order payment gateway is ignored.
  $this->order
    ->set('payment_method', NULL);
  $this->order
    ->set('payment_gateway', 'card_on_delivery');
  $default_option = $this->paymentOptionsBuilder
    ->selectDefaultOption($this->order, $options);
  $this
    ->assertEquals(reset($options), $default_option);
}