You are here

public function RecurringOrderManagerTest::testRenewOrder in Commerce Recurring Framework 8

@covers ::renewOrder

File

tests/src/Kernel/RecurringOrderManagerTest.php, line 396

Class

RecurringOrderManagerTest
@coversDefaultClass \Drupal\commerce_recurring\RecurringOrderManager @group commerce_recurring

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testRenewOrder() {
  $order = $this->recurringOrderManager
    ->startRecurring($this->activeSubscription);
  $next_order = $this->recurringOrderManager
    ->renewOrder($order);

  /** @var \Drupal\commerce_recurring\Plugin\Field\FieldType\BillingPeriodItem $billing_period_item */
  $billing_period_item = $order
    ->get('billing_period')
    ->first();
  $billing_period = $billing_period_item
    ->toBillingPeriod();

  /** @var \Drupal\commerce_recurring\Plugin\Field\FieldType\BillingPeriodItem $next_billing_period_item */
  $next_billing_period_item = $next_order
    ->get('billing_period')
    ->first();
  $next_billing_period = $next_billing_period_item
    ->toBillingPeriod();
  $this->activeSubscription = $this
    ->reloadEntity($this->activeSubscription);
  $this
    ->assertTrue($this->activeSubscription
    ->hasOrder($order));
  $this
    ->assertTrue($this->activeSubscription
    ->hasOrder($next_order));
  $this
    ->assertNotEmpty($this->activeSubscription
    ->getRenewedTime());
  $this
    ->assertEquals($next_billing_period
    ->getEndDate()
    ->getTimestamp(), $this->activeSubscription
    ->getNextRenewalTime());
  $this
    ->assertEquals($billing_period
    ->getEndDate(), $next_billing_period
    ->getStartDate());
  $this
    ->assertOrder($next_order, $this->activeSubscription);
  $this
    ->assertTrue($next_order
    ->hasItems());
  $order_items = $next_order
    ->getItems();
  $order_item = reset($order_items);
  $this
    ->assertEquals('recurring_product_variation', $order_item
    ->bundle());
  $this
    ->assertEquals($this->activeSubscription
    ->getTitle(), $order_item
    ->getTitle());
  $this
    ->assertEquals($this->activeSubscription
    ->getQuantity(), $order_item
    ->getQuantity());
  $this
    ->assertEquals($this->activeSubscription
    ->getUnitPrice(), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals($this->variation, $order_item
    ->getPurchasedEntity());
  $this
    ->assertEquals($next_billing_period, $order_item
    ->get('billing_period')
    ->first()
    ->toBillingPeriod());

  // Confirm that the next billing period (March) is 31 days long.
  $this
    ->assertEquals(2678400, $next_billing_period
    ->getDuration());
  $this
    ->assertEquals($this->activeSubscription
    ->id(), $order_item
    ->get('subscription')->target_id);

  // Confirm that no renewal occurs for canceled subscriptions.
  $this->activeSubscription
    ->cancel(FALSE)
    ->save();
  $result = $this->recurringOrderManager
    ->renewOrder($next_order);
  $this
    ->assertNull($result);
}