You are here

public function SubscriptionLifecycleTest::testSubscriptionDelete in Commerce Recurring Framework 8

Tests the subscription deletion.

Deleting the subscription will also delete its recurring orders.

File

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

Class

SubscriptionLifecycleTest
Tests the subscription lifecycle.

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testSubscriptionDelete() {
  $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);

  // Confirm that a recurring order was created for the subscription.
  $orders = $subscription
    ->getOrders();
  $this
    ->assertCount(1, $orders);
  $order = reset($orders);
  $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 deleting the subscription.
  $subscription
    ->delete();
  $subscription = $this
    ->reloadEntity($subscription);
  $order_item = $this
    ->reloadEntity($order_item);
  $order = $this
    ->reloadEntity($order);
  $this
    ->assertNull($subscription);
  $this
    ->assertNull($order_item);
  $this
    ->assertNull($order);
}