You are here

public function SubscriptionActivateTest::testActivate in Commerce Recurring Framework 8

@covers ::process

File

tests/src/Kernel/Plugin/AdvancedQueue/JobType/SubscriptionActivateTest.php, line 45

Class

SubscriptionActivateTest
@coversDefaultClass \Drupal\commerce_recurring\Plugin\AdvancedQueue\JobType\SubscriptionActivate @group commerce_recurring

Namespace

Drupal\Tests\commerce_recurring\Kernel\Plugin\AdvancedQueue\JobType

Code

public function testActivate() {

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = Subscription::create([
    'type' => 'product_variation',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'purchased_entity' => $this->variation,
    'title' => $this->variation
      ->getOrderItemTitle(),
    'unit_price' => new Price('2', 'USD'),
    'state' => 'active',
    'starts' => strtotime('2019-02-24 17:00'),
  ]);
  $subscription
    ->save();

  // Confirm that it is not possible to active an already-active subscription.
  $job = Job::create('commerce_subscription_activate', [
    'subscription_id' => $subscription
      ->id(),
  ]);
  $this->queue
    ->enqueueJob($job);
  $job = $this->queue
    ->getBackend()
    ->claimJob();

  /** @var \Drupal\advancedqueue\ProcessorInterface $processor */
  $processor = \Drupal::service('advancedqueue.processor');
  $result = $processor
    ->processJob($job, $this->queue);
  $this
    ->assertEquals(Job::STATE_FAILURE, $result
    ->getState());

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = Subscription::create([
    'type' => 'product_variation',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'purchased_entity' => $this->variation,
    'title' => $this->variation
      ->getOrderItemTitle(),
    'unit_price' => new Price('2', 'USD'),
    'state' => 'pending',
    'starts' => strtotime('2019-02-01 00:00'),
  ]);
  $subscription
    ->save();

  // Confirm that it is possible to activate a pending subscription.
  $job = Job::create('commerce_subscription_activate', [
    'subscription_id' => $subscription
      ->id(),
  ]);
  $this->queue
    ->enqueueJob($job);
  $job = $this->queue
    ->getBackend()
    ->claimJob();
  $result = $processor
    ->processJob($job, $this->queue);
  $this
    ->assertEquals(Job::STATE_SUCCESS, $result
    ->getState());
  $subscription = $this
    ->reloadEntity($subscription);
  $this
    ->assertEquals('active', $subscription
    ->getState()
    ->getId());
  $this
    ->assertCount(1, $subscription
    ->getOrders());
  $order = $subscription
    ->getOrders()[0];
  $this
    ->assertEquals(new Price('2', 'USD'), $order
    ->getTotalPrice());

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = Subscription::create([
    'type' => 'product_variation',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'purchased_entity' => $this->variation,
    'title' => $this->variation
      ->getOrderItemTitle(),
    'unit_price' => new Price('3', 'USD'),
    'state' => 'trial',
    'trial_starts' => strtotime('2019-02-01 00:00'),
    'trial_ends' => strtotime('2019-02-10 00:00'),
    'starts' => strtotime('2019-02-10 00:00'),
  ]);
  $subscription
    ->save();
  $this->recurringOrderManager
    ->startTrial($subscription);

  // Confirm that it is possible to activate a trial subscription.
  $job = Job::create('commerce_subscription_activate', [
    'subscription_id' => $subscription
      ->id(),
  ]);
  $this->queue
    ->enqueueJob($job);
  $job = $this->queue
    ->getBackend()
    ->claimJob();

  /** @var \Drupal\advancedqueue\ProcessorInterface $processor */
  $result = $processor
    ->processJob($job, $this->queue);
  $this
    ->assertEquals(Job::STATE_SUCCESS, $result
    ->getState());
  $subscription = $this
    ->reloadEntity($subscription);
  $this
    ->assertEquals('active', $subscription
    ->getState()
    ->getId());

  // One order for the trial period, one for the first billing period.
  $this
    ->assertCount(2, $subscription
    ->getOrders());
  $order = $subscription
    ->getOrders()[1];

  // Prorated price for the Feb 10th - Mar 1st period.
  $this
    ->assertEquals(new Price('2.04', 'USD'), $order
    ->getTotalPrice());
}