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());
}
In your tests, you create your handler objects just with mock services injected, as required for the specific test, without using the factories.