You are here

public function CronTest::testTrial in Commerce Recurring Framework 8

Tests handling trial subscriptions and their orders.

File

tests/src/Kernel/CronTest.php, line 36

Class

CronTest
@coversDefaultClass \Drupal\commerce_recurring\Cron @group commerce_recurring

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testTrial() {
  $subscription = Subscription::create([
    'type' => 'product_variation',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'payment_method' => $this->paymentMethod,
    'purchased_entity' => $this->variation,
    'title' => $this->variation
      ->getOrderItemTitle(),
    'unit_price' => new Price('2', 'USD'),
    'state' => 'trial',
    'trial_starts' => strtotime('2019-02-05 17:00'),
    'trial_ends' => strtotime('2019-02-15 17:00'),
    'starts' => strtotime('2019-02-15 17:00'),
  ]);
  $subscription
    ->save();
  $order = $this->recurringOrderManager
    ->startTrial($subscription);

  // Confirm that no jobs were scheduled while the trial was still ongoing.
  $this
    ->rewindTime(strtotime('2019-02-10 17:00'));
  $this->container
    ->get('commerce_recurring.cron')
    ->run();

  /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
  $queue = Queue::load('commerce_recurring');
  $counts = array_filter($queue
    ->getBackend()
    ->countJobs());
  $this
    ->assertEmpty($counts);

  // Confirm that the trial order was scheduled for closing (but not renewal),
  // and that the subscription was scheduled for activation.
  $this
    ->rewindTime(strtotime('2019-02-15 17:00'));
  $this->container
    ->get('commerce_recurring.cron')
    ->run();
  $this->container
    ->get('entity_type.manager')
    ->getStorage('advancedqueue_queue')
    ->resetCache();

  /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
  $queue = Queue::load('commerce_recurring');
  $counts = array_filter($queue
    ->getBackend()
    ->countJobs());
  $this
    ->assertEquals([
    Job::STATE_QUEUED => 2,
  ], $counts);
  $first_job = $queue
    ->getBackend()
    ->claimJob();
  $this
    ->assertSame([
    'order_id' => $order
      ->id(),
  ], $first_job
    ->getPayload());
  $this
    ->assertEquals('commerce_recurring_order_close', $first_job
    ->getType());
  $second_job = $queue
    ->getBackend()
    ->claimJob();
  $this
    ->assertSame([
    'subscription_id' => $subscription
      ->id(),
  ], $second_job
    ->getPayload());
  $this
    ->assertEquals('commerce_subscription_activate', $second_job
    ->getType());
}