You are here

public function SubscriptionLifecycleTest::testSubscriptionUpdates in Commerce Recurring Framework 8

Tests that updating an active subscription also updates the current order.

File

tests/src/Kernel/SubscriptionLifecycleTest.php, line 143

Class

SubscriptionLifecycleTest
Tests the subscription lifecycle.

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testSubscriptionUpdates() {
  $initial_order = $this
    ->createInitialOrder();

  // Set a payment gateway and place the order so the subscription gets
  // created.
  $initial_order
    ->set('payment_method', $this->paymentMethod);
  $initial_order
    ->getState()
    ->applyTransitionById('place');
  $initial_order
    ->save();

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = Subscription::load(1);
  $this
    ->assertEquals('active', $subscription
    ->getState()
    ->getId());

  /** @var \Drupal\commerce_order\Entity\OrderInterface $recurring_order */
  $recurring_order = Order::load(2);
  $this
    ->assertEquals('recurring', $recurring_order
    ->bundle());
  $this
    ->assertEquals($recurring_order
    ->id(), $subscription
    ->getCurrentOrder()
    ->id());

  // Check that updating the payment method of the subscription also updates
  // the current recurring order.
  $new_payment_gateway = PaymentGateway::create([
    'id' => 'example_2',
    'label' => 'Example 2',
    'plugin' => 'example_onsite',
  ]);
  $new_payment_gateway
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $new_payment_method */
  $new_payment_method = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => $new_payment_gateway,
    'card_type' => 'visa',
    'uid' => $this->user
      ->id(),
  ]);
  $new_payment_method
    ->save();
  $subscription
    ->setPaymentMethod($new_payment_method);
  $subscription
    ->save();
  $recurring_order = $this
    ->reloadEntity($recurring_order);
  $this
    ->assertEquals($new_payment_method
    ->id(), $subscription
    ->getPaymentMethodId());
  $this
    ->assertEquals($new_payment_method
    ->id(), $recurring_order
    ->get('payment_method')->target_id);
  $this
    ->assertEquals($new_payment_gateway
    ->id(), $recurring_order
    ->get('payment_gateway')->target_id);
}