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