You are here

public function FixedTest::testCustomStart in Commerce Recurring Framework 8

@covers ::generateFirstBillingPeriod

File

tests/src/Kernel/Plugin/Commerce/BillingSchedule/FixedTest.php, line 78

Class

FixedTest
Tests the fixed billing schedule.

Namespace

Drupal\Tests\commerce_recurring\Kernel\Plugin\Commerce\BillingSchedule

Code

public function testCustomStart() {

  // Hourly intervals should be unaffected.
  $plugin = new Fixed([
    'interval' => [
      'number' => '2',
      'unit' => 'hour',
    ],
    'start_month' => '2',
    'start_day' => '3',
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 10:00:00'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 12:00:00'), $billing_period
    ->getEndDate());

  // Monthly intervals should ignore the start_month.
  $plugin = new Fixed([
    'interval' => [
      'number' => '1',
      'unit' => 'month',
    ],
    'start_month' => '2',
    'start_day' => '3',
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-03 00:00:00'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-04-03 00:00:00'), $billing_period
    ->getEndDate());

  // Subscription started before start_day.
  $plugin = new Fixed([
    'interval' => [
      'number' => '1',
      'unit' => 'month',
    ],
    'start_month' => '2',
    'start_day' => '3',
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-02 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-02-03 00:00:00'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-03 00:00:00'), $billing_period
    ->getEndDate());
  $plugin = new Fixed([
    'interval' => [
      'number' => '2',
      'unit' => 'year',
    ],
    'start_month' => '2',
    'start_day' => '3',
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-02-03 00:00:00'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2019-02-03 00:00:00'), $billing_period
    ->getEndDate());

  // Subscription started before start_day/start_month.
  $plugin = new Fixed([
    'interval' => [
      'number' => '2',
      'unit' => 'year',
    ],
    'start_month' => '2',
    'start_day' => '3',
  ], '', []);
  $start_date = new DrupalDateTime('2017-01-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2015-02-03 00:00:00'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-02-03 00:00:00'), $billing_period
    ->getEndDate());
}