You are here

public function RetryTest::testFailure in Commerce Recurring Framework 8

@covers ::process @covers ::handleDecline @covers ::updateSubscriptions

File

tests/src/Kernel/Plugin/AdvancedQueue/JobType/RetryTest.php, line 174

Class

RetryTest
@coversDefaultClass \Drupal\commerce_recurring\Plugin\AdvancedQueue\JobType\RecurringJobTypeBase @group commerce_recurring

Namespace

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

Code

public function testFailure() {
  $payment_method = ExceptionPaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => 'example',
    'card_type' => 'visa',
    'card_number' => '1111',
  ]);
  $payment_method
    ->save();
  $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'),
    'payment_method' => $payment_method,
  ]);
  $subscription
    ->save();
  $order = $this->recurringOrderManager
    ->startRecurring($subscription);

  // Rewind time to the end of the first subscription.
  $this
    ->rewindTime(strtotime('2019-03-01 00:00'));
  $job = Job::create('commerce_recurring_order_close', [
    'order_id' => $order
      ->id(),
  ]);
  $this->queue
    ->enqueueJob($job);

  // Tell the payment method entity class to throw an exception.
  // This will be caught by RecurringOrderClose as with a DeclineException,
  // but re-thrown, and then caught by processJob().
  \Drupal::state()
    ->set('commerce_recurring_test.payment_method_throw', TRUE);
  $job = $this->queue
    ->getBackend()
    ->claimJob();

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

  // Confirm that the order was marked as failed.
  $order = $this
    ->reloadEntity($order);
  $this
    ->assertEquals('failed', $order
    ->getState()
    ->getId());

  // Confirm that the job result is correct.
  $this
    ->assertEquals(Job::STATE_FAILURE, $job
    ->getState());
  $this
    ->assertEquals('This payment is failing dramatically!', $result
    ->getMessage());

  // Confirm that the job was not requeued.
  $this
    ->assertEquals(0, $job
    ->getNumRetries());
  $counts = array_filter($this->queue
    ->getBackend()
    ->countJobs());
  $this
    ->assertEquals([
    Job::STATE_FAILURE => 1,
  ], $counts);

  // Confirm that the subscription was canceled.
  $subscription = $this
    ->reloadEntity($subscription);
  $this
    ->assertEquals('canceled', $subscription
    ->getState()
    ->getId());
}