Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for Optimization Detective #1817

Merged
merged 9 commits into from
Jan 23, 2025

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jan 21, 2025

Summary

This is part of #1789:

  • Ignore Coverage for Non-Critical Code Blocks
  • Add Missing @covers Annotations
  • Add Missing Tests
Before: 77.15% ⚠️ After: 93.99% ✅
image image
image image

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature skip changelog PRs that should not be mentioned in changelogs labels Jan 21, 2025
Copy link

codecov bot commented Jan 21, 2025

Codecov Report

Attention: Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 65.42%. Comparing base (762e52a) to head (275d102).
Report is 49 commits behind head on trunk.

Files with missing lines Patch % Lines
...imization-detective/class-od-strict-url-metric.php 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1817      +/-   ##
==========================================
+ Coverage   60.83%   65.42%   +4.58%     
==========================================
  Files          86       85       -1     
  Lines        6756     6738      -18     
==========================================
+ Hits         4110     4408     +298     
+ Misses       2646     2330     -316     
Flag Coverage Δ
multisite 65.42% <80.00%> (+4.58%) ⬆️
single 37.93% <30.00%> (+0.42%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -346,6 +363,9 @@ public function test_add_link( array $links_args, string $expected_html, string
}

$collection = new OD_Link_Collection();

$this->assertNull( $collection->get_response_header() );
Copy link
Member Author

Choose a reason for hiding this comment

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

This call was added to improve test coverage and it exposed error in PHP 7.2/7.3 which is fixed in 921c2d3

@westonruter westonruter marked this pull request as ready for review January 22, 2025 22:55
Copy link

github-actions bot commented Jan 22, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@westonruter This looks great, thanks for all the additional coverage! Just a few non-blocking thoughts.

Comment on lines -49 to +68
private static function set_additional_properties_to_false( $schema ) {
if ( ! isset( $schema['type'] ) ) {
return $schema;
}

private static function set_additional_properties_to_false( array $schema ): array {
$type = (array) $schema['type'];

if ( in_array( 'object', $type, true ) ) {
if ( isset( $schema['properties'] ) ) {
foreach ( $schema['properties'] as $key => $child_schema ) {
$schema['properties'][ $key ] = self::set_additional_properties_to_false( $child_schema );
if ( isset( $child_schema['type'] ) ) {
$schema['properties'][ $key ] = self::set_additional_properties_to_false( $child_schema );
}
}
}

if ( isset( $schema['patternProperties'] ) ) {
foreach ( $schema['patternProperties'] as $key => $child_schema ) {
$schema['patternProperties'][ $key ] = self::set_additional_properties_to_false( $child_schema );
if ( isset( $child_schema['type'] ) ) {
$schema['patternProperties'][ $key ] = self::set_additional_properties_to_false( $child_schema );
Copy link
Member

Choose a reason for hiding this comment

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

What's the rationale for this change? How was the previous implementation problematic?

Copy link
Member Author

Choose a reason for hiding this comment

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

There was no code coverage for the return in:

		if ( ! isset( $schema['type'] ) ) {
			return $schema;
		}

In practice, the $schema should always have a type specified given it is enforced by \OD_URL_Metric::extend_schema_with_optional_properties(). At the entrypoint where this method is called in \OD_Strict_URL_Metric::get_json_schema() it is absolutely defined, which the new typing makes clear. But there is less certainty about the nested properties, so this just moves the isset(...['type']) check down below, which has the same effect but also improves coverage.

Comment on lines +15 to 18
// The addition of the following hooks is tested in Test_OD_Hooks::test_hooks_added() and Test_OD_Storage_Post_Type::test_add_hooks().

// @codeCoverageIgnoreStart
add_action( 'init', 'od_initialize_extensions', PHP_INT_MAX );
Copy link
Member

Choose a reason for hiding this comment

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

Too bad, I guess there is no way to manually indicate that this is covered by the tests for the hooks added?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the only way may be to have a test that removes all filters/actions, and then does a require on the hooks.php file. If after doing so all of the filters/actions have been added, then we know it worked. This would work for Optimization Detective, but it wouldn't work for other plugins that actually define functions in hooks.php. So maybe we should consider that later as part of #1789.

Note that the coverage of uninstall.php is handled similarly: https://github.com/WordPress/performance/blob/trunk/plugins/optimization-detective/tests/test-uninstall.php

@westonruter westonruter merged commit b9154cc into trunk Jan 23, 2025
13 checks passed
@westonruter westonruter deleted the update/od-test-coverage branch January 23, 2025 00:54
@westonruter westonruter added [Plugin] Optimization Detective Issues for the Optimization Detective plugin and removed skip changelog PRs that should not be mentioned in changelogs labels Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: Done 😃
Development

Successfully merging this pull request may close these issues.

2 participants