TranslatableMarkupTest.php 3.19 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest.
6 7 8 9
 */

namespace Drupal\Tests\Core\StringTranslation;

10
use Drupal\Component\Render\FormattableMarkup;
11
use Drupal\Core\StringTranslation\TranslationInterface;
12
use Drupal\Core\StringTranslation\TranslatableMarkup;
13 14 15
use Drupal\Tests\UnitTestCase;

/**
16
 * Tests the TranslatableMarkup class.
17
 *
18
 * @coversDefaultClass \Drupal\Core\StringTranslation\TranslatableMarkup
19 20
 * @group StringTranslation
 */
21
class TranslatableMarkupTest extends UnitTestCase {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

  /**
   * 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) {
    $this->lastErrorNumber = $error_number;
    $this->lastErrorMessage = $error_message;
  }

  /**
   * Tests that errors are correctly handled when a __toString() fails.
   *
   * @covers ::__toString
   */
  public function testToString() {
60 61
    $translation = $this->getMock(TranslationInterface::class);

62
    $string = 'May I have an exception please?';
63
    $text = $this->getMockBuilder(TranslatableMarkup::class)
64
      ->setConstructorArgs([$string, [], [], $translation])
65 66 67 68 69 70 71
      ->setMethods(['_die'])
      ->getMock();
    $text
      ->expects($this->once())
      ->method('_die')
      ->willReturn('');

72 73 74 75
    $translation
      ->method('translateString')
      ->with($text)
      ->willReturnCallback(function () {
76 77 78 79 80 81 82 83 84 85
      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);
86
    $this->assertRegExp('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
87 88
  }

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  /**
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage $string ("foo") must be a string.
   *
   * @covers ::__construct
   */
  public function testIsStringAssertion() {
    $translation = $this->getStringTranslationStub();
    new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation));
  }

  /**
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage $string ("foo") must be a string.
   *
   * @covers ::__construct
   */
  public function testIsStringAssertionWithFormattableMarkup() {
    $translation = $this->getStringTranslationStub();
    $formattable_string = new FormattableMarkup('@bar', ['@bar' => 'foo']);
    new TranslatableMarkup($formattable_string);
  }

112
}