google / google/site-kit-wp

Improve the test coverage for the `fpm-server-requirement-status` endpoint.

Open
#9,708 0 comments 0 reactions 0 assignees View on GitHub
P2 Type: Infrastructure
Dominant language
JavaScript
Stars
1.4k
Forks
383
Avg merge
4d 14h
Merged PRs (30d)
77

Description

## Feature Description

The initial implementation of the test coverage for the `fpm-server-requirement-status` (see https://github.com/google/site-kit-wp/issues/9632) involved mocking the `is_endpoint_healthy()` protected method of the `REST_First_Party_Mode_Controller`, due to difficulty testing the code contained within the method.

Notably, although it's possible to mock `file_get_contents()`, it doesn't appear possible to mock the `$http_response_header` that gets set locally in the `file_get_contents()` caller's scope with off the shelf tooling.

https://github.com/google/site-kit-wp/blob/54f900c534c135682d44e4dfa8834e3891d91ff2/tests/phpunit/integration/Core/Tags/First_Party_Mode/REST_First_Party_Mode_ControllerTest.php#L235-L241

We should improve the test coverage to ensure the code contained in `is_endpoint_healthy()` is covered. Possible angles include:

- Refactoring `REST_First_Party_Mode_Controller` so we pass in say a Guzzle client instance, or our own wrapper to `file_get_contents()`, and use that rather than calling `file_get_contents()` directly, allowing us to pass in a mock client during testing and avoid mocking `is_endpoint_healthy()`.
- Adding E2E test coverage for the endpoint. This comes with its own challenges, notably mocking the health check to `https://g-1234.fps.goog/mpath/healthy` to avoid calling the real Google service from our E2E tests. It looks possible, but would involve some structural changes to our E2E test environment; this was explored during the implementation of 9632, see https://github.com/google/site-kit-wp/pull/9707. A more practical approach requiring less structural changes would be to provide filters for the health check URLs so we could override them during testing. However, the `REST_First_Party_Mode_Controller` described above is probably the best way to proceed here.

---------------

_Do not alter or remove anything below. The following sections will be managed by moderators only._

## Acceptance criteria

*

## Implementation Brief

* [ ]

### Test Coverage

*

## QA Brief

*

## Changelog entry

*

Contributor guide

Open the contributing guide

Research direction

Start with tests/phpunit/integration/Core/Tags/First_Party_Mode/REST_First_Party_Mode_ControllerTest.php around lines 235-241 and inspect REST_First_Party_Mode_Controller::is_endpoint_healthy(). Compare the wrapper/client and E2E approaches described in the issue; done means the method's health-check paths are covered without mocking the protected method and the endpoint tests avoid calling the real Google service.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.