class TranslatableMarkupTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest
- 8.9.x core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest
- 10 core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest
Tests the TranslatableMarkup class.
@coversDefaultClass \Drupal\Core\StringTranslation\TranslatableMarkup @group StringTranslation
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of TranslatableMarkupTest
File
-
core/
tests/ Drupal/ Tests/ Core/ StringTranslation/ TranslatableMarkupTest.php, line 18
Namespace
Drupal\Tests\Core\StringTranslationView source
class TranslatableMarkupTest extends UnitTestCase {
/**
* The error message of the last error in the error handler.
*
* @var string
*/
protected $lastErrorMessage;
/**
* The error number of the last error in the error handler.
*
* @var int
*/
protected $lastErrorNumber;
/**
* 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.
*
* @param int $error_number
* The error number.
* @param string $error_message
* The error message.
*/
public function errorHandler($error_number, $error_message) : void {
$this->lastErrorNumber = $error_number;
$this->lastErrorMessage = $error_message;
}
/**
* Tests that errors are correctly handled when a __toString() fails.
*
* @covers ::__toString
*/
public function testToString() : void {
$translation = $this->createMock(TranslationInterface::class);
$string = 'May I have an exception?';
$text = $this->getMockBuilder(TranslatableMarkup::class)
->setConstructorArgs([
$string,
[],
[],
$translation,
])
->onlyMethods([
'_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 .*MockObject_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
}
/**
* @covers ::__construct
*/
public function testIsStringAssertion() : void {
$translation = $this->getStringTranslationStub();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$string ("foo") must be a string.');
new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation));
}
/**
* @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.');
new TranslatableMarkup($formattable_string);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
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. | |
TranslatableMarkupTest::$lastErrorMessage | protected | property | The error message of the last error in the error handler. | |
TranslatableMarkupTest::$lastErrorNumber | protected | property | The error number of the last error in the error handler. | |
TranslatableMarkupTest::errorHandler | public | function | Custom error handler that saves the last error. | |
TranslatableMarkupTest::testIsStringAssertion | public | function | @covers ::__construct | |
TranslatableMarkupTest::testIsStringAssertionWithFormattableMarkup | public | function | @covers ::__construct | |
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::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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUp | protected | function | 358 | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.