function PhpUnitApiGetTestClassesTest::testEquality

Checks that Drupal legacy and PHPUnit API based discoveries are equal.

File

core/tests/Drupal/KernelTests/Core/Test/PhpUnitApiGetTestClassesTest.php, line 31

Class

PhpUnitApiGetTestClassesTest
Tests ::getTestClasses() between TestDiscovery and PhpPUnitTestDiscovery.

Namespace

Drupal\KernelTests\Core\Test

Code

public function testEquality(array $suites, ?string $extension = NULL, ?string $directory = NULL) : void {
    $testDiscovery = new TestDiscovery($this->container
        ->getParameter('app.root'), $this->container
        ->get('class_loader'));
    $internalList = $testDiscovery->getTestClasses($extension, $suites, $directory);
    // Location of PHPUnit configuration file.
    $configurationFilePath = $this->container
        ->getParameter('app.root') . \DIRECTORY_SEPARATOR . 'core';
    // @todo once PHPUnit 10 is no longer used, remove the condition.
    // @see https://www.drupal.org/project/drupal/issues/3497116
    if (RunnerVersion::getMajor() >= 11) {
        $configurationFilePath .= \DIRECTORY_SEPARATOR . '.phpunit-next.xml';
    }
    $phpUnitTestDiscovery = new PhpUnitTestDiscovery($configurationFilePath);
    $phpUnitList = $phpUnitTestDiscovery->getTestClasses($extension, $suites, $directory);
    // Downgrade results to make them comparable, working around bugs and
    // additions.
    // 1. Remove TestDiscovery empty groups.
    $internalList = array_filter($internalList);
    // 2. Remove TestDiscovery '##no-group-annotations' group.
    unset($internalList['##no-group-annotations']);
    // 3. Remove 'file' and 'tests_count' keys from PHPUnit results.
    foreach ($phpUnitList as &$group) {
        foreach ($group as &$testClass) {
            unset($testClass['file']);
            unset($testClass['tests_count']);
        }
    }
    // 4. Remove from PHPUnit results groups not found by TestDiscovery.
    $phpUnitList = array_intersect_key($phpUnitList, $internalList);
    // 5. Remove from PHPUnit groups classes not found by TestDiscovery.
    foreach ($phpUnitList as $groupName => &$group) {
        $group = array_intersect_key($group, $internalList[$groupName]);
    }
    // 6. Remove from PHPUnit test classes groups not found by TestDiscovery.
    foreach ($phpUnitList as $groupName => &$group) {
        foreach ($group as $testClassName => &$testClass) {
            $testClass['groups'] = array_intersect_key($testClass['groups'], $internalList[$groupName][$testClassName]['groups']);
        }
    }
    $this->assertEquals($internalList, $phpUnitList);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.