public function OrderReportGeneratorTest::testRefreshReports in Commerce Reporting 8
Tests the refresh reports method.
File
- tests/
src/ Kernel/ OrderReportGeneratorTest.php, line 187
Class
- OrderReportGeneratorTest
- Test order report generator service.
Namespace
Drupal\Tests\commerce_reports\KernelCode
public function testRefreshReports() {
/** @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
->set('state', 'completed');
$order
->save();
/** @var \Drupal\commerce_reports\OrderReportGeneratorInterface $orderReportGenerator */
$orderReportGenerator = $this->container
->get('commerce_reports.order_report_generator');
// Verify order reports generated.
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
]);
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
$reports_created = count($order_reports);
/** @var \Drupal\commerce_reports\Entity\OrderReport $order_report */
foreach (OrderReport::loadMultiple() as $order_report) {
$this
->assertEquals($order_report
->getOrderId(), $order
->id());
}
// Change the order amount and verify that new order report is generated.
$order_item
->setQuantity(2);
$order_item
->save();
$order
->setItems([
$order_item,
]);
$order
->save();
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
]);
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
// Verify that previously generated order reports have been deleted.
for ($index = 1; $index <= $reports_created; $index++) {
$this
->assertEmpty(OrderReport::load($index));
}
// Create a second order.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order_2 */
$order_2 = 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_2 = $order_item_storage
->createFromPurchasableEntity($this->variation, [
'quantity' => 1,
]);
$order_item_2
->save();
$order_2
->addItem($order_item_2);
$order_2
->save();
// Only the placed order should be processed by the report generator.
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
$order_2
->id(),
]);
$this
->assertEquals($processed, 1);
// Now both orders should be processed.
$order_2
->set('state', 'completed');
$order_2
->save();
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
$order_2
->id(),
]);
$this
->assertEquals($processed, 2);
}