You are here

trait CommerceBrowserTestTrait in Commerce Core 8.2

Provides a trait for Commerce functional tests.

Hierarchy

2 files declare their use of CommerceBrowserTestTrait
CommerceBrowserTestBase.php in tests/src/Functional/CommerceBrowserTestBase.php
CommerceWebDriverTestBase.php in tests/src/FunctionalJavascript/CommerceWebDriverTestBase.php

File

tests/src/Traits/CommerceBrowserTestTrait.php, line 11

Namespace

Drupal\Tests\commerce\Traits
View source
trait CommerceBrowserTestTrait {

  /**
   * Creates a new entity.
   *
   * @param string $entity_type
   *   The entity type to be created.
   * @param array $values
   *   An array of settings.
   *   Example: 'id' => 'foo'.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   A new entity.
   */
  protected function createEntity($entity_type, array $values) {

    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container
      ->get('entity_type.manager')
      ->getStorage($entity_type);
    $entity = $storage
      ->create($values);
    $status = $entity
      ->save();
    $this
      ->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created %label entity %type.', [
      '%label' => $entity
        ->getEntityType()
        ->getLabel(),
      '%type' => $entity
        ->id(),
    ]));

    // The newly saved entity isn't identical to a loaded one, and would fail
    // comparisons.
    $entity = $storage
      ->load($entity
      ->id());
    return $entity;
  }

  /**
   * Reloads the entity after clearing the static cache.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to reload.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The reloaded entity.
   */
  protected function reloadEntity(EntityInterface $entity) {

    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container
      ->get('entity_type.manager')
      ->getStorage($entity
      ->getEntityTypeId());
    $storage
      ->resetCache([
      $entity
        ->id(),
    ]);
    return $storage
      ->load($entity
      ->id());
  }

  /**
   * Debugger method to save additional HTML output.
   *
   * The base class will only save browser output when accessing page using
   * ::drupalGet and providing a printer class to PHPUnit. This method
   * is intended for developers to help debug browser test failures and capture
   * more verbose output.
   */
  protected function saveHtmlOutput() {
    $out = $this
      ->getSession()
      ->getPage()
      ->getContent();

    // Ensure that any changes to variables in the other thread are picked up.
    $this
      ->refreshVariables();
    if ($this->htmlOutputEnabled) {
      $html_output = '<hr />Ending URL: ' . $this
        ->getSession()
        ->getCurrentUrl();
      $html_output .= '<hr />' . $out;
      $html_output .= $this
        ->getHtmlOutputHeaders();
      $this
        ->htmlOutput($html_output);
    }
  }

  /**
   * Asserts that the passed field values are correct.
   *
   * Ignores differences in ordering.
   *
   * @param array $field_values
   *   The field values.
   * @param array $expected_values
   *   The expected values.
   * @param string $message
   *   (optional) A message to display with the assertion. Do not translate
   *   messages:
   *   use \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
   *   to embed variables in the message text, not t().
   *   If left blank, a default message will be displayed.
   */
  protected function assertFieldValues(array $field_values, array $expected_values, $message = '') {
    $valid = TRUE;
    if (count($field_values) == count($expected_values)) {
      foreach ($expected_values as $value) {
        if (!in_array($value, $field_values)) {
          $valid = FALSE;
          break;
        }
      }
    }
    else {
      $valid = FALSE;
    }
    $this
      ->assertNotEmpty($valid, $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceBrowserTestTrait::assertFieldValues protected function Asserts that the passed field values are correct.
CommerceBrowserTestTrait::createEntity protected function Creates a new entity.
CommerceBrowserTestTrait::reloadEntity protected function Reloads the entity after clearing the static cache.
CommerceBrowserTestTrait::saveHtmlOutput protected function Debugger method to save additional HTML output.