Skip to content

Tests: Add unit test coverage for MC4WP_MailChimp_Subscriber - #855

Open
noruzzamans wants to merge 1 commit into
ibericode:mainfrom
noruzzamans:test/mailchimp-subscriber-unit-tests
Open

noruzzamans wants to merge 1 commit into
ibericode:mainfrom
noruzzamans:test/mailchimp-subscriber-unit-tests

Conversation

@noruzzamans

@noruzzamans noruzzamans commented Sep 17, 2026

Copy link
Copy Markdown

Summary

Adds comprehensive PHPUnit unit test coverage for the MC4WP_MailChimp_Subscriber class (includes/class-mailchimp-subscriber.php).

Why

MC4WP_MailChimp_Subscriber is the core data transfer object representing subscriber payload data sent to Mailchimp API v3 throughout the plugin:

  • MC4WP_List_Data_Mapper (mapping form and integration input into subscriber payloads)
  • MC4WP_Form_Listener (handling newsletter form submissions and lifecycle filters)
  • MC4WP_Integration (processing integration submissions from WooCommerce, Contact Form 7, WPForms, etc.)

Despite being central to subscription processing, the class currently has zero dedicated unit tests in tests/. Adding isolated test coverage ensures its default values, null-filtering behavior, and array conversion remain consistent and regression-free.

What was tested

Introduced tests/MailchimpSubscriberTest.php with 7 tests and 42 assertions covering:

  1. test_default_properties: verifies default values (email_address, status = 'pending', email_type = 'html', empty arrays for interests, merge_fields, tags, marketing_permissions, and nullable properties initialized to null).
  2. test_to_array_default_values: validates that to_array() retains non-null defaults, excludes null properties (ip_signup, language, vip), and skips empty marketing_permissions.
  3. test_to_array_with_custom_values: verifies populated scalar and array properties (email, status, email type, interests, merge fields, tags) are correctly serialized.
  4. test_to_array_includes_marketing_permissions_when_not_empty: verifies that non-empty marketing permissions arrays are preserved in to_array().
  5. test_to_array_with_nullable_fields_populated: verifies that when $ip_signup, $language, and $vip are populated, they are included in to_array().
  6. test_to_array_filters_out_explicit_null_values: verifies setting any property explicitly to null excludes it from to_array().
  7. test_to_array_preserves_falsy_non_null_values: verifies non-null falsy values (such as $subscriber->vip = false) are preserved in to_array().

Note: This is a 100% test-only contribution with 0 modifications to production code.

Testing Instructions

1. Run the new test suite:

vendor/bin/phpunit tests/MailchimpSubscriberTest.php

Output:

Welcome to the Mailchimp for WordPress Test Suite
PHPUnit 9.6.36 by Sebastian Bergmann and contributors.

.......                                                             7 / 7 (100%)

Time: 00:00.007, Memory: 6.00 MB

OK (7 tests, 42 assertions)

2. Run full regression suite:

vendor/bin/phpunit

Output:

OK (109 tests, 370 assertions)

3. Run coding standards linter:

vendor/bin/phpcs -n -s tests/MailchimpSubscriberTest.php

(0 errors, 0 warnings)

@noruzzamans
noruzzamans marked this pull request as ready for review September 17, 2026 11:43
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.

1 participant