function UnitTestCase::getStringTranslationStub

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getStringTranslationStub()
  2. 10 core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getStringTranslationStub()
  3. 11.x core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getStringTranslationStub()

Returns a stub translation manager that just returns the passed string.

Return value

\PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\StringTranslation\TranslationInterface A mock translation object.

60 calls to UnitTestCase::getStringTranslationStub()
AggregatorPluginSettingsBaseTest::setUp in core/modules/aggregator/tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.php
BaseFieldDefinitionTestBase::setUp in core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php
BlockManagerTest::setUp in core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php
BookManagerTest::setUp in core/modules/book/tests/src/Unit/BookManagerTest.php
BookUninstallValidatorTest::setUp in core/modules/book/tests/src/Unit/BookUninstallValidatorTest.php

... See full list

File

core/tests/Drupal/Tests/UnitTestCase.php, line 220

Class

UnitTestCase
Provides a base class and helpers for Drupal unit tests.

Namespace

Drupal\Tests

Code

public function getStringTranslationStub() {
    $translation = $this->createMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
    $translation->expects($this->any())
        ->method('translate')
        ->willReturnCallback(function ($string, array $args = [], array $options = []) use ($translation) {
        return new TranslatableMarkup($string, $args, $options, $translation);
    });
    $translation->expects($this->any())
        ->method('translateString')
        ->willReturnCallback(function (TranslatableMarkup $wrapper) {
        return $wrapper->getUntranslatedString();
    });
    $translation->expects($this->any())
        ->method('formatPlural')
        ->willReturnCallback(function ($count, $singular, $plural, array $args = [], array $options = []) use ($translation) {
        $wrapper = new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $translation);
        return $wrapper;
    });
    return $translation;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.