You are here

class MockConfig in Configuration Update Manager 8

Mock class for mutable configuration, config entity, and entity storage.

Hierarchy

  • class \Drupal\Tests\config_update\Unit\MockConfig

Expanded class hierarchy of MockConfig

File

tests/src/Unit/ConfigUpdateUnitTestBase.php, line 403

Namespace

Drupal\Tests\config_update\Unit
View source
class MockConfig {

  /**
   * Name of the config.
   *
   * @var string
   */
  protected $name = '';

  /**
   * Prefix for the entity type being mocked, for entity storage mocking.
   *
   * @var string
   */
  protected $entityPrefix = '';

  /**
   * Test class this comes from.
   *
   * @var \Drupal\Tests\config_update\Unit\ConfigUpdateUnitTestBase
   */
  protected $test;

  /**
   * Current value of the configuration.
   *
   * @var array
   */
  protected $value = '';

  /**
   * Constructs a mock config object.
   *
   * @param string $name
   *   Name of the config that is being mocked. Can be blank.
   * @param string $entity_prefix
   *   Prefix for the entity type that is being mocked. Often blank.
   * @param \Drupal\Tests\config_update\Unit\ConfigUpdateUnitTestBase $test
   *   Test class this comes from.
   */
  public function __construct($name, $entity_prefix, ConfigUpdateUnitTestBase $test) {
    $this->name = $name;
    $this->entityPrefix = $entity_prefix;
    $this->test = $test;
    $storage = $test
      ->getConfigStorage();
    if ($name && isset($storage[$name])) {
      $value = $storage[$name];
      $value['is_new'] = FALSE;
    }
    else {
      $value['is_new'] = TRUE;
    }
    $value['_core'] = 'core_for_' . $name;
    $this->value = $value;
  }

  /**
   * Gets a component of the configuration value.
   */
  public function get($key) {
    return isset($this->value[$key]) ? $this->value[$key] : NULL;
  }

  /**
   * Sets a component of the configuration value.
   */
  public function set($key, $value) {
    $this->value[$key] = $value;
    return $this;
  }

  /**
   * Sets the entire configuration value.
   */
  public function setData($value) {

    // Retain the _core key.
    $core = isset($this->value['_core']) ? $this->value['_core'] : '';
    $this->value = $value;
    if ($core) {
      $this->value['_core'] = $core;
    }
    return $this;
  }

  /**
   * Saves the configuration.
   */
  public function save() {
    $config = $this->test
      ->getConfigStorage();
    $config[$this->name] = $this->value;
    $this->test
      ->setConfigStorage($config);
    return $this;
  }

  /**
   * Deletes the configuration.
   */
  public function delete() {
    $config = $this->test
      ->getConfigStorage();
    unset($config[$this->name]);
    $this->test
      ->setConfigStorage($config);
    return $this;
  }

  /**
   * Mocks the createFromStorageRecord() method from entity storage.
   */
  public function createFromStorageRecord($values) {
    if (!$this->entityPrefix) {
      return NULL;
    }

    // This is supposed to return an entity, but the only method we need is
    // save(), so instead set up and return this object.
    $this->name = $this->entityPrefix . '.' . $values['id'];
    $this->value = $values;
    $this->value['_core'] = 'core_for_' . $this->name;
    return $this;
  }

  /**
   * Mocks the updateFromStorageRecord() method from entity storage.
   */
  public function updateFromStorageRecord($object, $values) {
    return $object
      ->createFromStorageRecord($values);
  }

  /**
   * Mocks the load() method for entity storage.
   */
  public function load($id) {
    $full_name = $this->entityPrefix . '.' . $id;
    $configs = $this->test
      ->getConfigStorage();
    if (isset($configs[$full_name])) {
      $this->value = $configs[$full_name];
      $this->name = $full_name;
      $this->value['_core'] = 'core_for_' . $full_name;
      return $this;
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockConfig::$entityPrefix protected property Prefix for the entity type being mocked, for entity storage mocking.
MockConfig::$name protected property Name of the config.
MockConfig::$test protected property Test class this comes from.
MockConfig::$value protected property Current value of the configuration.
MockConfig::createFromStorageRecord public function Mocks the createFromStorageRecord() method from entity storage.
MockConfig::delete public function Deletes the configuration.
MockConfig::get public function Gets a component of the configuration value.
MockConfig::load public function Mocks the load() method for entity storage.
MockConfig::save public function Saves the configuration.
MockConfig::set public function Sets a component of the configuration value.
MockConfig::setData public function Sets the entire configuration value.
MockConfig::updateFromStorageRecord public function Mocks the updateFromStorageRecord() method from entity storage.
MockConfig::__construct public function Constructs a mock config object.