Skip to content

Support PHPUnit TestSuite factories when constructors are private #1925

Description

@chubes4

Summary

The generated wordpress.phpunit Playground harness crashes during load_tests with PHPUnit 10+ because it directly constructs PHPUnit\Framework\TestSuite, whose constructor is private in those versions.

Downstream report: Extra-Chill/homeboy-extensions#2258.

Reproduction

A Homeboy WordPress release gate using WP Codebox 0.12.14 installed WordPress and dependencies and discovered 46 *Test.php files, then failed before any component test ran:

STAGE_FATAL:load_tests:Uncaught Error: Call to private PHPUnit\Framework\TestSuite::__construct() from global scope in /internal/eval.php:1096

The defect remains on current main at both generated harness sites:

  • packages/runtime-playground/src/phpunit-command-handlers.ts:1273 (phpunitRunCode)
  • packages/runtime-playground/src/phpunit-command-handlers.ts:1592 (corePhpunitRunCode)

Both emit new PHPUnit\Framework\TestSuite(...).

Compatibility approach

Use the public factory when available, preserving PHPUnit 9 compatibility:

$suite = method_exists(PHPUnit\Framework\TestSuite::class, 'empty')
    ? PHPUnit\Framework\TestSuite::empty('WP Codebox PHPUnit Tests')
    : new PHPUnit\Framework\TestSuite('WP Codebox PHPUnit Tests');

PHPUnit 10 through current PHPUnit expose TestSuite::empty() and keep the constructor private. PHPUnit 9 has a public constructor and no empty() factory.

The discovered-class path also currently calls $suite->addTestSuite($class_name). PHPUnit 10+ requires a ReflectionClass; PHPUnit 9 accepts either a class name or ReflectionClass, so passing the existing $ref is compatible across both API generations.

Acceptance criteria

  • Update plugin and WordPress-core generated harnesses.
  • Add generated-code assertions covering the public factory fallback.
  • Add focused execution coverage with a private-constructor TestSuite double proving discovered test files reach execution.
  • Preserve distinct STAGE_FAIL / STAGE_FATAL infrastructure reporting versus test assertion failures.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions