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

Add @cover annotation in test-load file and add support for codecov #1586

Merged
merged 10 commits into from
Nov 13, 2024
39 changes: 39 additions & 0 deletions .github/workflows/codecov-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PHP Test and Coverage
sarthak-19 marked this conversation as resolved.
Show resolved Hide resolved

on: [push, pull_request]

jobs:
php-test:
runs-on: ubuntu-latest
name: WordPress Test Coverage
strategy:
fail-fast: false
matrix:
php: ['8.2', '8.1', '8.0', '7.4', '7.3', '7.2']
wp: [ 'latest' ]
include:
- php: '7.4'
wp: '6.5'
- php: '8.3'
wp: 'trunk'
env:
WP_ENV_PHP_VERSION: ${{ matrix.php }}
WP_ENV_CORE: ${{ matrix.wp == 'trunk' && 'WordPress/WordPress' || format( 'https://wordpress.org/wordpress-{0}.zip', matrix.wp ) }}
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, intl, curl, dom, json, xml, zip
- name: Install Composer dependencies
run: composer install --no-interaction --no-progress
- name: Run PHPUnit tests
run: vendor/bin/phpunit --coverage-clover=coverage.xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.xml
flags: unittests
name: ${{ matrix.php }}-coverage
15 changes: 15 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
codecov:
notify:
require_ci_to_pass: yes
coverage:
status:
project:
default:
target: auto
threshold: 80%
base: auto
informational: true
patch:
default:
threshold: 80%
informational: true
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
"wp-coding-standards/wpcs": "^3.1",
"wp-phpunit/wp-phpunit": "^6.5",
"yoast/phpunit-polyfills": "^2.0",
"phpstan/php-8-stubs": "^0.3.84",
"phpstan/phpstan-strict-rules": "^1.6"
"phpstan/php-8-stubs": "^0.4.0",
"phpstan/phpstan-strict-rules": "^1.6",
"phpunit/phpunit": "^9.5",
sarthak-19 marked this conversation as resolved.
Show resolved Hide resolved
"codecov/codecov": "^1.0"
sarthak-19 marked this conversation as resolved.
Show resolved Hide resolved
},
"config": {
"allow-plugins": {
Expand Down Expand Up @@ -89,7 +91,7 @@
"lint:web-worker-offloading": "@lint -- ./plugins/web-worker-offloading --standard=./plugins/web-worker-offloading/phpcs.xml.dist",
"lint:webp-uploads": "@lint -- ./plugins/webp-uploads --standard=./plugins/webp-uploads/phpcs.xml.dist",
"phpstan": "phpstan analyse --memory-limit=2048M",
"test": "phpunit",
"test": "phpunit --strict-coverage",
"test-multisite": [
"WP_MULTISITE=1 phpunit --exclude-group=ms-excluded"
],
Expand Down
71 changes: 71 additions & 0 deletions plugins/webp-uploads/tests/test-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static function ( string $filename ) {
/**
* Don't create the original mime type for JPEG images.
*
* @covers ::wp_get_attachment_metadata
* @covers ::wp_get_original_image_path
* @covers ::get_post_mime_type
* @dataProvider data_provider_supported_image_types
*/
public function test_it_should_not_create_the_original_mime_type_for_jpeg_images( string $image_type ): void {
Expand Down Expand Up @@ -79,6 +82,9 @@ public function test_it_should_not_create_the_original_mime_type_for_jpeg_images

/**
* Create the original mime type for WebP images.
*
* @covers ::wp_get_attachment_metadata
* @covers ::get_post_mime_type
*/
public function test_it_should_create_the_original_mime_type_as_well_with_all_the_available_sources_for_the_specified_mime(): void {
update_option( 'perflab_generate_webp_and_jpeg', false );
Expand Down Expand Up @@ -109,6 +115,8 @@ public function test_it_should_create_the_original_mime_type_as_well_with_all_th
/**
* Create JPEG and output type for JPEG images, if opted in.
*
* @covers ::wp_get_attachment_metadata
* @covers ::get_post_mime_type
* @dataProvider data_provider_supported_image_types
*/
public function test_it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in( string $image_type ): void {
Expand Down Expand Up @@ -145,6 +153,8 @@ public function test_it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in(
/**
* Create JPEG and output format for JPEG images, if perflab_generate_webp_and_jpeg option set.
*
* @covers ::wp_get_attachment_metadata
* @covers ::get_post_mime_type
* @dataProvider data_provider_supported_image_types
*/
public function test_it_should_create_jpeg_and_webp_for_jpeg_images_if_generate_webp_and_jpeg_set( string $image_type ): void {
Expand Down Expand Up @@ -181,6 +191,9 @@ public function test_it_should_create_jpeg_and_webp_for_jpeg_images_if_generate_

/**
* Don't create the sources property if no transform is provided.
*
* @covers ::wp_get_attachment_metadata
* @covers ::assertArrayNotHasKey
*/
public function test_it_should_not_create_the_sources_property_if_no_transform_is_provided(): void {
add_filter( 'webp_uploads_upload_image_mime_transforms', '__return_empty_array' );
Expand All @@ -200,6 +213,8 @@ public function test_it_should_not_create_the_sources_property_if_no_transform_i

/**
* Create the sources property when no transform is available
*
* @covers ::wp_get_attachment_metadata
*/
public function test_it_should_create_the_sources_property_when_no_transform_is_available(): void {
add_filter(
Expand All @@ -225,6 +240,9 @@ static function () {

/**
* Not create the sources property if the mime is not specified on the transforms images
*
* @covers ::wp_get_attachment_metadata
* @covers ::assertArrayNotHasKey
*/
public function test_it_should_not_create_the_sources_property_if_the_mime_is_not_specified_on_the_transforms_images(): void {
add_filter(
Expand All @@ -249,6 +267,9 @@ static function () {

/**
* Create a WebP version with all the required properties
*
* @covers ::wp_get_attachment_metadata
* @covers ::assertFileExists
*/
public function test_it_should_create_a_webp_version_with_all_the_required_properties(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
Expand All @@ -275,6 +296,8 @@ public function test_it_should_create_a_webp_version_with_all_the_required_prope

/**
* Create the full size images when no size is available
*
* @covers ::wp_get_attachment_metadata
*/
public function test_it_should_create_the_full_size_images_when_no_size_is_available(): void {
add_filter( 'intermediate_image_sizes', '__return_empty_array' );
Expand All @@ -291,6 +314,9 @@ public function test_it_should_create_the_full_size_images_when_no_size_is_avail

/**
* Remove `scaled` suffix from the generated filename
*
* @covers ::get_attached_file
* @covers ::wp_get_attachment_metadata
*/
public function test_it_should_remove_scaled_suffix_from_the_generated_filename(): void {
// Create JPEG and WebP to check for scaled suffix.
Expand All @@ -316,6 +342,9 @@ static function () {

/**
* Remove the generated webp images when the attachment is deleted
*
* @covers ::get_attached_file
* @covers ::wp_get_attachment_metadata
*/
public function test_it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
Expand Down Expand Up @@ -349,6 +378,9 @@ public function test_it_should_remove_the_generated_webp_images_when_the_attachm

/**
* Remove the attached WebP version if the attachment is force deleted
*
* @covers ::wp_delete_attachment
* @covers ::wp_get_attachment_metadata
*/
public function test_it_should_remove_the_attached_webp_version_if_the_attachment_is_force_deleted(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
Expand All @@ -374,6 +406,9 @@ public function test_it_should_remove_the_attached_webp_version_if_the_attachmen

/**
* Remove full size images when no size image exists
*
* @covers ::wp_delete_attachment
* @covers ::wp_get_attachment_metadata
*/
public function test_it_should_remove_full_size_images_when_no_size_image_exists(): void {
add_filter( 'intermediate_image_sizes', '__return_empty_array' );
Expand All @@ -397,6 +432,10 @@ public function test_it_should_remove_full_size_images_when_no_size_image_exists

/**
* Remove the attached WebP version if the attachment is force deleted after edit.
*
* @covers ::wp_delete_attachment
* @covers ::wp_get_attachment_metadata
* @covers ::get_post_meta
*/
public function test_it_should_remove_the_backup_sizes_and_sources_if_the_attachment_is_deleted_after_edit(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
Expand Down Expand Up @@ -429,6 +468,7 @@ public function test_it_should_remove_the_backup_sizes_and_sources_if_the_attach
/**
* Avoid the change of URLs of images that are not part of the media library
*
* @covers ::webp_uploads_update_image_references
* @group webp_uploads_update_image_references
*/
public function test_it_should_avoid_the_change_of_urls_of_images_that_are_not_part_of_the_media_library(): void {
Expand All @@ -443,6 +483,7 @@ public function test_it_should_avoid_the_change_of_urls_of_images_that_are_not_p
/**
* Avoid replacing not existing attachment IDs
*
* @covers ::webp_uploads_update_image_references
* @group webp_uploads_update_image_references
*/
public function test_it_should_avoid_replacing_not_existing_attachment_i_ds(): void {
Expand All @@ -457,6 +498,7 @@ public function test_it_should_avoid_replacing_not_existing_attachment_i_ds(): v
/**
* Prevent replacing a WebP image
*
* @covers ::webp_uploads_update_image_references
* @group webp_uploads_update_image_references
*/
public function test_it_should_prevent_replacing_a_webp_image(): void {
Expand Down Expand Up @@ -485,6 +527,8 @@ public function test_it_should_prevent_replacing_a_webp_image(): void {

/**
* Prevent replacing a jpg image if the image does not have the target class name
*
* @covers ::webp_uploads_update_image_references
*/
public function test_it_should_prevent_replacing_a_jpg_image_if_the_image_does_not_have_the_target_class_name(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
Expand All @@ -502,6 +546,8 @@ public function test_it_should_prevent_replacing_a_jpg_image_if_the_image_does_n
/**
* Replace references to a JPG image to a WebP version
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
* @dataProvider provider_replace_images_with_different_extensions
* @group webp_uploads_update_image_references
*/
Expand Down Expand Up @@ -530,6 +576,8 @@ public function test_it_should_replace_references_to_a_jpg_image_to_a_webp_versi
/**
* Should not replace jpeg images in the content if other mime types are disabled via filter.
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
* @dataProvider provider_replace_images_with_different_extensions
* @group webp_uploads_update_image_references
*/
Expand Down Expand Up @@ -559,6 +607,9 @@ public function provider_replace_images_with_different_extensions(): Generator {

/**
* Replace all the images including the full size image
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
*/
public function test_it_should_replace_all_the_images_including_the_full_size_image(): void {
// Create JPEG and WebP to check replacement of JPEG => WebP.
Expand Down Expand Up @@ -586,6 +637,8 @@ public function test_it_should_replace_all_the_images_including_the_full_size_im
/**
* Prevent replacing an image with no available sources
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
* @group webp_uploads_update_image_references
*/
public function test_it_should_prevent_replacing_an_image_with_no_available_sources(): void {
Expand All @@ -602,6 +655,8 @@ public function test_it_should_prevent_replacing_an_image_with_no_available_sour
/**
* Prevent update not supported images with no available sources
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
* @dataProvider data_provider_not_supported_webp_images
* @group webp_uploads_update_image_references
*/
Expand Down Expand Up @@ -654,6 +709,8 @@ static function () {

/**
* Tests that we can force generating jpeg subsizes using the webp_uploads_upload_image_mime_transforms filter.
*
* @covers \Test_WebP_Uploads_Load::opt_in_to_jpeg_and_webp
*/
public function test_it_should_preserve_jpeg_subsizes_using_transform_filter(): void {
// Create JPEG and WebP.
Expand All @@ -674,6 +731,8 @@ public function test_it_should_preserve_jpeg_subsizes_using_transform_filter():
/**
* Allow the upload of an image if at least one editor supports the image type
*
* @covers ::wp_image_editor_supports
* @covers ::set_post_thumbnail
* @dataProvider data_provider_supported_image_types
*
* @group current1
Expand Down Expand Up @@ -716,6 +775,8 @@ static function ( $editors ) {
/**
* Replace the featured image to the proper type when requesting the featured image.
*
* @covers ::get_the_post_thumbnail
* @covers ::set_post_thumbnail
* @dataProvider data_provider_supported_image_types
*/
public function test_it_should_replace_the_featured_image_to_webp_when_requesting_the_featured_image( string $image_type ): void {
Expand Down Expand Up @@ -752,6 +813,7 @@ public function data_provider_supported_image_types(): array {
/**
* Prevent replacing an image if image was uploaded via external source or plugin.
*
* @covers ::webp_uploads_update_image_references
* @group webp_uploads_update_image_references
*/
public function test_it_should_prevent_replacing_an_image_uploaded_via_external_source(): void {
Expand All @@ -774,6 +836,9 @@ static function () {

/**
* The image with the smaller filesize should be used when webp_uploads_discard_larger_generated_images is set to true.
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
*/
public function test_it_should_create_webp_when_webp_is_smaller_than_jpegs(): void {
// Create JPEG and WebP.
Expand Down Expand Up @@ -813,6 +878,9 @@ public function test_it_should_create_webp_when_webp_is_smaller_than_jpegs(): vo

/**
* The image with the smaller filesize should be used when webp_uploads_discard_larger_generated_images is set to true.
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
*/
public function test_it_should_create_webp_for_full_size_which_is_smaller_in_webp_format(): void {
// Create JPEG and WebP.
Expand Down Expand Up @@ -840,6 +908,9 @@ public function test_it_should_create_webp_for_full_size_which_is_smaller_in_web

/**
* The image with the smaller filesize should be used when webp_uploads_discard_larger_generated_images is set to true.
*
* @covers ::webp_uploads_img_tag_update_mime_type
* @covers ::webp_uploads_update_image_references
*/
public function test_it_should_create_webp_for_some_sizes_which_are_smaller_in_webp_format(): void {
// Create JPEG and WebP.
Expand Down