You are here

public function BillingPeriodItemTest::testField in Commerce Recurring Framework 8

Tests the field.

File

tests/src/Kernel/BillingPeriodItemTest.php, line 58

Class

BillingPeriodItemTest
Tests the billing period field type.

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testField() {
  $start_date = new DrupalDateTime('2019-10-19 15:07:12');
  $end_date = new DrupalDateTime('2019-11-19 15:07:12');
  $entity = EntityTest::create([
    'field_billing_period' => [
      'starts' => $start_date
        ->format('U'),
      'ends' => $end_date
        ->format('U'),
    ],
  ]);
  $entity
    ->save();
  $entity = EntityTest::load($entity
    ->id());

  /** @var \Drupal\commerce_recurring\Plugin\Field\FieldType\BillingPeriodItem $billing_period_field */
  $billing_period_field = $entity
    ->get('field_billing_period')
    ->first();
  $this
    ->assertEquals($start_date
    ->format('U'), $billing_period_field->starts);
  $this
    ->assertEquals($end_date
    ->format('U'), $billing_period_field->ends);
  $billing_period = $billing_period_field
    ->toBillingPeriod();
  $this
    ->assertInstanceOf(BillingPeriod::class, $billing_period);
  $this
    ->assertEquals($start_date, $billing_period
    ->getStartDate());
  $this
    ->assertEquals($end_date, $billing_period
    ->getEndDate());

  // Test passing billing periods.
  $new_end_date = new DrupalDateTime('2019-12-19 15:07:12');
  $billing_period = new BillingPeriod($end_date, $new_end_date);
  $entity
    ->set('field_billing_period', $billing_period);

  /** @var \Drupal\commerce_recurring\Plugin\Field\FieldType\BillingPeriodItem $billing_period_field */
  $billing_period_field = $entity
    ->get('field_billing_period')
    ->first();
  $returned_billing_period = $billing_period_field
    ->toBillingPeriod();
  $this
    ->assertEquals($billing_period, $returned_billing_period);
}