View source
<?php
namespace Drupal\Tests\commerce_recurring\Kernel\Plugin\Commerce\BillingSchedule;
use Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule\Fixed;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\KernelTestBase;
class FixedTest extends KernelTestBase {
public static $modules = [
'commerce',
'commerce_price',
'commerce_order',
'commerce_recurring',
];
public function testGenerate() {
$plugin = new Fixed([
'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:00:00'), $billing_period
->getStartDate());
$this
->assertEquals(new DrupalDateTime('2017-03-16 12:00:00'), $billing_period
->getEndDate());
$next_billing_period = $plugin
->generateNextBillingPeriod($start_date, $billing_period);
$this
->assertEquals(new DrupalDateTime('2017-03-16 12:00:00'), $next_billing_period
->getStartDate());
$this
->assertEquals(new DrupalDateTime('2017-03-16 14:00:00'), $next_billing_period
->getEndDate());
$plugin = new Fixed([
'interval' => [
'number' => '1',
'unit' => 'month',
],
], '', []);
$start_date = new DrupalDateTime('2017-03-16 10:22:30');
$billing_period = $plugin
->generateFirstBillingPeriod($start_date);
$this
->assertEquals(new DrupalDateTime('2017-03-01 00:00:00'), $billing_period
->getStartDate());
$this
->assertEquals(new DrupalDateTime('2017-04-01 00:00:00'), $billing_period
->getEndDate());
$next_billing_period = $plugin
->generateNextBillingPeriod($start_date, $billing_period);
$this
->assertEquals(new DrupalDateTime('2017-04-01 00:00:00'), $next_billing_period
->getStartDate());
$this
->assertEquals(new DrupalDateTime('2017-05-01 00:00:00'), $next_billing_period
->getEndDate());
$plugin = new Fixed([
'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-01-01 00:00:00'), $billing_period
->getStartDate());
$this
->assertEquals(new DrupalDateTime('2018-01-01 00:00:00'), $billing_period
->getEndDate());
$next_billing_period = $plugin
->generateNextBillingPeriod($start_date, $billing_period);
$this
->assertEquals(new DrupalDateTime('2018-01-01 00:00:00'), $next_billing_period
->getStartDate());
$this
->assertEquals(new DrupalDateTime('2019-01-01 00:00:00'), $next_billing_period
->getEndDate());
}
public function testCustomStart() {
$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());
$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());
$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());
$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());
}
}