You are here

class TranslationMock in Plugin 8.2

Provides a mock implementation of \Drupal\Core\StringTranslation\TranslationInterface.

This is an alternative to UnitTestCase::getStringTranslationStub(), which cannot be used inside usort() callbacks, for instance.

Hierarchy

Expanded class hierarchy of TranslationMock

1 file declares its use of TranslationMock
ListPluginTypesTest.php in tests/src/Unit/Controller/ListPluginTypesTest.php

File

tests/src/Unit/TranslationMock.php, line 15

Namespace

Drupal\Tests\plugin\Unit
View source
class TranslationMock implements TranslationInterface {

  /**
   * {@inheritdoc}
   */
  public function translate($string, array $args = array(), array $options = array()) {
    return new TranslatableMarkup($string, $args, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function translateString(TranslatableMarkup $translatable_string) {
    return new FormattableMarkup($translatable_string
      ->getUntranslatedString(), $translatable_string
      ->getArguments());
  }

  /**
   * {@inheritdoc}
   */
  public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) {
    return $count === 1 ? new FormattableMarkup($singular, $args) : new FormattableMarkup($plural, $args + [
      '@count' => $count,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array()) {
    return new FormattableMarkup($translation, $args);
  }

  /**
   * {@inheritdoc}
   */
  public function getNumberOfPlurals($langcode = NULL) {
    return mt_rand();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TranslationMock::formatPlural public function Formats a string containing a count of items. Overrides TranslationInterface::formatPlural
TranslationMock::formatPluralTranslated public function
TranslationMock::getNumberOfPlurals public function
TranslationMock::translate public function Translates a string to the current language or to a given language. Overrides TranslationInterface::translate
TranslationMock::translateString public function Translates a TranslatableMarkup object to a string. Overrides TranslationInterface::translateString