Testing¶
Two suites, both on PHPUnit 9.
| Suite | Config | Needs | Command |
|---|---|---|---|
| Unit | phpunit.unit.xml, bootstrap tests/Unit/bootstrap.php | nothing (no WordPress, no database) | composer test:unit |
| Integration | phpunit.xml, bootstrap tests/bootstrap.php | Docker (subscribed/wp-testing-kit: WordPress test library, WooCommerce, WooCommerce Subscriptions from the dev dependency) | composer test |
| Live API | phpunit.external.xml | the same Docker environment plus KLAVIYO_API_PRIVATE_KEY, KLAVIYO_API_PUBLIC_KEY, KLAVIYO_LISTS_SERVICE | composer test:external |
composer test:all runs unit + integration. Run the unit suite before and after every change; run the integration suite too when you touch anything that talks to WordPress or WooCommerce.
Unit suite¶
tests/Unit/bootstrap.php stubs the WordPress functions the classes call at construction (add_action, add_filter, apply_filters, wc_get_logger) and the klaviyo_add_consents_by_email() helper. Every stub records its calls in $GLOBALS['klaviyo_unit_calls'], so a test can assert on them. Stand-ins for WC_Order, WC_Subscription, WC_Product and WC_Order_Item_Product exist for type hints; mock the methods you need with onlyMethods().
| Test file | Covers |
|---|---|
Unit/Watchers/OrdersFlowCheckoutTest.php | Orders::on_flow_checkout_parent_order() (base-plugin flow-checkout/subscription/parent-order) |
Unit/Watchers/PluginsEventAPIProvisionerAddonTest.php | PluginsEventAPIProvisioner::subscription_addons__item_deleted() (base-plugin base-plugin/subscription/addon-product/item-deleted); skips without base-plugin in vendor/ |
Unit/Trackers/CheckoutTrackingMarketingPermissionsTest.php | CheckoutTracking::on_marketing_permissions() (base-plugin flow-checkout/subscription/marketing-permissions) |
Integration suite¶
Settings live in .env.testing. tests/integration-setup.php (kit BOOTSTRAP_EXTRA) loads WooCommerce Subscriptions from vendor/wp-plugins/woocommerce-subscriptions after WooCommerce. tests/Unit is excluded from this suite.
| Test file | Covers |
|---|---|
API/APIConfigTest.php | KlaviyoAPIConfig |
API/Objects/Event/EventTest.php | Event object |
API/Objects/Event/Metric/MetricTest.php | Metric object |
API/Objects/Lists/Subscribe/*Test.php | List subscription request objects |
API/Objects/Profile/*Test.php | Profile request objects |
API/Responses/ResponseDataTest.php | API response parsing |
API/KlaviyoAPIFacadeTest.php talks to the real Klaviyo API and is in group external (see table above).
Writing new tests¶
- Prefer the unit suite. Put tests in
tests/Unit/mirroring the source path underinc/. - Use the integration suite only for code that genuinely needs WordPress or WooCommerce.
- Extend
PHPUnit\Framework\TestCase. Mock external dependencies. Never put credentials in a test; read them withgetenv()and skip when absent.