ksfraser/mock-woocommerce

WooCommerce mocks, factories, and assertions for unit testing WooCommerce plugins

Maintainers

Package info

github.com/ksfraser/Mock-Woocommerce

Homepage

pkg:composer/ksfraser/mock-woocommerce

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-03-22 18:49 UTC

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

Related Packages

License

GPL 3.0 or later

Contributing

When extending mocks:

  1. Follow PSR-12 code standards
  2. Add PHPDoc documentation with @requirement tags
  3. Ensure fluent interfaces for factories
  4. Include 100% unit test coverage
  5. Update this README with usage examples