You are here

public function StandaloneTest::testCharges in Commerce Recurring Framework 8

@covers ::collectCharges

File

tests/src/Kernel/Plugin/Commerce/SubscriptionType/StandaloneTest.php, line 35

Class

StandaloneTest
Tests the standalone subscription type.

Namespace

Drupal\Tests\commerce_recurring\Kernel\Plugin\Commerce\SubscriptionType

Code

public function testCharges() {

  // Confirms that the Standalone plugin outputs the correct charge.
  // The full prepaid/postpaid logic is tested in ProductVariationTest.
  $subscription = Subscription::create([
    'type' => 'standalone',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'title' => 'My donation',
    'quantity' => 1,
    'unit_price' => new Price('19', 'USD'),
    'starts' => strtotime('2019-02-24 17:00:00'),
  ]);
  $subscription
    ->save();
  $start_date = $subscription
    ->getStartDate();
  $billing_period = $this->billingSchedule
    ->getPlugin()
    ->generateFirstBillingPeriod($start_date);

  // The billing schedule is fixed, the first period starts before the charge.
  $expected_billing_period = new BillingPeriod($subscription
    ->getStartDate(), $billing_period
    ->getEndDate());
  $charges = $subscription
    ->getType()
    ->collectCharges($subscription, $billing_period);
  $this
    ->assertCount(1, $charges);
  $base_charge = reset($charges);
  $this
    ->assertInstanceOf(Charge::class, $base_charge);
  $this
    ->assertNull($base_charge
    ->getPurchasedEntity());
  $this
    ->assertEquals($subscription
    ->getTitle(), $base_charge
    ->getTitle());
  $this
    ->assertEquals($subscription
    ->getQuantity(), $base_charge
    ->getQuantity());
  $this
    ->assertEquals($subscription
    ->getUnitPrice(), $base_charge
    ->getUnitPrice());
  $this
    ->assertEquals($expected_billing_period, $base_charge
    ->getBillingPeriod());
}