You are here

trait AttributeTestTrait in Ubercart 8.4

Utility functions to provide products 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

2 files declare their use of AttributeTestTrait
UbercartBrowserTestBase.php in uc_store/tests/src/Functional/UbercartBrowserTestBase.php
UbercartJavascriptTestBase.php in uc_store/tests/src/FunctionalJavascript/UbercartJavascriptTestBase.php

File

uc_attribute/tests/src/Traits/AttributeTestTrait.php, line 12

Namespace

Drupal\Tests\uc_attribute\Traits
View source
trait AttributeTestTrait {

  /**
   * Creates an attribute.
   *
   * @param array $data
   *   (optional) An associative array of attribute initialization data.
   * @param bool $save
   *   If TRUE, save attribute in database.
   *
   * @return array
   *   Associative array of attribute data.
   */
  protected function createAttribute(array $data = [], $save = TRUE) {
    $attribute = $data + [
      'name' => $this
        ->randomMachineName(8),
      'label' => $this
        ->randomMachineName(8),
      'description' => $this
        ->randomMachineName(8),
      'required' => mt_rand(0, 1) ? TRUE : FALSE,
      'display' => mt_rand(0, 3),
      'ordering' => mt_rand(-10, 10),
    ];
    $attribute = (object) $attribute;
    if ($save) {
      uc_attribute_save($attribute);
    }
    return $attribute;
  }

  /**
   * Creates an attribute option.
   *
   * @param array $data
   *   Array containing attribute data, with keys corresponding to the
   *   columns of the {uc_attribute} table.
   * @param bool $save
   *   If TRUE, save attribute option in database.
   *
   * @return array
   *   Associative array of attribute option data.
   */
  protected function createAttributeOption(array $data = [], $save = TRUE) {
    $max_aid = \Drupal::database()
      ->select('uc_attributes', 'a')
      ->fields('a', [
      'aid',
    ])
      ->orderBy('aid', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchField();
    $option = $data + [
      'aid' => $max_aid,
      'name' => $this
        ->randomMachineName(8),
      'cost' => mt_rand(-500, 500),
      'price' => mt_rand(-500, 500),
      'weight' => mt_rand(-500, 500),
      'ordering' => mt_rand(-10, 10),
    ];
    $option = (object) $option;
    if ($save) {
      uc_attribute_option_save($option);
    }
    return $option;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AttributeTestTrait::createAttribute protected function Creates an attribute.
AttributeTestTrait::createAttributeOption protected function Creates an attribute option.