Issue #3433093 by mondrake: Method getObjectForTrait() of class PHPUnit\Framework\TestCase is deprecated in PHPUnit 10

(cherry picked from commit 2dad474438)
merge-requests/7287/head
Alex Pott 2024-03-25 10:32:03 +00:00
parent 0590956f51
commit 74631a1331
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
1 changed files with 25 additions and 27 deletions

View File

@ -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);
}