diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php index 69cea9cae73..dcb0a2ba8bd 100644 --- a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php +++ b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Drupal\Tests\Core\StringTranslation; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Tests\UnitTestCase; @@ -17,19 +18,9 @@ use Prophecy\Argument; class StringTranslationTraitTest extends UnitTestCase { /** - * A reflection of self::$translation. - * - * @var \ReflectionClass + * The object under test that uses StringTranslationTrait. */ - protected $reflection; - - /** - * The mock under test that uses StringTranslationTrait. - * - * @var object - * @see \PHPUnit\Framework\MockObject\Generator::getObjectForTrait() - */ - protected $translation; + protected object $testObject; /** * {@inheritdoc} @@ -37,24 +28,29 @@ class StringTranslationTraitTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); - $this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationTrait'); - $mock = $this->prophesize(TranslationInterface::class); - $mock->translate(Argument::cetera())->shouldNotBeCalled(); - $mock->formatPlural(Argument::cetera())->shouldNotBeCalled(); - $mock->translateString(Argument::cetera())->will(function ($args) { + // Prepare a mock translation service to pass to the trait. + $translation = $this->prophesize(TranslationInterface::class); + $translation->translate(Argument::cetera())->shouldNotBeCalled(); + $translation->formatPlural(Argument::cetera())->shouldNotBeCalled(); + $translation->translateString(Argument::cetera())->will(function ($args) { return $args[0]->getUntranslatedString(); }); - $this->translation->setStringTranslation($mock->reveal()); - $this->reflection = new \ReflectionClass(get_class($this->translation)); + + // Set up the object under test. + $this->testObject = new class() { + + use StringTranslationTrait; + + }; + $this->testObject->setStringTranslation($translation->reveal()); } /** * @covers ::t */ - public function testT() { - $method = $this->reflection->getMethod('t'); - - $result = $method->invoke($this->translation, 'something'); + public function testT(): void { + $invokableT = new \ReflectionMethod($this->testObject, 't'); + $result = $invokableT->invoke($this->testObject, 'something'); $this->assertInstanceOf(TranslatableMarkup::class, $result); $this->assertEquals('something', $result); } @@ -62,10 +58,12 @@ class StringTranslationTraitTest extends UnitTestCase { /** * @covers ::formatPlural */ - public function testFormatPlural() { - $method = $this->reflection->getMethod('formatPlural'); - - $result = $method->invoke($this->translation, 2, 'apple', 'apples'); + public function testFormatPlural(): void { + $invokableFormatPlural = new \ReflectionMethod($this->testObject, 'formatPlural'); + $result = $invokableFormatPlural->invoke($this->testObject, 1, 'apple', 'apples'); + $this->assertInstanceOf(PluralTranslatableMarkup::class, $result); + $this->assertEquals('apple', $result); + $result = $invokableFormatPlural->invoke($this->testObject, 2, 'apple', 'apples'); $this->assertInstanceOf(PluralTranslatableMarkup::class, $result); $this->assertEquals('apples', $result); }