public function TranslatableMarkupTest::testToString in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest::testToString()
- 10 core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest::testToString()
Tests that errors are correctly handled when a __toString() fails.
@covers ::__toString
File
- core/
tests/ Drupal/ Tests/ Core/ StringTranslation/ TranslatableMarkupTest.php, line 54
Class
- TranslatableMarkupTest
- Tests the TranslatableMarkup class.
Namespace
Drupal\Tests\Core\StringTranslationCode
public function testToString() {
$translation = $this
->createMock(TranslationInterface::class);
$string = 'May I have an exception please?';
$text = $this
->getMockBuilder(TranslatableMarkup::class)
->setConstructorArgs([
$string,
[],
[],
$translation,
])
->setMethods([
'_die',
])
->getMock();
$text
->expects($this
->once())
->method('_die')
->willReturn('');
$translation
->method('translateString')
->with($text)
->willReturnCallback(function () {
throw new \Exception('Yes you may.');
});
// We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
set_error_handler([
$this,
'errorHandler',
]);
// We want this to trigger an error.
(string) $text;
restore_error_handler();
$this
->assertEquals(E_USER_ERROR, $this->lastErrorNumber);
$this
->assertMatchesRegularExpression('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
}