Skip to content

Conversation

@butschster
Copy link
Contributor

This PR introduces the TestCaseFactory, providing developers with complete control over how test case instances are created. Previously, test classes were instantiated using basic reflection, limiting support for dependency injection and complex initialization scenarios.

What's New

Three Implementation Strategies:

  1. ReflectionFactory (default) - Simple reflection-based instantiation
  2. ContainerFactory - PSR-11 container integration for dependency injection
  3. CallableFactory - Custom factory logic via user-defined closures

💡 Motivation

Problem

Test classes with constructor dependencies couldn't be instantiated:

final class UserRepositoryTest 
{
    // ❌ This previously failed - no way to inject dependencies
    public function __construct(
        private DatabaseConnection $db,
        private UserRepository $repository,
    ) {}
}

Solution

Now developers can configure a custom factory:

// Example 1: Using PSR-11 Container
$container = new Container();
$container->set(DatabaseConnection::class, $dbConnection);

$app = Application::createFromConfig(
    config: $config,
    testCaseFactory: new ContainerFactory($container)
);

// Example 2: Custom Factory Logic
$app = Application::createFromConfig(
    config: $config,
    testCaseFactory: new CallableFactory(
        fn(\ReflectionClass $class) => $myFactory->create($class->getName())
    )
);

… to give

developers full control over test case instantiation. This enables
dependency injection, custom initialization logic, and integration with
existing DI containers.

Features:
- ReflectionFactory: Simple reflection-based instantiation (default)
- ContainerFactory: PSR-11 container integration for DI support
- CallableFactory: Custom factory logic via user-defined closures
@butschster butschster requested a review from a team as a code owner January 11, 2026 12:35
@butschster butschster added the enhancement New feature or request label Jan 11, 2026
@butschster butschster marked this pull request as draft January 11, 2026 12:36
@butschster butschster linked an issue Jan 11, 2026 that may be closed by this pull request
@butschster butschster marked this pull request as ready for review January 11, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test class instantiation

2 participants