class StringTranslationTraitTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php \Drupal\Tests\Core\StringTranslation\StringTranslationTraitTest
  2. 8.9.x core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php \Drupal\Tests\Core\StringTranslation\StringTranslationTraitTest
  3. 10 core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php \Drupal\Tests\Core\StringTranslation\StringTranslationTraitTest

@coversDefaultClass \Drupal\Core\StringTranslation\StringTranslationTrait @group StringTranslation

Hierarchy

Expanded class hierarchy of StringTranslationTraitTest

File

core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php, line 18

Namespace

Drupal\Tests\Core\StringTranslation
View source
class StringTranslationTraitTest extends UnitTestCase {
    
    /**
     * The object under test that uses StringTranslationTrait.
     */
    protected object $testObject;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        // Prepare a mock translation service to pass to the trait.
        $translation = $this->prophesize(TranslationInterface::class);
        $translation->translate(Argument::cetera())
            ->shouldNotBeCalled();
        $translation->formatPlural(Argument::cetera())
            ->shouldNotBeCalled();
        $translation->translateString(Argument::cetera())
            ->will(function ($args) {
            return $args[0]->getUntranslatedString();
        });
        // Set up the object under test.
        $this->testObject = new class  {
            use StringTranslationTrait;

};
        $this->testObject
            ->setStringTranslation($translation->reveal());
    }
    
    /**
     * @covers ::t
     */
    public function testT() : void {
        $invokableT = new \ReflectionMethod($this->testObject, 't');
        $result = $invokableT->invoke($this->testObject, 'something');
        $this->assertInstanceOf(TranslatableMarkup::class, $result);
        $this->assertEquals('something', $result);
    }
    
    /**
     * @covers ::formatPlural
     */
    public function testFormatPlural() : void {
        $invokableFormatPlural = new \ReflectionMethod($this->testObject, 'formatPlural');
        $result = $invokableFormatPlural->invoke($this->testObject, 1, 'apple', 'apples');
        $this->assertInstanceOf(PluralTranslatableMarkup::class, $result);
        $this->assertEquals('apple', $result);
        $result = $invokableFormatPlural->invoke($this->testObject, 2, 'apple', 'apples');
        $this->assertInstanceOf(PluralTranslatableMarkup::class, $result);
        $this->assertEquals('apples', $result);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
StringTranslationTraitTest::$testObject protected property The object under test that uses StringTranslationTrait.
StringTranslationTraitTest::setUp protected function Overrides UnitTestCase::setUp
StringTranslationTraitTest::testFormatPlural public function @covers ::formatPlural
StringTranslationTraitTest::testT public function @covers ::t
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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