You are here

public function RecurringOrderManagerTest::testStartRecurringPostpaid in Commerce Recurring Framework 8

@covers ::startRecurring @covers ::collectSubscriptions

File

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

Class

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

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testStartRecurringPostpaid() {
  $order = $this->recurringOrderManager
    ->startRecurring($this->activeSubscription);
  $expected_billing_period = new BillingPeriod(new DrupalDateTime('2019-02-01 00:00:00'), new DrupalDateTime('2019-03-01 00:00'));

  /** @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();
  $this
    ->assertEquals($expected_billing_period, $billing_period);
  $this
    ->assertEquals($billing_period
    ->getEndDate()
    ->getTimestamp(), $this->activeSubscription
    ->getNextRenewalTime());

  // Confirm that the current billing period is 28 days long.
  $this
    ->assertEquals(2419200, $billing_period
    ->getDuration());
  $this
    ->assertTrue($this->activeSubscription
    ->hasOrder($order));
  $this
    ->assertEmpty($this->activeSubscription
    ->getRenewedTime());
  $this
    ->assertOrder($order, $this->activeSubscription);
  $this
    ->assertTrue($order
    ->hasItems());
  $order_items = $order
    ->getItems();
  $order_item = reset($order_items);

  /** @var \Drupal\commerce_recurring\BillingPeriod $order_item_billing_period */

  /** @var \Drupal\commerce_recurring\Plugin\Field\FieldType\BillingPeriodItem $order_item_billing_period_item */
  $order_item_billing_period_item = $order_item
    ->get('billing_period')
    ->first();
  $order_item_billing_period = $order_item_billing_period_item
    ->toBillingPeriod();
  $expected_order_item_billing_period = new BillingPeriod(new DrupalDateTime('2019-02-15 00:00:00'), new DrupalDateTime('2019-03-01 00:00'));
  $this
    ->assertEquals('recurring_product_variation', $order_item
    ->bundle());
  $this
    ->assertEquals($this->activeSubscription
    ->id(), $order_item
    ->get('subscription')->target_id);
  $this
    ->assertEquals($this->activeSubscription
    ->getTitle(), $order_item
    ->getTitle());
  $this
    ->assertEquals($this->activeSubscription
    ->getQuantity(), $order_item
    ->getQuantity());
  $this
    ->assertEquals($this->activeSubscription
    ->getPurchasedEntityId(), $order_item
    ->getPurchasedEntityId());
  $this
    ->assertEquals($expected_order_item_billing_period, $order_item_billing_period);

  // The subscription was created mid-cycle, the unit price should be
  // half the usual due to proration.
  $this
    ->assertEquals($this->activeSubscription
    ->getUnitPrice()
    ->divide('2'), $order_item
    ->getUnitPrice());
  $this
    ->assertEquals($this->activeSubscription
    ->getStartDate(), $order_item_billing_period
    ->getStartDate());
  $this
    ->assertEquals($billing_period
    ->getEndDate(), $order_item_billing_period
    ->getEndDate());
}