trait OrderTestTrait in Ubercart 8.4
Utility functions to provide orders for test purposes.
This trait can only be used in classes which already use RandomGeneratorTrait. RandomGeneratorTrait is used in all the PHPUnit and Simpletest base classes.
Hierarchy
- trait \Drupal\Tests\uc_order\Traits\OrderTestTrait uses ProductTestTrait
3 files declare their use of OrderTestTrait
- OrderRulesTestBase.php in uc_order/
tests/ src/ Functional/ OrderRulesTestBase.php - UbercartBrowserTestBase.php in uc_store/
tests/ src/ Functional/ UbercartBrowserTestBase.php - UbercartJavascriptTestBase.php in uc_store/
tests/ src/ FunctionalJavascript/ UbercartJavascriptTestBase.php
File
- uc_order/
tests/ src/ Traits/ OrderTestTrait.php, line 16
Namespace
Drupal\Tests\uc_order\TraitsView source
trait OrderTestTrait {
use ProductTestTrait;
/**
* Creates a new order directly, without going through checkout.
*
* @param array $edit
* (optional) An associative array of order fields to change from the
* defaults, keys are order field names. For example, 'price' => '12.34'.
*
* @return \Drupal\uc_order\OrderInterface
* The created Order entity.
*/
protected function createOrder(array $edit = []) {
if (empty($edit['primary_email'])) {
$edit['primary_email'] = $this
->randomMachineName(8) . '@example.org';
}
$order = Order::create($edit);
if (!isset($edit['products'])) {
$product = $this
->createProduct();
$order->products[] = OrderProduct::create([
'nid' => $product->nid->target_id,
'title' => $product->title->value,
'model' => $product->model,
'qty' => 1,
'cost' => $product->cost->value,
'price' => $product->price->value,
'weight' => $product->weight,
'data' => [],
]);
}
$order
->save();
return Order::load($order
->id());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderTestTrait:: |
protected | function | Creates a new order directly, without going through checkout. | |
ProductTestTrait:: |
protected | function | Creates a new product. | |
ProductTestTrait:: |
protected | function | Creates a new product node type, AKA 'product class'. |