Skip to content

Fix some phpcs issues in merged tests - #1701

Merged
pattonwebz merged 3 commits into
developfrom
william/add-some-tests-for-rest-api-callbacks
May 18, 2026
Merged

Fix some phpcs issues in merged tests#1701
pattonwebz merged 3 commits into
developfrom
william/add-some-tests-for-rest-api-callbacks

Conversation

@pattonwebz

@pattonwebz pattonwebz commented May 18, 2026

Copy link
Copy Markdown
Member

This pull request updates the RestApiEndpointsTest class to improve test coverage and ensure proper visibility for PHPUnit lifecycle methods. The most important changes include making the setUp and tearDown methods public, and enhancing assertions in stats-related test cases to verify the expected structure and keys in the API responses.

Test lifecycle method visibility:

  • Changed the visibility of the setUp and tearDown methods from protected to public in the RestApiEndpointsTest class to comply with PHPUnit 10+ requirements. [1] [2]

Test assertion improvements for API responses:

  • In test_scans_stats_permissions_and_payload, added an assertion to check that the stats array includes the scannable_posts_count key when stats are present.
  • In test_scans_stats_by_post_types_permissions_and_payload, updated the loop to assert that each stat is an array and contains a post_type key, ensuring the structure matches expected output.

Summary by CodeRabbit

  • Tests
    • Enhanced REST API endpoint tests: verify scan stats include a scannable_posts_count when present; strengthen checks for per-post-type stats to ensure each post-type key is a non-empty string and each stat value is either false or an array.

Review Change Stack

…b.com:equalizedigital/accessibility-checker into william/add-some-tests-for-rest-api-callbacks
Copilot AI review requested due to automatic review settings May 18, 2026 12:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Minor test cleanup to address PHPCS issues and align with PHPUnit 10+ requirements, plus stronger assertions on stats endpoints.

Changes:

  • Made setUp/tearDown public for PHPUnit 10+ compliance.
  • Added assertion for scannable_posts_count key in scans-stats response.
  • Tightened per-post-type stats assertions to require array with post_type key.

@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 55981562-fb15-4575-bfe7-caa219ff5aed

📥 Commits

Reviewing files that changed from the base of the PR and between 4025c07 and 6622d54.

📒 Files selected for processing (1)
  • tests/phpunit/includes/classes/RestApiEndpointsTest.php

📝 Walkthrough

Walkthrough

RestApiEndpointsTest makes setUp() and tearDown() public and tightens endpoint assertions: scans-stats now checks scannable_posts_count when stats is non-empty; scans-stats-by-post-types enforces each post_type key is a non-empty string and each stat is either false or an array.

Changes

REST API Test Updates

Layer / File(s) Summary
Test fixture visibility
tests/phpunit/includes/classes/RestApiEndpointsTest.php
setUp() and tearDown() changed from protected to public.
Endpoint response assertions
tests/phpunit/includes/classes/RestApiEndpointsTest.php
scans-stats test asserts scannable_posts_count exists when stats is non-empty. scans-stats-by-post-types test strengthens per-entry checks: each post_type key is a non-empty string and each stat value is either false or an array.

Possibly related PRs

Suggested reviewers

  • SteveJonesDev

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Hop, I nibble at the tests tonight,

public fixtures ready, assertions tight,
counts and types all in a row,
each post_type proud enough to show,
a passing run — what a lovely sight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix some phpcs issues in merged tests' is partially related to the changeset, referring to a real aspect (test lifecycle method visibility changes for PHPUnit compliance), but it doesn't clearly convey that the main changes also include improved test assertions for API response validation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch william/add-some-tests-for-rest-api-callbacks

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/phpunit/includes/classes/RestApiEndpointsTest.php`:
- Around line 326-329: The test loop in RestApiEndpointsTest assumes each $stat
in $data['stats'] contains an embedded 'post_type' key, but the API returns
stats keyed by post type instead; update the assertions to reflect that shape by
asserting $data['stats'] is an array and iterating with keys: foreach
($data['stats'] as $post_type => $stat) { assertIsString($post_type);
assertIsArray($stat); } (replace the assertArrayHasKey('post_type', $stat)
check) so the test validates keyed-by-post_type payloads rather than expecting a
'post_type' field inside each item.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bb8c7695-ff67-485a-9eb7-98d81fd34018

📥 Commits

Reviewing files that changed from the base of the PR and between 1bea54e and 4025c07.

📒 Files selected for processing (1)
  • tests/phpunit/includes/classes/RestApiEndpointsTest.php

Comment thread tests/phpunit/includes/classes/RestApiEndpointsTest.php Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the visibility of test lifecycle methods and refactors assertions for scan statistics REST API endpoints. Feedback indicates that the loop logic for post-type statistics is incorrect based on the API's response structure and should be reverted to correctly handle the associative array. Additionally, a redundant conditional check in the general statistics test should be removed to ensure stricter validation of the returned payload, as the test environment expects data to be present.

Comment on lines 326 to 330
foreach ( $data['stats'] as $stat ) {
$this->assertIsArray( $stat );
// Each stat should have post_type key.
$this->assertArrayHasKey( 'post_type', $stat );
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This loop logic is incorrect and will cause the test to fail. The scans-stats-by-post-types endpoint returns an associative array where the keys are post type slugs and the values are either false (for non-scannable types) or an array of statistics. The statistics array itself does not contain a post_type key. Additionally, use is_array for strict type checking to align with repository standards.

			foreach ( $data['stats'] as $post_type => $stat ) {
				$this->assertIsString( $post_type );
				$this->assertTrue( $stat === false || is_array( $stat ) );
			}
References
  1. When the contract for a variable is a specific type, prefer a stricter type check (e.g., is_array) over a more general one (e.g., is_iterable) to enforce that contract.

Comment on lines +246 to +248
if ( ! empty( $data['stats'] ) ) {
$this->assertArrayHasKey( 'scannable_posts_count', $data['stats'] );
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The if ( ! empty( $data['stats'] ) ) check is redundant and weakens the test. Since the test setup creates a post, the stats array should not be empty. Removing the conditional ensures that the presence of the scannable_posts_count key is always verified, and the test will correctly fail if the API returns an empty response unexpectedly.

		$this->assertArrayHasKey( 'scannable_posts_count', $data['stats'] );

Updated the assertions for the stats structure to ensure each key is a post type slug and each value is either false or a summary array.
@pattonwebz
pattonwebz merged commit 5ee4148 into develop May 18, 2026
18 checks passed
@pattonwebz
pattonwebz deleted the william/add-some-tests-for-rest-api-callbacks branch May 18, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants