class TranslatableMarkupTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest
  2. 10 core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest
  3. 9 core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest
  4. 8.9.x core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest

Tests the TranslatableMarkup class.

Attributes

#[CoversClass(TranslatableMarkup::class)] #[Group('StringTranslation')]

Hierarchy

Expanded class hierarchy of TranslatableMarkupTest

File

core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php, line 16

Namespace

Drupal\Tests\Core\StringTranslation
View source
class TranslatableMarkupTest extends UnitTestCase {
  
  /**
   * Tests that errors are correctly handled when a __toString() fails.
   *
   * @legacy-covers ::__toString
   */
  public function testToString() : void {
    $string = 'May I have an exception?';
    $exception = new \Exception('Yes you may.');
    $text = $this->getMockBuilder(TranslatableMarkup::class)
      ->setConstructorArgs([
      $string,
      [],
      [],
    ])
      ->onlyMethods([
      'render',
    ])
      ->getMock();
    $text->expects($this->once())
      ->method('render')
      ->willThrowException($exception);
    $this->expectException(\Exception::class);
    $this->expectExceptionMessage($exception->getMessage());
    (string) $text;
  }
  
  /**
   * Tests is string assertion.
   *
   * @legacy-covers ::__construct
   */
  public function testIsStringAssertion() : void {
    $translation = $this->getStringTranslationStub();
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('$string ("foo") must be a string.');
    // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
    new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation));
  }
  
  /**
   * Tests is string assertion with formattable markup.
   *
   * @legacy-covers ::__construct
   */
  public function testIsStringAssertionWithFormattableMarkup() : void {
    $formattable_string = new FormattableMarkup('@bar', [
      '@bar' => 'foo',
    ]);
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('$string ("foo") must be a string.');
    // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
    new TranslatableMarkup($formattable_string);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
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.
TranslatableMarkupTest::testIsStringAssertion public function Tests is string assertion.
TranslatableMarkupTest::testIsStringAssertionWithFormattableMarkup public function Tests is string assertion with formattable markup.
TranslatableMarkupTest::testToString public function Tests that errors are correctly handled when a __toString() fails.
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.
UnitTestCase::setUp protected function 354
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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