Commit dbd2f63e authored by catch's avatar catch
Browse files

task: #3613394 PHPUnit 13.2 - Replace expectExceptionMessage() with...

task: #3613394 PHPUnit 13.2 - Replace expectExceptionMessage() with expectExceptionMessageIs*() in Component tests

By: mondrake
By: dcam
(cherry picked from commit f51df2af)
parent 5dbc49ae
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for core/modules/views/tests/src/Unit/Plugin/HandlerBaseTest.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ public function testAccessThrowsExceptionWithAccessCallback(): void {
    $handler = new TestHandler([], 'test_handler', $definition);

    $this->expectException(AccessException::class);
    $this->expectExceptionMessage("The 'access callback' key in Views handler definitions is no longer supported in drupal:12.0.0. Use a custom access method on the handler instead.");
    $this->expectExceptionMessageIs("The 'access callback' key in Views handler definitions is no longer supported in drupal:12.0.0. Use a custom access method on the handler instead.");
    $handler->access($this->createStub(AccountInterface::class));
  }

+40 −0
Changes for core/tests/Drupal/TestTools/PhpUnitCompatibility/ForwardCompatibilityTrait.php: 40 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\TestTools\PhpUnitCompatibility;

/**
 * Provides forward compatibility shim to future PHPUnit versions.
 *
 * This trait is meant to be used only by test classes.
 *
 * @internal
 */
trait ForwardCompatibilityTrait {

  /**
   * Expects an exactly matching exception message.
   *
   * Forward compatibility for PHPUnit 13.
   *
   * @param string $message
   *   The expected exception message.
   */
  protected function expectExceptionMessageIs(string $message): void {
    $this->expectExceptionMessage($message);
  }

  /**
   * Expects an exception message containing a specified string.
   *
   * Forward compatibility for PHPUnit 13.
   *
   * @param string $message
   *   The expected exception message.
   */
  protected function expectExceptionMessageIsOrContains(string $message): void {
    $this->expectExceptionMessage($message);
  }

}
+17 −14
Changes for core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php: 17 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
use Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationWithConstants;
use Drupal\Tests\Component\Annotation\Doctrine\Fixtures\ClassWithConstants;
use Drupal\Tests\Component\Annotation\Doctrine\Fixtures\IntefaceWithConstants;
use Drupal\TestTools\PhpUnitCompatibility\ForwardCompatibilityTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
@@ -29,6 +30,8 @@
#[Group('Annotation')]
class DocParserTest extends TestCase
{
    use ForwardCompatibilityTrait;

    public function testNestedArraysWithNestedAnnotation(): void
    {
        $parser = $this->createTestParser();
@@ -728,7 +731,7 @@ public function testAnnotationWithRequiredAttributesWithoutContructor(): void
    public function testAnnotationEnumeratorException(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('Attribute "value" of @Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationEnum declared on property SomeClassName::invalidProperty. accepts only [ONE, TWO, THREE], but got FOUR.');
        $this->expectExceptionMessageIs('[Enum Error] Attribute "value" of @Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationEnum declared on property SomeClassName::invalidProperty. accepts only [ONE, TWO, THREE], but got FOUR.');

        $parser     = $this->createTestParser();
        $context    = 'property SomeClassName::invalidProperty.';
@@ -742,7 +745,7 @@ public function testAnnotationEnumeratorException(): void
    public function testAnnotationEnumeratorLiteralException(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('Attribute "value" of @Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationEnumLiteral declared on property SomeClassName::invalidProperty. accepts only [AnnotationEnumLiteral::ONE, AnnotationEnumLiteral::TWO, AnnotationEnumLiteral::THREE], but got 4.');
        $this->expectExceptionMessageIs('[Enum Error] Attribute "value" of @Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationEnumLiteral declared on property SomeClassName::invalidProperty. accepts only [AnnotationEnumLiteral::ONE, AnnotationEnumLiteral::TWO, AnnotationEnumLiteral::THREE], but got 4.');

        $parser     = $this->createTestParser();
        $context    = 'property SomeClassName::invalidProperty.';
@@ -756,7 +759,7 @@ public function testAnnotationEnumeratorLiteralException(): void
    public function testAnnotationEnumInvalidTypeDeclarationException(): void
    {
        $this->expectException('\InvalidArgumentException');
        $this->expectExceptionMessage('@Enum supports only scalar values "array" given.');
        $this->expectExceptionMessageIs('@Enum supports only scalar values "array" given.');

        $parser     = $this->createTestParser();
        $docblock   = '@Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationEnumInvalid("foo")';
@@ -768,7 +771,7 @@ public function testAnnotationEnumInvalidTypeDeclarationException(): void
    public function testAnnotationEnumInvalidLiteralDeclarationException(): void
    {
        $this->expectException('\InvalidArgumentException');
        $this->expectExceptionMessage('Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE".');
        $this->expectExceptionMessageIs('Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE".');

        $parser     = $this->createTestParser();
        $docblock   = '@Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationEnumLiteralInvalid("foo")';
@@ -904,7 +907,7 @@ public function testSupportClassConstants($docblock, $expected): void
    public function testWithoutConstructorWhenIsNotDefaultValue(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on  does not accept any values, but got {"value":"Foo"}.');
        $this->expectExceptionMessageIs('[Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on  does not accept any values, but got {"value":"Foo"}.');

        $parser     = $this->createTestParser();
        $docblock   = <<<DOCBLOCK
@@ -921,7 +924,7 @@ public function testWithoutConstructorWhenIsNotDefaultValue(): void
    public function testWithoutConstructorWhenHasNoProperties(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on  does not accept any values, but got {"value":"Foo"}.');
        $this->expectExceptionMessageIs('[Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on  does not accept any values, but got {"value":"Foo"}.');

        $parser     = $this->createTestParser();
        $docblock   = <<<DOCBLOCK
@@ -937,7 +940,7 @@ public function testWithoutConstructorWhenHasNoProperties(): void
    public function testAnnotationTargetSyntaxError(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('Expected namespace separator or identifier, got \')\' at position 24 in class @Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationWithTargetSyntaxError.');
        $this->expectExceptionMessageIs('[Syntax Error] Expected namespace separator or identifier, got \')\' at position 24 in class @Drupal\Tests\Component\Annotation\Doctrine\Fixtures\AnnotationWithTargetSyntaxError.');

        $parser     = $this->createTestParser();
        $context    = 'class ' . 'SomeClassName';
@@ -954,7 +957,7 @@ public function testAnnotationTargetSyntaxError(): void
    public function testAnnotationWithInvalidTargetDeclarationError(): void
    {
        $this->expectException('\InvalidArgumentException');
        $this->expectExceptionMessage('Invalid Target "Foo". Available targets: [ALL, CLASS, METHOD, PROPERTY, FUNCTION, ANNOTATION]');
        $this->expectExceptionMessageIs('Invalid Target "Foo". Available targets: [ALL, CLASS, METHOD, PROPERTY, FUNCTION, ANNOTATION]');

        $parser     = $this->createTestParser();
        $context    = 'class ' . 'SomeClassName';
@@ -971,7 +974,7 @@ public function testAnnotationWithInvalidTargetDeclarationError(): void
    public function testAnnotationWithTargetEmptyError(): void
    {
        $this->expectException('\InvalidArgumentException');
        $this->expectExceptionMessage('@Target expects either a string value, or an array of strings, "NULL" given.');
        $this->expectExceptionMessageIs('@Target expects either a string value, or an array of strings, "NULL" given.');

        $parser     = $this->createTestParser();
        $context    = 'class ' . 'SomeClassName';
@@ -1049,7 +1052,7 @@ public function testNotAnAnnotationClassIsIgnoredWithoutWarning(): void
    public function testAnnotationDontAcceptSingleQuotes(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('Expected PlainValue, got \'\'\' at position 10.');
        $this->expectExceptionMessageIs('[Syntax Error] Expected PlainValue, got \'\'\' at position 10.');

        $parser = $this->createTestParser();
        $parser->parse("@Name(foo='bar')");
@@ -1123,7 +1126,7 @@ public function createTestParser()
    public function testSyntaxErrorWithContextDescription(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('Expected PlainValue, got \'\'\' at position 10 in class \Drupal\Tests\Component\Annotation\Doctrine\Name');
        $this->expectExceptionMessageIs('[Syntax Error] Expected PlainValue, got \'\'\' at position 10 in class \Drupal\Tests\Component\Annotation\Doctrine\Name.');

        $parser = $this->createTestParser();
        $parser->parse("@Name(foo='bar')", "class \Drupal\Tests\Component\Annotation\Doctrine\Name");
@@ -1240,7 +1243,7 @@ public function testCastNegativeFloat(): void
    public function testSetValuesException(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('[Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructor declared on some class does not have a property named "invalidaProperty". Available properties: data, name');
        $this->expectExceptionMessageIs('[Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructor declared on some class does not have a property named "invalidaProperty". Available properties: data, name');

        $docblock = <<<DOCBLOCK
/**
@@ -1254,7 +1257,7 @@ public function testSetValuesException(): void
    public function testInvalidIdentifierInAnnotation(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('[Syntax Error] Expected Drupal\Component\Annotation\Doctrine\DocLexer::T_IDENTIFIER or Drupal\Component\Annotation\Doctrine\DocLexer::T_TRUE or Drupal\Component\Annotation\Doctrine\DocLexer::T_FALSE or Drupal\Component\Annotation\Doctrine\DocLexer::T_NULL, got \'3.42\' at position 5.');
        $this->expectExceptionMessageIs('[Syntax Error] Expected Drupal\Component\Annotation\Doctrine\DocLexer::T_IDENTIFIER or Drupal\Component\Annotation\Doctrine\DocLexer::T_TRUE or Drupal\Component\Annotation\Doctrine\DocLexer::T_FALSE or Drupal\Component\Annotation\Doctrine\DocLexer::T_NULL, got \'3.42\' at position 5.');

        $parser = $this->createTestParser();
        $parser->parse('@Foo\3.42');
@@ -1293,7 +1296,7 @@ public function testArrayWithColon(): void
    public function testInvalidContantName(): void
    {
        $this->expectException('\Drupal\Component\Annotation\Doctrine\AnnotationException');
        $this->expectExceptionMessage('[Semantical Error] Couldn\'t find constant foo.');
        $this->expectExceptionMessageIs('[Semantical Error] Couldn\'t find constant foo.');

        $parser = $this->createTestParser();
        $parser->parse('@Name(foo: "bar")');
+5 −2
Changes for core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Tests\Component\Datetime;

use Drupal\Component\Datetime\DateTimePlus;
use Drupal\TestTools\PhpUnitCompatibility\ForwardCompatibilityTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
@@ -17,6 +18,8 @@
#[Group('Datetime')]
class DateTimePlusTest extends TestCase {

  use ForwardCompatibilityTrait;

  /**
   * Tests creating dates from string and array input.
   *
@@ -90,7 +93,7 @@ public function testDateDiff(DateTimePlus $input1, DateTimePlus|\DateTime|bool $
  #[DataProvider('providerTestInvalidDateDiff')]
  public function testInvalidDateDiff(DateTimePlus $input1, ?string $input2, bool $absolute): void {
    $this->expectException(\BadMethodCallException::class);
    $this->expectExceptionMessage('Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object');
    $this->expectExceptionMessageIs('Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object');
    $input1->diff($input2, $absolute);
  }

@@ -1082,7 +1085,7 @@ public function testChainableNonChainable(): void {
   */
  public function testChainableNonCallable(): void {
    $this->expectException(\BadMethodCallException::class);
    $this->expectExceptionMessage('Call to undefined method Drupal\Component\Datetime\DateTimePlus::nonexistent()');
    $this->expectExceptionMessageIs('Call to undefined method Drupal\Component\Datetime\DateTimePlus::nonexistent()');
    $date = new DateTimePlus('now', 'Australia/Sydney');
    $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent();
  }
+5 −2
Changes for core/tests/Drupal/Tests/Component/Discovery/YamlDirectoryDiscoveryTest.php: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Component\Discovery\DiscoveryException;
use Drupal\Component\Discovery\YamlDirectoryDiscovery;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\TestTools\PhpUnitCompatibility\ForwardCompatibilityTrait;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
@@ -19,6 +20,8 @@
#[Group('Discovery')]
class YamlDirectoryDiscoveryTest extends TestCase {

  use ForwardCompatibilityTrait;

  /**
   * {@inheritdoc}
   */
@@ -178,7 +181,7 @@ public function testDiscoveryAlternateId(): void {
   */
  public function testDiscoveryNoIdException(): void {
    $this->expectException(DiscoveryException::class);
    $this->expectExceptionMessage('The vfs://modules/test_1' . DIRECTORY_SEPARATOR . 'item_1.test.yml contains no data in the identifier key \'id\'');
    $this->expectExceptionMessageIs('The vfs://modules/test_1' . DIRECTORY_SEPARATOR . 'item_1.test.yml contains no data in the identifier key \'id\'');
    vfsStream::setup('modules', NULL, [
      'test_1' => [
        'item_1.test.yml' => "",
@@ -199,7 +202,7 @@ public function testDiscoveryNoIdException(): void {
   */
  public function testDiscoveryInvalidYamlException(): void {
    $this->expectException(DiscoveryException::class);
    $this->expectExceptionMessage('The vfs://modules/test_1' . DIRECTORY_SEPARATOR . 'item_1.test.yml contains invalid YAML');
    $this->expectExceptionMessageIs('The vfs://modules/test_1' . DIRECTORY_SEPARATOR . 'item_1.test.yml contains invalid YAML');
    vfsStream::setup('modules', NULL, [
      'test_1' => [
        'item_1.test.yml' => "id: invalid\nfoo : [bar}",
Loading