Separate the construction process in your chain from the actual constructor call by providing a corresponding factory class for each of your handler classes. Each factory constructor stays parameterless, but inside each factory you create the modules with the required services injected.
By using using a strict naming scheme like FirstHandlerFactory
for FirstHandler
, SecondHandlerFactory
for SecondHandler
etc, the resulting code will then look like
foreach ( $modulesQueue as $moduleName ) {
$factoryName = "extensions\\".$moduleName."Factory";
$startModule->setSuccessor((new $factoryName())->buildModule());
}
The buildModule
function then may look like this
class FirstHandlerFactory
{
function buildModule()
{
return new FirstHandler(/* provide the individual services here */);
}
}
In your unit tests, you will be able to create your handler objects just with mock services injected, as required for the specific test, without using the factories.