function FormattableMarkupTest::testUnexpectedPlaceholder

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php \Drupal\Tests\Component\Render\FormattableMarkupTest::testUnexpectedPlaceholder()
  2. 10 core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php \Drupal\Tests\Component\Render\FormattableMarkupTest::testUnexpectedPlaceholder()
  3. 9 core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php \Drupal\Tests\Component\Render\FormattableMarkupTest::testUnexpectedPlaceholder()
  4. 8.9.x core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php \Drupal\Tests\Component\Render\FormattableMarkupTest::testUnexpectedPlaceholder()

@legacy-covers ::__toString

Attributes

#[DataProvider('providerTestUnexpectedPlaceholder')]

File

core/tests/Drupal/Tests/Component/Render/FormattableMarkupTest.php, line 61

Class

FormattableMarkupTest
Tests the TranslatableMarkup class.

Namespace

Drupal\Tests\Component\Render

Code

public function testUnexpectedPlaceholder(string $string, array $arguments, ?int $error_number, string $error_message) : void {
  // We set a custom error handler that saves the last error.
  // We need this custom error handler because we cannot rely on the error to
  // exception conversion as __toString is never allowed to leak any kind of
  // exception.
  // https://github.com/sebastianbergmann/phpunit/issues/487
  set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) : bool {
    $this->lastErrorNumber = $errno;
    $this->lastErrorMessage = $errstr;
    return TRUE;
  });
  // We want this to trigger an error.
  $markup = new FormattableMarkup($string, $arguments);
  // Cast it to a string which will generate the errors.
  $output = (string) $markup;
  restore_error_handler();
  // The string should not change.
  $this->assertEquals($string, $output);
  $this->assertEquals($error_number, $this->lastErrorNumber);
  $this->assertEquals($error_message, $this->lastErrorMessage);
}

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