You are here

public function RollingTest::testGenerate in Commerce Recurring Framework 8

@covers ::generateFirstBillingPeriod @covers ::generateNextBillingPeriod

File

tests/src/Kernel/Plugin/Commerce/BillingSchedule/RollingTest.php, line 31

Class

RollingTest
Tests the rolling billing schedule.

Namespace

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

Code

public function testGenerate() {
  $plugin = new Rolling([
    'interval' => [
      'number' => '2',
      'unit' => 'hour',
    ],
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 10:22:30'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 12:22:30'), $billing_period
    ->getEndDate());
  $next_billing_period = $plugin
    ->generateNextBillingPeriod($start_date, $billing_period);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 12:22:30'), $next_billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 14:22:30'), $next_billing_period
    ->getEndDate());
  $plugin = new Rolling([
    'interval' => [
      'number' => '1',
      'unit' => 'week',
    ],
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 10:22:30'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-23 10:22:30'), $billing_period
    ->getEndDate());
  $next_billing_period = $plugin
    ->generateNextBillingPeriod($start_date, $billing_period);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-23 10:22:30'), $next_billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-30 10:22:30'), $next_billing_period
    ->getEndDate());
  $plugin = new Rolling([
    'interval' => [
      'number' => '1',
      'unit' => 'month',
    ],
  ], '', []);
  $start_date = new DrupalDateTime('2017-01-30 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-01-30 10:22:30'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-02-28 10:22:30'), $billing_period
    ->getEndDate());
  $next_billing_period = $plugin
    ->generateNextBillingPeriod($start_date, $billing_period);
  $this
    ->assertEquals(new DrupalDateTime('2017-02-28 10:22:30'), $next_billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2017-03-30 10:22:30'), $next_billing_period
    ->getEndDate());
  $plugin = new Rolling([
    'interval' => [
      'number' => '1',
      'unit' => 'year',
    ],
  ], '', []);
  $start_date = new DrupalDateTime('2017-03-16 10:22:30');
  $billing_period = $plugin
    ->generateFirstBillingPeriod($start_date);
  $this
    ->assertEquals(new DrupalDateTime('2017-03-16 10:22:30'), $billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2018-03-16 10:22:30'), $billing_period
    ->getEndDate());
  $next_billing_period = $plugin
    ->generateNextBillingPeriod($start_date, $billing_period);
  $this
    ->assertEquals(new DrupalDateTime('2018-03-16 10:22:30'), $next_billing_period
    ->getStartDate());
  $this
    ->assertEquals(new DrupalDateTime('2019-03-16 10:22:30'), $next_billing_period
    ->getEndDate());
}