Register all of /tests/src/ for class loading

Created on 16 January 2022, over 2 years ago
Updated 18 March 2024, 6 months ago

Problem/Motivation

When running phpunit tests for a module, if that module does not have its own composer.json setup, Drupal will register autoloading for the test namespace.

The details are in drupal_phpunit_get_extension_namespaces() and drupal_phpunit_populate_class_loader().

The following namespaces directories are registered:

  • "Drupal\\Tests\\$extension\\Unit\\" -> /src/tests/Unit/.
  • "Drupal\\Tests\\$extension\\Kernel\\" -> /src/tests/Kernel/.
  • "Drupal\\Tests\\$extension\\Functional\\" -> /src/tests/Functional/.
  • "Drupal\\Tests\\$extension\\Build\\" -> /src/tests/Build/.
  • "Drupal\\Tests\\$extension\\FunctionalJavascript\\" -> /src/tests/FunctionalJavascript/.
  • "Drupal\\Tests\\$extension\\Traits\\" -> /src/tests/Traits/.

However, not having the top-level namespace registered for class loading means that we cannot easily use helper classes.

See https://drupal.stackexchange.com/questions/309143/autoload-helper-classe...

Steps to reproduce

Create a module with tests.
Have one of the tests use a trait in /tests/src/Traits/.
Have another test use a helper class in /tests/src/Helper/.
Run the tests from a Drupal installation, where the module is in a subdirectory.
E.g. "./vendor/bin/phpunit web/modules/custom/mymodule/tests/src/Kernel/MyKernelTest.php".

Result:
- The test using the trait works, because the trait is correctly autoloaded.
- The test using the helper class will break, because the helper class cannot be autoloaded.

Proposed resolution

Register all of \Drupal\\Tests\\$extension at /src/tests/ so any sub-namespace can be found, not just the predefined list above.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Fixed

Version

10.3 ✨

Component
PHPUnit  →

Last updated about 4 hours ago

Created by

🇩🇪Germany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot → tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

  • 🇮🇳India Nikhil_110

    Attached patch against Drupal 10.1.x

    Patch #7 is not applied for Drupal 10.1.x so Inter-diff file is not added.

    Checking patch core/lib/Drupal/Core/Test/TestDiscovery.php...
    error: while searching for:
    $this->testNamespaces["Drupal\\$name\\Tests\\"][] = "$base_path/src/Tests";

    // Add PHPUnit test namespaces.
    $this->testNamespaces["Drupal\\Tests\\$name\\Unit\\"][] = "$base_path/tests/src/Unit";
    $this->testNamespaces["Drupal\\Tests\\$name\\Kernel\\"][] = "$base_path/tests/src/Kernel";
    $this->testNamespaces["Drupal\\Tests\\$name\\Functional\\"][] = "$base_path/tests/src/Functional";
    $this->testNamespaces["Drupal\\Tests\\$name\\Build\\"][] = "$base_path/tests/src/Build";
    $this->testNamespaces["Drupal\\Tests\\$name\\FunctionalJavascript\\"][] = "$base_path/tests/src/FunctionalJavascript";

    // Add discovery for traits which are shared between different test
    // suites.
    $this->testNamespaces["Drupal\\Tests\\$name\\Traits\\"][] = "$base_path/tests/src/Traits";
    }

    foreach ($this->testNamespaces as $prefix => $paths) {

    error: patch failed: core/lib/Drupal/Core/Test/TestDiscovery.php:102
    error: core/lib/Drupal/Core/Test/TestDiscovery.php: patch does not apply
    Checking patch core/tests/bootstrap.php...
    Hunk #1 succeeded at 79 (offset -1 lines).
    Hunk #2 succeeded at 87 (offset -1 lines).

  • last update over 1 year ago
    29,204 pass
  • Status changed to Needs review over 1 year ago
  • 🇺🇸United States smustgrave

    Needs work per #7/ #3

  • Status changed to Needs work 11 months ago
  • 🇦🇺Australia craigra

    I've tested the patch in #11 on Drupal 10.2 and confirmed that this fixes the issue with test classes being unable to find a helper class under tests/src/Helper within a module test directory, as described in the steps to reproduce. Similarly I've verified this for theme and profile test directories.

    What additional work is needed for core test locations (#7), and could this be split to another issue so the existing fix for modules, themes and profiles can be RTBC?

  • Merge request !6870Simplify test namespaces. → (Closed) created by longwave
  • 🇬🇧United Kingdom longwave UK

    I don't actually think we can do much about the core namespaces, because they sit directly under \Drupal and we don't want to go looking in core/tests/Drupal for non-test Drupal code.

    Converted #11 to an MR, inlined a variable for readability, and updated the IS with the proposed solution.

  • Status changed to Needs review 7 months ago
  • 🇬🇧United Kingdom longwave UK

    Hiding patches.

  • Pipeline finished with Failed
    7 months ago
    Total: 3337s
    #109226
  • Status changed to Needs work 7 months ago
  • 🇺🇸United States smustgrave

    Seems to have a test failure.

  • Status changed to Needs review 7 months ago
  • 🇬🇧United Kingdom longwave UK

    Just needed rebase.

  • Status changed to RTBC 7 months ago
  • 🇺🇸United States smustgrave

    Sweet. Change didn’t break anything

    • catch → committed d39f152b on 10.3.x
      Issue #3258817 by longwave, donquixote: Register all of /tests/src/ for...
    • catch → committed 7c6547de on 11.x
      Issue #3258817 by longwave, donquixote: Register all of /tests/src/ for...
  • 🇬🇧United Kingdom catch

    Committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

  • Status changed to Fixed 7 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024