TranslatableMarkupTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Drupal\Tests\Core\StringTranslation;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Core\StringTranslation\TranslationInterface;
  5. use Drupal\Core\StringTranslation\TranslatableMarkup;
  6. use Drupal\Tests\UnitTestCase;
  7. /**
  8. * Tests the TranslatableMarkup class.
  9. *
  10. * @coversDefaultClass \Drupal\Core\StringTranslation\TranslatableMarkup
  11. * @group StringTranslation
  12. */
  13. class TranslatableMarkupTest extends UnitTestCase {
  14. /**
  15. * The error message of the last error in the error handler.
  16. *
  17. * @var string
  18. */
  19. protected $lastErrorMessage;
  20. /**
  21. * The error number of the last error in the error handler.
  22. *
  23. * @var int
  24. */
  25. protected $lastErrorNumber;
  26. /**
  27. * Custom error handler that saves the last error.
  28. *
  29. * We need this custom error handler because we cannot rely on the error to
  30. * exception conversion as __toString is never allowed to leak any kind of
  31. * exception.
  32. *
  33. * @param int $error_number
  34. * The error number.
  35. * @param string $error_message
  36. * The error message.
  37. */
  38. public function errorHandler($error_number, $error_message) {
  39. $this->lastErrorNumber = $error_number;
  40. $this->lastErrorMessage = $error_message;
  41. }
  42. /**
  43. * Tests that errors are correctly handled when a __toString() fails.
  44. *
  45. * @covers ::__toString
  46. */
  47. public function testToString() {
  48. $translation = $this->createMock(TranslationInterface::class);
  49. $string = 'May I have an exception please?';
  50. $text = $this->getMockBuilder(TranslatableMarkup::class)
  51. ->setConstructorArgs([$string, [], [], $translation])
  52. ->setMethods(['_die'])
  53. ->getMock();
  54. $text
  55. ->expects($this->once())
  56. ->method('_die')
  57. ->willReturn('');
  58. $translation
  59. ->method('translateString')
  60. ->with($text)
  61. ->willReturnCallback(function () {
  62. throw new \Exception('Yes you may.');
  63. });
  64. // We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
  65. set_error_handler([$this, 'errorHandler']);
  66. // We want this to trigger an error.
  67. (string) $text;
  68. restore_error_handler();
  69. $this->assertEquals(E_USER_ERROR, $this->lastErrorNumber);
  70. $this->assertRegExp('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
  71. }
  72. /**
  73. * @covers ::__construct
  74. */
  75. public function testIsStringAssertion() {
  76. $translation = $this->getStringTranslationStub();
  77. $this->expectException(\InvalidArgumentException::class);
  78. $this->expectExceptionMessage('$string ("foo") must be a string.');
  79. new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation));
  80. }
  81. /**
  82. * @covers ::__construct
  83. */
  84. public function testIsStringAssertionWithFormattableMarkup() {
  85. $formattable_string = new FormattableMarkup('@bar', ['@bar' => 'foo']);
  86. $this->expectException(\InvalidArgumentException::class);
  87. $this->expectExceptionMessage('$string ("foo") must be a string.');
  88. new TranslatableMarkup($formattable_string);
  89. }
  90. }