You are here

public function SubscriptionLifecycleTest::testLifecycle in Commerce Recurring Framework 8

Tests the subscription lifecycle, without a free trial.

Placing an initial order should create an active subscription. Canceling the initial order should cancel the subscription.

File

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

Class

SubscriptionLifecycleTest
Tests the subscription lifecycle.

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

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

  // Confirm that placing the initial order with no payment method doesn't
  // create the subscription.
  $initial_order
    ->getState()
    ->applyTransitionById('place');
  $initial_order
    ->save();
  $subscriptions = Subscription::loadMultiple();
  $this
    ->assertCount(0, $subscriptions);

  // Confirm that placing an order with a payment method creates an
  // active subscription.
  $initial_order
    ->set('state', 'draft');
  $initial_order
    ->set('payment_method', $this->paymentMethod);
  $initial_order
    ->save();
  $initial_order
    ->getState()
    ->applyTransitionById('place');
  $initial_order
    ->save();
  $subscriptions = Subscription::loadMultiple();
  $this
    ->assertCount(1, $subscriptions);

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = reset($subscriptions);
  $this
    ->assertEquals('active', $subscription
    ->getState()
    ->getId());
  $this
    ->assertEquals($this->store
    ->id(), $subscription
    ->getStoreId());
  $this
    ->assertEquals($this->billingSchedule
    ->id(), $subscription
    ->getBillingSchedule()
    ->id());
  $this
    ->assertEquals($this->user
    ->id(), $subscription
    ->getCustomerId());
  $this
    ->assertEquals($this->paymentMethod
    ->id(), $subscription
    ->getPaymentMethod()
    ->id());
  $this
    ->assertEquals($this->variation
    ->id(), $subscription
    ->getPurchasedEntityId());
  $this
    ->assertEquals($this->variation
    ->getOrderItemTitle(), $subscription
    ->getTitle());
  $this
    ->assertEquals('3', $subscription
    ->getQuantity());
  $this
    ->assertEquals($this->variation
    ->getPrice(), $subscription
    ->getUnitPrice());
  $this
    ->assertEquals($initial_order
    ->id(), $subscription
    ->getInitialOrderId());
  $orders = $subscription
    ->getOrders();
  $this
    ->assertCount(1, $orders);
  $order = reset($orders);
  $this
    ->assertFalse($order
    ->getTotalPrice()
    ->isZero());
  $this
    ->assertEquals('recurring', $order
    ->bundle());

  // Confirm that the recurring order has an order item for the subscription.
  $order_items = $order
    ->getItems();
  $this
    ->assertCount(1, $order_items);
  $order_item = reset($order_items);
  $this
    ->assertEquals($subscription
    ->id(), $order_item
    ->get('subscription')->target_id);

  // Test initial order cancellation.
  $initial_order
    ->getState()
    ->applyTransitionById('cancel');
  $initial_order
    ->save();
  $subscription = $this
    ->reloadEntity($subscription);
  $this
    ->assertEquals('canceled', $subscription
    ->getState()
    ->getId());
}