Verified Commit 503f885f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3433093 by mondrake: Method getObjectForTrait() of class...

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

(cherry picked from commit 2dad4744)
parent 4fb6e509
Loading
Loading
Loading
Loading
Loading
+25 −27
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
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 @@
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 @@ public function testT() {
  /**
   * @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);
  }