HttpTestTrait - Define helper methods for E2E tests of Civi's HTTP routes #19600
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This introduces some programmatic helpers to facilitate end-to-end testing of Civi's HTTP routes.
Technical Details
Here's a basic example which attempts to fetch the
civicrm/dashboard
route -- and which assertsThere are a couple of main helpers being used in this example:
$http->get('civicrm/dashboard')
will resolve to a full URL likehttp://localhost:8001/drupal/civicrm/dashboard
. It specifically supports these URL schemes:route://ROUTE_NAME
(aka)route:ROUTE_NAME
var://PATH_EXPRESSION
(aka)var:PATH_EXPRESSION
ext://EXTENSION/FILE
(aka)ext:EXTENSION/FILE
assetBuilder://ASSET_NAME?PARAMS
(aka)assetBuilder:ASSET_NAME?PARAMS
auto://ROUTE_NAME_OR_PATH_EXPRESSION
auto://
. More details inCRM_Utils_GuzzleMiddleware::url()
.assertStatusCode()
orassertContentType()
). These assertions are more informative than the typicalassertEquals()
. If you useassertEquals()
, then it only reports one datum in isolation (eg the status code). With the extended assertion, the failure will report more detailed context (ie the HTTP request and HTTP response).A few more notes:
GuzzleTestTrait
. My understanding is that itHttpTestTrait
andGuzzleTestTrait
serve different purposes:GuzzleTestTrait
is used for cases like payment-processing -- wherein Civi acts as a client to some remote service. Civi makes outbound requests through Guzzle. One instruments mocking into Guzzle to see that outbound requests meet assertions and to simulate their responses.HttpTestTrait
is used for E2E-style tests ofauthx
andafform
public form-submissions -- wherein Civi acts as a server. Our test needs to send real HTTP requests to Civi. These requests also go through Guzzle, but we don't want to mock them. We just want to reduce some tedium in sending them.Comments
Anything else you would like the reviewer to note