You are here

class StringTranslationTraitTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php \Drupal\Tests\Core\StringTranslation\StringTranslationTraitTest
  2. 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 15

Namespace

Drupal\Tests\Core\StringTranslation
View source
class StringTranslationTraitTest extends UnitTestCase {

  /**
   * A reflection of self::$translation.
   *
   * @var \ReflectionClass
   */
  protected $reflection;

  /**
   * The mock under test that uses StringTranslationTrait.
   *
   * @var object
   * @see \PHPUnit\Framework\MockObject\Generator::getObjectForTrait()
   */
  protected $translation;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->translation = $this
      ->getObjectForTrait('\\Drupal\\Core\\StringTranslation\\StringTranslationTrait');
    $mock = $this
      ->prophesize(TranslationInterface::class);
    $mock
      ->translate(Argument::cetera())
      ->shouldNotBeCalled();
    $mock
      ->formatPlural(Argument::cetera())
      ->shouldNotBeCalled();
    $mock
      ->translateString(Argument::cetera())
      ->will(function ($args) {
      return $args[0]
        ->getUntranslatedString();
    });
    $this->translation
      ->setStringTranslation($mock
      ->reveal());
    $this->reflection = new \ReflectionClass(get_class($this->translation));
  }

  /**
   * @covers ::t
   */
  public function testT() {
    $method = $this->reflection
      ->getMethod('t');
    $method
      ->setAccessible(TRUE);
    $result = $method
      ->invoke($this->translation, 'something');
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $result);
    $this
      ->assertEquals('something', $result);
  }

  /**
   * @covers ::formatPlural
   */
  public function testFormatPlural() {
    $method = $this->reflection
      ->getMethod('formatPlural');
    $method
      ->setAccessible(TRUE);
    $result = $method
      ->invoke($this->translation, 2, 'apple', 'apples');
    $this
      ->assertInstanceOf(PluralTranslatableMarkup::class, $result);
    $this
      ->assertEquals('apples', $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
StringTranslationTraitTest::$reflection protected property A reflection of self::$translation.
StringTranslationTraitTest::$translation protected property The mock 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::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.