Open NightJar opened 1 month ago
Thanks for raising this!
You could work around this by injecting the request: Injector::inst()->registerService(HTTPRequest::class, $request);
Given this is only happening in a unit test and there's an obvious workaround I can't promise the issue will be prioritised any time in the near future.
Yes, I was just about to add this in my own comment. To clarify in the same example from the OP (slightly expanded to include namespace usage changes):
+use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
+use SilverStripe\Core\Injector\Injector;
$admin = new ModelAdmin();
$request = $admin->getRequest();
+ Injector::inst()->registerService($request, HTTPRequest::class);
$request->setSession(new Session([]));
$admin->doInit();
Resulting in
https://github.com/silverstripe/silverstripe-admin/blob/55ad9610ff1095b576168a238654d672cb401576/code/SudoModeController.php#L48
Thanks @GuySartorelli for pointing out (via user's slack) that it's looking for a pre-registered request. Seems like this is a new code execution path in CMS 5, as the error doesn't occur when running tests against CMS 4.
The context of the error is attempting to run a PHPUnit test with a normal "just enough" setup (i.e. it's not production traffic):
Interestingly the offending line is not
getRequest()
as one might think, but ratherdoInit()
The execution path is via
getCombinedClientConfig()
which falls throught to the sudo mode controller that for whatever reason usesInjector::get
rather than$this->getRequest()
. UsinggetRequest
would cause no issue, as per the default request set (but not registered with Injector) inRequestHandler::__construct()
: