Verified Commit ed828b00 authored by godotislate's avatar godotislate
Browse files

task: #3581399 Stop using legacy names for test suites

By: mondrake
By: longwave
parent c62d3799
Loading
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -305,7 +305,7 @@ default:
    - mkdir -p ./sites/simpletest ./sites/default/files ./build/logs/junit /var/www/.composer
    - chown -R www-data:www-data ./sites ./build/logs/junit ./vendor /var/www/
  script:
    - sudo -u www-data -E -H php ./core/scripts/run-tests.sh --color --keep-results --types "PHPUnit-Unit" --concurrency "$CONCURRENCY" --repeat "1" --sqlite ":memory:" --verbose --non-html --all || EXIT_CODE=$?
    - sudo -u www-data -E -H php ./core/scripts/run-tests.sh --color --keep-results --types "unit" --concurrency "$CONCURRENCY" --repeat "1" --sqlite ":memory:" --verbose --non-html --all || EXIT_CODE=$?
    # Allow failure for the next PHP major.
    - |
      [[ $EXIT_CODE != 0 && $_TARGET_PHP == $NEXT_PHP_MAJOR ]] && exit 100
@@ -414,7 +414,7 @@ default:
  <<: [ *with-composer, *test-setup ]
  retry: 1
  variables:
    TESTSUITE: PHPUnit-Build
    TESTSUITE: build
    KUBERNETES_CPU_REQUEST: "16"
    CONCURRENCY: "8"
    # No parallel for this job, setup manually the env variables that need to
@@ -476,7 +476,7 @@ default:
  <<: [ *with-composer, *test-setup ]
  parallel: 3
  variables:
    TESTSUITE: PHPUnit-FunctionalJavascript
    TESTSUITE: functional-javascript
    CONCURRENCY: "12"
    KUBERNETES_CPU_REQUEST: "14"
  services:
@@ -490,7 +490,7 @@ default:
  <<: [ *with-composer, *test-setup ]
  parallel: 8
  variables:
    TESTSUITE: PHPUnit-Functional
    TESTSUITE: functional
    CONCURRENCY: "20"
    KUBERNETES_CPU_REQUEST: "14"
  services:
@@ -500,7 +500,7 @@ default:
  <<: [ *with-composer, *test-setup ]
  parallel: 4
  variables:
    TESTSUITE: PHPUnit-Kernel
    TESTSUITE: kernel
    KUBERNETES_CPU_REQUEST: "10"
    CONCURRENCY: "20"
  services:
+19 −45
Original line number Diff line number Diff line
@@ -30,27 +30,6 @@ class PhpUnitTestDiscovery {
   */
  private static ?self $instance = NULL;

  /**
   * The map of legacy test suite identifiers to phpunit.xml ones.
   *
   * @var array<string,string>
   */
  private array $map = [
    'PHPUnit-FunctionalJavascript' => 'functional-javascript',
    'PHPUnit-Functional' => 'functional',
    'PHPUnit-Kernel' => 'kernel',
    'PHPUnit-Unit' => 'unit',
    'PHPUnit-Unit-Component' => 'unit-component',
    'PHPUnit-Build' => 'build',
  ];

  /**
   * The reverse map of legacy test suite identifiers to phpunit.xml ones.
   *
   * @var array<string,string>
   */
  private array $reverseMap;

  /**
   * Path to PHPUnit's configuration file.
   */
@@ -64,7 +43,6 @@ class PhpUnitTestDiscovery {
  private array $warnings = [];

  private function __construct() {
    $this->reverseMap = array_flip($this->map);
    try {
      EventFacade::instance()->registerTracer(new PhpUnitTestDiscoveryTracer($this));
      EventFacade::instance()->seal();
@@ -114,20 +92,15 @@ public function getTestClasses(?string $extension = NULL, array $testSuites = []
    $args = ['--configuration', $this->configurationFilePath];

    if (!empty($testSuites)) {
      // Convert $testSuites from Drupal's legacy syntax to the syntax used in
      // phpunit.xml, that is necessary to PHPUnit to be able to apply the
      // test suite filter. For example, 'PHPUnit-Unit' to 'unit'.
      $tmp = [];
      foreach ($testSuites as $i) {
        if (!is_string($i)) {
      foreach ($testSuites as $testSuite) {
        if (!is_string($testSuite)) {
          throw new \InvalidArgumentException("Test suite must be a string");
        }
        if (str_contains($i, ' ')) {
          throw new \InvalidArgumentException("Test suite name '{$i}' is invalid");
        if (str_contains($testSuite, ' ')) {
          throw new \InvalidArgumentException("Test suite name '{$testSuite}' is invalid");
        }
        $tmp[] = $this->map[$i] ?? $i;
      }
      $args[] = '--testsuite=' . implode(',', $tmp);
      $args[] = '--testsuite=' . implode(',', $testSuites);
    }

    if ($directory !== NULL) {
@@ -238,10 +211,7 @@ private function getTestList(TestSuite $phpUnitTestSuite, ?string $extension): a
          continue;
        }

        $item = $this->getTestClassInfo(
          $testClass,
          $this->reverseMap[$testSuite->name()] ?? $testSuite->name(),
        );
        $item = $this->getTestClassInfo($testClass, $testSuite->name());

        foreach ($item['groups'] as $group) {
          $list[$group][$item['name']] = $item;
@@ -281,7 +251,7 @@ private function getTestListLimitedToDirectory(TestSuite $phpUnitTestSuite, ?str
      }

      // Take the test suite name from the class namespace.
      $testSuite = 'PHPUnit-' . self::getPhpunitTestSuite($phpUnitTestSuite->name());
      $testSuite = self::getPhpunitTestSuite($phpUnitTestSuite->name());
      if (!empty($testSuites) && !in_array($testSuite, $testSuites, TRUE)) {
        return [];
      }
@@ -306,7 +276,7 @@ private function getTestListLimitedToDirectory(TestSuite $phpUnitTestSuite, ?str
      }

      // Take the test suite name from the class namespace.
      $testSuite = 'PHPUnit-' . self::getPhpunitTestSuite($testClass->name());
      $testSuite = self::getPhpunitTestSuite($testClass->name());
      if (!empty($testSuites) && !in_array($testSuite, $testSuites, TRUE)) {
        continue;
      }
@@ -436,21 +406,25 @@ private static function parseTestClassSummary(string $doc_comment): string {
  public static function getPhpunitTestSuite(string $classname): string|false {
    if (preg_match('/Drupal\\\\Tests\\\\(\w+)\\\\(\w+)/', $classname, $matches)) {
      if ($matches[1] === 'Component') {
        return 'Unit-Component';
        return 'unit-component';
      }
      // This could be an extension test, in which case the first match will be
      // the extension name. We assume that lower-case strings are module names.
      if (strtolower($matches[1]) == $matches[1]) {
        return $matches[2];
        return match ($matches[2]) {
          'FunctionalJavascript' => 'functional-javascript',
          default => strtolower($matches[2]),
        };
      }
      return 'Unit';
      return 'unit';
    }
    // Core tests.
    elseif (preg_match('/Drupal\\\\(\w*)Tests\\\\/', $classname, $matches)) {
      if ($matches[1] == '') {
        return 'Unit';
      }
      return $matches[1];
      return match ($matches[1]) {
        '' => 'unit',
        'FunctionalJavascript' => 'functional-javascript',
        default => strtolower($matches[1]),
      };
    }
    return FALSE;
  }
+8 −8
Original line number Diff line number Diff line
@@ -37,15 +37,15 @@ public function testSuite(array $suites, ?string $extension = NULL, ?string $dir
   */
  public static function argumentsProvider(): \Generator {
    yield 'All tests' => ['suites' => []];
    yield 'Testsuite: functional-javascript' => ['suites' => ['PHPUnit-FunctionalJavascript']];
    yield 'Testsuite: functional' => ['suites' => ['PHPUnit-Functional']];
    yield 'Testsuite: kernel' => ['suites' => ['PHPUnit-Kernel']];
    yield 'Testsuite: unit' => ['suites' => ['PHPUnit-Unit']];
    yield 'Testsuite: unit-component' => ['suites' => ['PHPUnit-Unit-Component']];
    yield 'Testsuite: build' => ['suites' => ['PHPUnit-Build']];
    yield 'Testsuite: functional-javascript' => ['suites' => ['functional-javascript']];
    yield 'Testsuite: functional' => ['suites' => ['functional']];
    yield 'Testsuite: kernel' => ['suites' => ['kernel']];
    yield 'Testsuite: unit' => ['suites' => ['unit']];
    yield 'Testsuite: unit-component' => ['suites' => ['unit-component']];
    yield 'Testsuite: build' => ['suites' => ['build']];
    yield 'Extension: system' => ['suites' => [], 'extension' => 'system'];
    yield 'Extension: system, testsuite: unit' => [
      'suites' => ['PHPUnit-Unit'],
      'suites' => ['unit'],
      'extension' => 'system',
    ];
    yield 'Extension: system, directory' => [
@@ -54,7 +54,7 @@ public static function argumentsProvider(): \Generator {
      'directory' => 'core/modules/system/tests/src',
    ];
    yield 'Extension: system, testsuite: unit, directory' => [
      'suites' => ['PHPUnit-Unit'],
      'suites' => ['unit'],
      'extension' => 'system',
      'directory' => 'core/modules/system/tests/src',
    ];
+1 −1
Original line number Diff line number Diff line
@@ -198,7 +198,7 @@ public static function commandLineDefinition(): InputDefinition {
      'types',
      NULL,
      InputOption::VALUE_REQUIRED,
      'Runs just tests from the specified test type, for example run-tests.sh --types "PHPUnit-Unit,PHPUnit-Kernel"',
      'Runs just tests from the specified test type, for example run-tests.sh --types "unit,kernel"',
    ));
    $inputDefinition->addOption(new InputOption(
      'directory',
+28 −30
Original line number Diff line number Diff line
@@ -37,16 +37,16 @@ public function testTestInfoParser(array $expected, string $classname): void {
    $this->assertEquals($expected, $info);
  }

  public static function infoParserProvider(): array {
  public static function infoParserProvider(): \Generator {
    // A module provided unit test.
    $tests[] = [
    yield 'module provided unit test' => [
      // Expected result.
      [
        'name' => TestDatabaseTest::class,
        'group' => 'Test',
        'groups' => ['Test', 'simpletest', 'Template'],
        'description' => 'Tests Drupal\Core\Test\TestDatabase.',
        'type' => 'PHPUnit-Unit',
        'type' => 'unit',
        'tests_count' => 4,
      ],
      // Classname.
@@ -54,14 +54,14 @@ public static function infoParserProvider(): array {
    ];

    // A core unit test.
    $tests[] = [
    yield 'core unit test' => [
      // Expected result.
      [
        'name' => 'Drupal\Tests\Core\DrupalTest',
        'group' => 'DrupalTest',
        'groups' => ['DrupalTest'],
        'description' => 'Tests Drupal.',
        'type' => 'PHPUnit-Unit',
        'type' => 'unit',
        'tests_count' => 34,
      ],
      // Classname.
@@ -69,14 +69,14 @@ public static function infoParserProvider(): array {
    ];

    // A component unit test.
    $tests[] = [
    yield 'component unit test' => [
      // Expected result.
      [
        'name' => 'Drupal\Tests\Component\Plugin\PluginBaseTest',
        'group' => 'Plugin',
        'groups' => ['Plugin'],
        'description' => 'Tests Drupal\Component\Plugin\PluginBase.',
        'type' => 'PHPUnit-Unit-Component',
        'type' => 'unit-component',
        'tests_count' => 7,
      ],
      // Classname.
@@ -84,14 +84,14 @@ public static function infoParserProvider(): array {
    ];

    // Functional PHPUnit test.
    $tests[] = [
    yield 'functional test' => [
      // Expected result.
      [
        'name' => 'Drupal\FunctionalTests\BrowserTestBaseTest',
        'group' => 'browsertestbase',
        'groups' => ['browsertestbase'],
        'description' => 'Tests BrowserTestBase functionality.',
        'type' => 'PHPUnit-Functional',
        'type' => 'functional',
        'tests_count' => 21,
      ],
      // Classname.
@@ -99,21 +99,19 @@ public static function infoParserProvider(): array {
    ];

    // Kernel PHPUnit test.
    $tests['phpunit-kernel'] = [
    yield 'kernel test' => [
      // Expected result.
      [
        'name' => 'Drupal\Tests\file\Kernel\FileItemValidationTest',
        'group' => 'file',
        'groups' => ['file'],
        'description' => 'Tests that files referenced in file and image fields are always validated.',
        'type' => 'PHPUnit-Kernel',
        'type' => 'kernel',
        'tests_count' => 2,
      ],
      // Classname.
      'Drupal\Tests\file\Kernel\FileItemValidationTest',
    ];

    return $tests;
  }

  /**
@@ -148,25 +146,25 @@ public function testGetPhpunitTestSuite(string $classname, string|false $expecte
    $this->assertEquals($expected, PhpUnitTestDiscovery::getPhpunitTestSuite($classname));
  }

  public static function providerTestGetPhpunitTestSuite(): array {
    $data = [];
    $data['simpletest-web test'] = ['\Drupal\rest\Tests\NodeTest', FALSE];
    $data['module-unittest'] = [static::class, 'Unit'];
    $data['module-kernel test'] = ['\Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest', 'Kernel'];
    $data['module-functional test'] = ['\Drupal\FunctionalTests\BrowserTestBaseTest', 'Functional'];
    $data['module-functional javascript test'] = [
  public static function providerTestGetPhpunitTestSuite(): \Generator {
    yield 'simpletest-web test' => ['\Drupal\rest\Tests\NodeTest', FALSE];
    yield 'module-unittest' => [static::class, 'unit'];
    yield 'module-kernel test' => ['\Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest', 'kernel'];
    yield 'module-functional test' => ['\Drupal\FunctionalTests\BrowserTestBaseTest', 'functional'];
    yield 'module-functional javascript test' => [
      '\Drupal\Tests\toolbar\FunctionalJavascript\ToolbarIntegrationTest',
      'FunctionalJavascript',
      'functional-javascript',
    ];
    $data['core-unittest'] = ['\Drupal\Tests\ComposerIntegrationTest', 'Unit'];
    $data['core-unittest2'] = ['Drupal\Tests\Core\DrupalTest', 'Unit'];
    $data['core-script-test'] = ['Drupal\KernelTests\Scripts\TestSiteApplicationTest', 'Kernel'];
    $data['core-kernel test'] = ['\Drupal\KernelTests\KernelTestBaseTest', 'Kernel'];
    $data['core-functional test'] = ['\Drupal\FunctionalTests\ExampleTest', 'Functional'];
    $data['core-functional javascript test'] = ['\Drupal\FunctionalJavascriptTests\ExampleTest', 'FunctionalJavascript'];
    $data['core-build test'] = ['\Drupal\BuildTests\Framework\Tests\BuildTestTest', 'Build'];

    return $data;
    yield 'core-unittest' => ['\Drupal\Tests\ComposerIntegrationTest', 'unit'];
    yield 'core-unittest2' => ['Drupal\Tests\Core\DrupalTest', 'unit'];
    yield 'core-script-test' => ['Drupal\KernelTests\Scripts\TestSiteApplicationTest', 'kernel'];
    yield 'core-kernel test' => ['\Drupal\KernelTests\KernelTestBaseTest', 'kernel'];
    yield 'core-functional test' => ['\Drupal\FunctionalTests\ExampleTest', 'functional'];
    yield 'core-functional javascript test' => [
      '\Drupal\FunctionalJavascriptTests\ExampleTest',
      'functional-javascript',
    ];
    yield 'core-build test' => ['\Drupal\BuildTests\Framework\Tests\BuildTestTest', 'build'];
  }

}