You are here

public function OrderReportGeneratorTest::testGenerateSinglePluginReports in Commerce Reporting 8

Tests the generate reports method for a single plugin type.

File

tests/src/Kernel/OrderReportGeneratorTest.php, line 273

Class

OrderReportGeneratorTest
Test order report generator service.

Namespace

Drupal\Tests\commerce_reports\Kernel

Code

public function testGenerateSinglePluginReports() {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = Order::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'cart' => TRUE,
    'mail' => $this
      ->randomString() . '@example.com',
    'uid' => User::getAnonymousUser(),
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'billing_profile' => $this->profile,
  ]);
  $order_item_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_order_item');
  $order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variation, [
    'quantity' => 1,
  ]);
  $order_item
    ->save();
  $order
    ->addItem($order_item);
  $order
    ->save();

  // Set the state to 'completed' for the order.
  $order
    ->set('state', 'completed');
  $order
    ->save();

  /** @var \Drupal\commerce_reports\OrderReportGeneratorInterface $orderReportGenerator */
  $orderReportGenerator = $this->container
    ->get('commerce_reports.order_report_generator');

  // Verify that no reports are created for an invalid plugin type.
  $processed = $orderReportGenerator
    ->generateReports([
    $order
      ->id(),
  ], 'none');
  $this
    ->assertEquals($processed, 0);
  $order_reports = OrderReport::loadMultiple();
  $this
    ->assertEmpty($order_reports);

  // Verify that only order item reports are generated.
  $processed = $orderReportGenerator
    ->generateReports([
    $order
      ->id(),
  ], 'order_items_report');
  $this
    ->assertEquals($processed, 1);
  $order_reports = OrderReport::loadMultiple();
  $this
    ->assertNotEmpty($order_reports);
  foreach ($order_reports as $order_report) {
    $this
      ->assertEquals('order_items_report', $order_report
      ->bundle());
  }
}