You are here

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

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\Traits
View 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

Namesort descending Modifiers Type Description Overrides
OrderTestTrait::createOrder protected function Creates a new order directly, without going through checkout.
ProductTestTrait::createProduct protected function Creates a new product.
ProductTestTrait::createProductClass protected function Creates a new product node type, AKA 'product class'.