You are here

public function OrderReportTest::testOrderReport in Commerce Reporting 8

Tests order report entity.

File

tests/src/Kernel/ReportType/OrderReportTest.php, line 75

Class

OrderReportTest
Tests the `commerce_order_report` entity.

Namespace

Drupal\Tests\commerce_reports\Kernel\ReportType

Code

public function testOrderReport() {

  /** @var \Drupal\commerce_reports\Plugin\Commerce\ReportType\ReportTypeInterface $report_type_plugin */
  $report_type_plugin = $this->reportTypeManager
    ->createInstance('order_report');

  /** @var \Drupal\profile\Entity\Profile $profile */
  $profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'US',
      'postal_code' => '53177',
      'locality' => 'Milwaukee',
      'address_line1' => 'Pabst Blue Ribbon Dr',
      'administrative_area' => 'WI',
      'given_name' => 'Frederick',
      'family_name' => 'Pabst',
    ],
    'uid' => $this->user
      ->id(),
  ]);
  $profile
    ->save();

  /** @var \Drupal\commerce_order\Entity\Order $order */
  $order = Order::create([
    'order_id' => 1234,
    'type' => 'default',
    'state' => 'completed',
    'placed' => '1518491883',
    'mail' => $this->user
      ->getEmail(),
    'uid' => $this->user
      ->id(),
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'billing_profile' => $profile,
    'store_id' => $this->store
      ->id(),
  ]);
  $order
    ->get('total_price')
    ->setValue(new Price('100.00', 'USD'));
  $report_type_plugin
    ->generateReports($order);

  /** @var \Drupal\commerce_reports\Entity\OrderReport[] $order_reports */
  $order_reports = OrderReport::loadMultiple();
  $this
    ->assertEquals(1, count($order_reports));
  $order_report = reset($order_reports);
  $this
    ->assertEquals('1518491883', $order_report
    ->getCreatedTime());
  $this
    ->assertEquals(1234, $order_report
    ->getOrderId());
  $this
    ->assertFalse($order_report
    ->get('order_type_id')
    ->isEmpty());
  $this
    ->assertEquals($order
    ->bundle(), $order_report
    ->get('order_type_id')
    ->first()->target_id);
  $this
    ->assertFalse($order_report
    ->get('amount')
    ->isEmpty());
  $this
    ->assertEquals(new Price('100.00', 'USD'), $order_report
    ->get('amount')
    ->first()
    ->toPrice());
  $this
    ->assertFalse($order_report
    ->get('mail')
    ->isEmpty());
  $this
    ->assertEquals($this->user
    ->getEmail(), $order_report
    ->get('mail')
    ->first()->value);
  $this
    ->assertFalse($order_report
    ->get('billing_address')
    ->isEmpty());
  $this
    ->assertEquals($profile
    ->get('address')
    ->first()
    ->toArray(), $order_report
    ->get('billing_address')
    ->first()
    ->toArray());
}