You are here

public function CronTest::testActive in Commerce Recurring Framework 8

Tests handling active subscriptions and their orders.

File

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

Class

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

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testActive() {
  $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' => 'active',
    'starts' => strtotime('2019-02-15 00:00'),
  ]);
  $subscription
    ->save();
  $order = $this->recurringOrderManager
    ->startRecurring($subscription);

  // Confirm that no jobs were scheduled before the end of the billing period.
  $this
    ->rewindTime(strtotime('2019-02-24 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 order was scheduled for closing and renewal.
  $this
    ->rewindTime(strtotime('2019-03-01 00: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([
    'order_id' => $order
      ->id(),
  ], $second_job
    ->getPayload());
  $this
    ->assertEquals('commerce_recurring_order_renew', $second_job
    ->getType());
}