public function SubscriptionLifecycleTest::testLifecycleWithTrial in Commerce Recurring Framework 8
Tests the subscription lifecycle, with a free trial.
Placing an initial order should create a trial subscription. Canceling the initial order should cancel the trial.
File
- tests/
src/ Kernel/ SubscriptionLifecycleTest.php, line 91
Class
- SubscriptionLifecycleTest
- Tests the subscription lifecycle.
Namespace
Drupal\Tests\commerce_recurring\KernelCode
public function testLifecycleWithTrial() {
// Rewind the time so that the trial duration is not affected by daylight
// savings.
// If the daylight savings occur during the trial, then the trial duration
// could be 1hour less/more than expected, so rewinding the time helps us
// ensuring the trial is exactly 10 days.
$this
->rewindTime(strtotime('2021-01-01 00:00'));
$initial_order = $this
->createInitialOrder(TRUE);
// Confirm that placing the initial order creates a trial subscription,
// even without a payment method.
$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('trial', $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
->assertNull($subscription
->getPaymentMethod());
$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());
$this
->assertNotEmpty($subscription
->getTrialStartTime());
$this
->assertNotEmpty($subscription
->getTrialEndTime());
$this
->assertEquals(864000, $subscription
->getTrialEndTime() - $subscription
->getTrialStartTime());
$orders = $subscription
->getOrders();
$this
->assertCount(1, $orders);
$order = reset($orders);
$this
->assertEquals('recurring', $order
->bundle());
$this
->assertTrue($order
->getTotalPrice()
->isZero());
// 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());
}