ksfraser / mock-woocommerce
WooCommerce mocks, factories, and assertions for unit testing WooCommerce plugins
dev-main
2026-03-22 18:49 UTC
Requires
- php: >=7.3
- ksfraser/mock-wordpress: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- yoast/phpunit-polyfills: ^1.0
This package is auto-updated.
Last update: 2026-03-22 18:49:18 UTC
README
WooCommerce-specific mocks and test utilities for unit testing WooCommerce plugins without requiring a full WooCommerce installation.
Namespace: ksfraser\MockWooCommerce\
Requirement: PHP 7.3+
Dependencies: ksfraser/mock-wordpress
Features
- WC Product Mock (
WCProduct) - Simulates WC_Product with all essential methods - WC Order Mock (
WCOrder) - Simulates WC_Order for order testing - Product Factory - Fluent builder for test products
- Order Factory - Fluent builder for test orders
- Payment Assertions - Verify payment processing
Installation
composer require ksfraser/mock-woocommerce --dev
Or use file-based repository during development:
{
"repositories": [
{"type": "path", "url": "../mock-wordpress"},
{"type": "path", "url": "../mock-woocommerce"}
],
"require-dev": {
"ksfraser/mock-woocommerce": "^1.0"
}
}
Usage
Creating Test Products
use ksfraser\MockWooCommerce\Factory\ProductFactory; $product = (new ProductFactory()) ->name('Auction Item') ->price(99.99) ->regularPrice(149.99) ->stock(5) ->sku('AUCTION-001') ->description('Premium auction item') ->build(); echo $product->get_name(); // "Auction Item" echo $product->get_price(); // 99.99 echo $product->is_in_stock(); // true
Creating Test Orders
use ksfraser\MockWooCommerce\Factory\OrderFactory; $order = (new OrderFactory()) ->customerId(42) ->total(99.99) ->status('completed') ->paymentMethod('stripe') ->transactionId('txn_12345') ->addProduct(product_id: 1, quantity: 1, total: 99.99) ->build(); echo $order->get_id(); // Auto-incremented order ID echo $order->get_total(); // 99.99 echo $order->is_paid(); // true
Product Testing
use ksfraser\MockWooCommerce\Mock\WCProduct; $product = new WCProduct(123); $product->set_name('Test Product'); $product->set_price(29.99); $product->set_stock_quantity(10); $product->set_sku('TEST-001'); $product->set_meta('_auction_price', '24.99'); if ($product->is_in_stock()) { echo "Product in stock"; } $auction_price = $product->get_meta('_auction_price');
Order Testing
use ksfraser\MockWooCommerce\Mock\WCOrder; $order = new WCOrder(456); $order->set_customer_id(42); $order->set_status('pending'); $order->set_payment_method('stripe'); $order->set_total(199.99); $order->add_item([ 'product_id' => 1, 'quantity' => 2, 'total' => 199.99, ]); $order->payment_complete(); echo $order->get_status(); // "processing"
Components
WCProduct
Mock WooCommerce product:
$product = new WCProduct(product_id: 1); // Setters $product->set_name('Name'); $product->set_price(9.99); $product->set_regular_price(19.99); $product->set_sale_price(9.99); $product->set_type('simple'); $product->set_status('publish'); $product->set_description('Description'); $product->set_sku('SKU-001'); $product->set_stock_quantity(10); $product->set_manage_stock(true); $product->set_meta('key', 'value'); // Getters $product->get_id(); $product->get_name(); $product->get_price(); $product->get_regular_price(); $product->get_sale_price(); $product->get_type(); $product->get_status(); $product->get_description(); $product->get_sku(); $product->get_stock_quantity(); $product->get_manage_stock(); $product->get_meta('key'); $product->is_in_stock(); $product->get_data();
WCOrder
Mock WooCommerce order:
$order = new WCOrder(order_id: 1); // Setters $order->set_customer_id(42); $order->set_status('completed'); $order->set_total(99.99); $order->set_currency('USD'); $order->set_payment_method('stripe'); $order->set_transaction_id('txn_12345'); $order->set_meta('key', 'value'); // Getters $order->get_id(); $order->get_customer_id(); $order->get_status(); $order->get_total(); $order->get_currency(); $order->get_payment_method(); $order->get_transaction_id(); $order->get_meta('key'); $order->get_items(); $order->get_item(item_id: 1); $order->is_paid(); $order->get_formatted_order_total(); // Item management $order->add_item(['product_id' => 1, 'quantity' => 2]); // Payment $order->payment_complete(); // Sets status to 'processing'
ProductFactory
Fluent builder for WooCommerce products:
$product = (new ProductFactory()) ->id(123) ->name('Product Name') ->price(99.99) ->regularPrice(149.99) ->salePrice(79.99) ->type('simple') // or 'variable' ->stock(10) ->sku('SKU-001') ->description('Product description') ->shortDescription('Short desc') ->meta('_custom_field', 'value') ->inStock() // Helper ->simple() // Helper for type ->build();
OrderFactory
Fluent builder for WooCommerce orders:
$order = (new OrderFactory()) ->id(456) ->customerId(42) ->status('completed') // or pending(), processing(), refunded() ->total(99.99) ->currency('USD') ->paymentMethod('stripe') ->transactionId('txn_12345') ->addProduct(product_id: 1, quantity: 2, total: 99.99) ->addItem(['product_id' => 2, 'quantity' => 1, 'total' => 49.99]) ->build();
Running Tests
vendor/bin/phpunit vendor/bin/phpunit tests/Factory/ProductFactoryTest.php vendor/bin/phpunit --coverage-html=coverage
Test Coverage
- Comprehensive tests for all mocks and factories
- 100% code coverage for mock components
- Run with:
composer test-coverage
Dependencies
- PHPUnit 9.6+
- PHP 7.3+
- ksfraser/mock-wordpress - WordPress mocks
Related Packages
- ksfraser/mock-wordpress - WordPress mocks (dependency)
- ksfraser/test-factories - Auction-specific test builders
License
GPL 3.0 or later
Contributing
When extending mocks:
- Follow PSR-12 code standards
- Add PHPDoc documentation with
@requirementtags - Ensure fluent interfaces for factories
- Include 100% unit test coverage
- Update this README with usage examples