Problem/Motivation

Similarly to #1693398: Allow PSR-0 test classes to be used in D7 (followups) we should support D7 modules being forward-thinking and using a PSR-4 directory structure for their tests.

Proposed resolution

Add PSR-4 support to the Simpletest test class discovery.

Remaining tasks

User interface changes

API changes

CommentFileSizeAuthor
#1 2293767-1.patch9.27 KBtstoeckler

Comments

tstoeckler’s picture

Status: Active » Needs review
StatusFileSize
new9.27 KB

Here we go.

sun’s picture

The discovery as well as the loading of both PSR-0 and PSR-4 at the same time could use some profiling/benchmarks.

D7 doesn't have as many tests as D8, but at the same time, D7 has a lot more module directories that need to be scanned (both for discovery and for locating/loading).

tstoeckler’s picture

Not disagreeing, but just wanted to note that the patch builds on the existing behavior in D7. But yes, it doubles the amount of is_dir() checks on discovery and then adds one file_exists() for PSR-0 class files upon autoloading.

Also note that this only happens on test runs, not on every page request.

donquixote’s picture

I'd say we should absolutely wait with this until #2260121: PHPUnit Tests namespace of modules is ambiguous with regular runtime namespace (+ Simpletest tests) is decided.
Every API feature we add to D7 has to be maintained forever, so we should not add stuff that will soon be discarded or changed in D8.

donquixote’s picture

it doubles the amount of is_dir() checks on discovery

This is +1 is_dir() per module, and only on test runs.
I think this is survivable.
If a module does not have a tests/src or tests/Drupal/.., then the scan will simply move on. No subdirectories will be visited, because there are none.

dawehner’s picture

The patch worked fine in order to resolve the actual problem.

I'm not sure whether we really need profiling given that we cache the result anyway. A not small Drupal installation with xdebug on was not incredible slow,
when loading the simpletest UI.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

Performance wise this should be okay,

get_all is really only called at the beginning for discovery.

Code looks good.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +7.36 release notes

Committed to 7.x - thanks!

  • David_Rothstein committed 731a540 on
    Issue #2293767 by tstoeckler: Allow PSR-4 test classes to be used in...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

donquixote’s picture

Did we completely fail to document this anywhere on drupal.org?
I can't find anything!
(searched for "Drupal 7 test classes PSR-4" and "Drupal 7 test classes PSR-0")

opdavies’s picture

There's a reference to it on https://www.drupal.org/docs/7/testing/organizing-your-test-cases#s-where..., but no specific example that I can find.

Alternatively, you can locate your files according to PSR-0, the PHP Framework Interop Group's Autoloading Standard. (See the SimpleTest module code for specifics).

opdavies’s picture

I have it working using PSR-4 with a basic custom module on a plain Drupal 7 installation (no xautoload module or any additional modules). The module is on GitHub (https://github.com/opdavies/drupal7-psr4-autoloading-test-cases) if anyone wants to see it.

This to me definitely seems like it should be documented on https://www.drupal.org/docs/7/testing/organizing-your-test-cases#s-where.... I'm happy to look into updating that page.