{"id":20930071,"url":"https://github.com/level-level/wp-browser-woocommerce","last_synced_at":"2025-10-15T16:15:57.659Z","repository":{"id":40299121,"uuid":"377079622","full_name":"level-level/wp-browser-woocommerce","owner":"level-level","description":"Adds factory functions for WooCommerce to be used with wp-browser integration tests.","archived":false,"fork":false,"pushed_at":"2025-03-24T13:05:03.000Z","size":32,"stargazers_count":13,"open_issues_count":1,"forks_count":2,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-30T14:58:49.090Z","etag":null,"topics":["codeception","integration-testing","testing","woocommerce","wp-browser"],"latest_commit_sha":null,"homepage":"https://level-level.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/level-level.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-15T07:47:09.000Z","updated_at":"2025-02-27T07:59:01.000Z","dependencies_parsed_at":"2024-02-06T15:29:27.183Z","dependency_job_id":"128a7a4d-cc3b-4817-bf1f-948316f99c1e","html_url":"https://github.com/level-level/wp-browser-woocommerce","commit_stats":{"total_commits":18,"total_committers":5,"mean_commits":3.6,"dds":0.5,"last_synced_commit":"89488fbafc8043c3a2e290950b0d36758a566ea5"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/level-level%2Fwp-browser-woocommerce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/level-level%2Fwp-browser-woocommerce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/level-level%2Fwp-browser-woocommerce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/level-level%2Fwp-browser-woocommerce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/level-level","download_url":"https://codeload.github.com/level-level/wp-browser-woocommerce/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254003427,"owners_count":21997886,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeception","integration-testing","testing","woocommerce","wp-browser"],"created_at":"2024-11-18T21:28:40.989Z","updated_at":"2025-10-15T16:15:52.615Z","avatar_url":"https://github.com/level-level.png","language":"PHP","readme":"# wp-browser-woocommerce\nThis library simplifies testing of WooCommerce themes and plugins with [wp-browser](https://github.com/lucatume/wp-browser). Several [Unit Test Factories](https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#fixtures-and-factories) are added that allow you to quickly create WooCommerce products and orders within an integration test.\n\n## Getting started\nBefore getting started with `wp-browser-woocommerce`, make sure you read the excellent [documentation for wp-browser](https://wpbrowser.wptestkit.dev/) first.\n### Installation\nTo install `wp-browser-woocommerce` you use [composer](https://getcomposer.org/). The library is published on [packagist](https://packagist.org/packages/level-level/wp-browser-woocommerce). \n\n```shell\ncomposer require --dev level-level/wp-browser-woocommerce\n```\n\n### Your first WooCommerce test\n\nTests written with `wp-browser-woocommerce` are a lot like regular `wp-browser` integration tests. By extending from `\\LevelLevel\\WPBrowserWooCommerce\\WCTestCase` instead of the regular `\\WPTestCase`, you will get access to WooCommerce unit test factories. \n\n```php\n\u003c?php // ./tests/wpunit/ExampleTest.php\n\nuse LevelLevel\\WPBrowserWooCommerce\\WCTestCase;\n\nclass ExampleTest extends WCTestCase{\n    public function test_something(){\n        // Create a WooCommerce product.\n        $product = $this-\u003efactory()-\u003eproduct-\u003ecreate_and_get(\n\t\t\tarray(\n\t\t\t\t'name'          =\u003e 'test',\n\t\t\t\t'regular_price' =\u003e '12.12',\n\t\t\t)\n\t\t);\n\n        // Create a WooCommerce order with two products.\n\t\t$order   = $this-\u003efactory()-\u003eorder-\u003ecreate_and_get(\n\t\t\tarray(\n\t\t\t\t'payment_method'       =\u003e 'bacs',\n\t\t\t\t'payment_method_title' =\u003e 'BACS',\n\t\t\t\t'set_paid'             =\u003e true,\n\t\t\t\t'line_items'           =\u003e array(\n\t\t\t\t\tarray(\n\t\t\t\t\t\t'product_id' =\u003e $product-\u003eget_id(),\n\t\t\t\t\t\t'quantity'   =\u003e 2,\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t)\n\t\t);\n\n        // Make sure the order total price is correct.\n        $this-\u003eassertEquals( 24.24, $order-\u003eget_total() );\n    }\n}\n```\n\n## Factories\nThe factories provide methods to allow for quick object creation. The factories are access with the `$this-\u003efactory()` method on a testcase.\n\nIn the background, the factories use [the WooCommerce REST API](https://woocommerce.github.io/woocommerce-rest-api-docs/#introduction) methods to create and retrieve objects. These are not actual `GET`/`POST` requests, but rather internal calls to the methods that would process the regular requests to the API.\n\nAll factories extend from the WordPress default [WP_UnitTest_Factory_For_Thing](https://github.com/WordPress/wordpress-develop/blob/master/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php). All methods that are specified on this base class are available on the factories you will use in WooCommerce tests.\n\nIn this documentation you will only find the most used ones, refer to the base class or WordPress documentation for others.\n\n### Orders\nYou can access the order factory by using `$this-\u003efactory()-\u003eorder` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to an order are the same as you can give to the order creation API endpoint. \n\n`create_and_get($args)` returns the result of `wc_get_order()` for the created object.\n\nSee https://woocommerce.github.io/woocommerce-rest-api-docs/#create-an-order\n\nExample:\n```php\n$order = $this-\u003efactory()-\u003eorder-\u003ecreate_and_get(\n    array(\n        'payment_method'       =\u003e 'bacs',\n        'payment_method_title' =\u003e 'BACS',\n        'set_paid'             =\u003e true,\n        'billing'              =\u003e array(\n            'first_name'   =\u003e 'John',\n            'last_name'    =\u003e 'Doe',\n            'address_1'    =\u003e 'Market',\n            'house_number' =\u003e '1',\n            'address_2'    =\u003e '',\n            'city'         =\u003e 'Rotterdam',\n            'postcode'     =\u003e '3456AB',\n            'country'      =\u003e 'NL',\n            'email'        =\u003e 'john.doe@example.com',\n        ),\n        'shipping'             =\u003e array(\n            'first_name'   =\u003e 'John',\n            'last_name'    =\u003e 'Doe',\n            'address_1'    =\u003e 'Memory Lane',\n            'house_number' =\u003e '1',\n            'address_2'    =\u003e '',\n            'city'         =\u003e 'Rotterdam',\n            'postcode'     =\u003e '3456AB',\n            'country'      =\u003e 'NL',\n        ),\n        'line_items'           =\u003e array(\n            array(\n                'product_id' =\u003e 1,\n                'quantity'   =\u003e 1,\n                'meta_data'  =\u003e array(\n                    array(\n                        'key'   =\u003e 'made_by',\n                        'value' =\u003e 'Level Level',\n                    ),\n                    array(\n                        'key'   =\u003e 'with_love',\n                        'value' =\u003e 'obviously'\n                    ),\n                ),\n            ),\n        ),\n        'shipping_lines': array(\n            array(\n                'method_id': 'flat_rate',\n                'method_title': 'Flat Rate',\n                'total': '10.00'\n            )\n        )\n    )\n);\n```\n\n### Products\n\nYou can access the order factory by using `$this-\u003efactory()-\u003eproduct` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to an order are the same as you can give to the product creation API endpoint. \n\n`create_and_get($args)` returns the result of `wc_get_product()` for the created object.\n\nSee https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product\n\nExample:\n\n```php\n$this-\u003efactory()-\u003eproduct-\u003ecreate_and_get(\n    array(\n        'name'            =\u003e 'test',\n        'regular_price'   =\u003e '103.11',\n        'weight'          =\u003e '14',\n        'dimensions'      =\u003e array(\n            'height' =\u003e '1',\n        ),\n        'reviews_allowed' =\u003e false,\n        'manage_stock'    =\u003e true,\n        'stock_status'    =\u003e 'onbackorder',\n        'backorders'      =\u003e 'yes',\n        'meta_data'       =\u003e array(\n            array(\n                'key'   =\u003e 'made_in',\n                'value' =\u003e 'Rotterdam',\n            ),\n        ),\n    )\n);\n```\n\n### Tax rates\n\nYou can access the order factory by using `$this-\u003efactory()-\u003etax_rate` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to an order are the same as you can give to the product creation API endpoint. \n\n`create_and_get($args)` returns an array, as tax rates have no data class/model within WooCommerce.\n\nSee https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-tax-rate\n\nExample:\n\n```php\n$this-\u003efactory()-\u003etax_rate-\u003ecreate_and_get(\n    array(\n        'country'=\u003e'NL',\n        'rate'=\u003e '21',\n        'name'=\u003e'BTW hoog tarief',\n        'shipping'=\u003efalse,\n    )\n);\n```\n\n### Coupons\n\nYou can access the coupon factory by using `$this-\u003efactory()-\u003ecoupon` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to a coupon are the same as you can give to the coupon creation API endpoint. \n\n`create_and_get($args)` returns the result of `new WC_Coupon( $coupon_id )` for the created object.\n\nSee https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-coupon\n\nExample:\n\n```php\n$this-\u003efactory()-\u003ecoupon-\u003ecreate_and_get(\n    array(\n        'code'               =\u003e '25off',\n        'discount_type'      =\u003e 'percent',\n        'amount'             =\u003e '10',\n        'individual_use'     =\u003e true,\n        'exclude_sale_items' =\u003e true,\n        'minimum_amount'     =\u003e '100.00',\n    )\n);\n```\n\n### Shipping zones\n\nYou can access the shipping zone factory by using `$this-\u003efactory()-\u003eshipping_zone` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to a shipping zone are the same as you can give to the shipping zone creation API endpoint. \n\n`create_and_get($args)` returns the result of `new WC_Shipping_Zone( $shipping_zone_id )` for the created object.\n\nSee https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-shipping-zone\n\nExample:\n\n```php\n$this-\u003efactory()-\u003eshipping_zone-\u003ecreate_and_get(\n    array(\n        'name' =\u003e 'Global'\n    )\n);\n```\n\n#### Adding locations\n\nIt's possible to add location rules to shipping zones. See the code below for an example:\n\n```php\n$this-\u003efactory()-\u003eshipping_zone-\u003eset_zone_locations(\n    1, array(\n        array(\n            'code' =\u003e '3024*',\n            'type' =\u003e 'postcode',\n        ),\n    )\n);\n```\n\n### Shipping zone methods\n\nYou can access the shipping zone method factory by using `$this-\u003efactory()-\u003eshipping_zone_method` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to a shipping zone method are the same as you can give to the shipping zone method creation API endpoint. \n\n`create_and_get($args)` returns an `WC_Shipping_Method` object.\n\nNot that you have to set a zone_id, as created with the `shipping_zone` factory.\n\nSee https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#include-a-shipping-method-to-a-shipping-zone\n\nExample:\n\n```php\n$this-\u003efactory()-\u003eshipping_zone_method-\u003ezone_id(5)-\u003ecreate_and_get(\n    array(\n        'method_id' =\u003e 'flat_rate',\n        'settings'  =\u003e array(\n            'cost'=\u003e '20.00',\n        ),\n    ),\n);\n```\n\n### Subscriptions\n\nThe subscription factory can only be used when the [WooCommerce Subscriptions](https://woocommerce.com/products/woocommerce-subscriptions/) plugin is installed and activated.\n\nYou can access the subscription factory by using `$this-\u003efactory()-\u003esubscription` within a WooCommerce integration test. \n\nThe main method you'll use is `create_and_get( $args )`. The input you can give to a subscription are the same as you can give to the subscription creation API endpoint. \n\n`create_and_get($args)` returns the result of `wcs_get_subscription( $subscription_id )` for the created object.\n\nSee https://woocommerce.github.io/subscriptions-rest-api-docs/v1.html#create-a-subscription\n\nExample:\n\n```php\n$this-\u003efactory()-\u003esubscription-\u003ecreate_and_get(\n    array(\n        'customer_id' =\u003e 1,\n        'parent_id' =\u003e 1,\n        'status' =\u003e 'pending',\n        'billing_period' =\u003e 'month',\n        'billing_interval' =\u003e 1,\n        'start_date' =\u003e ( new DateTime( 'now', wp_timezone() ) )-\u003eformat( 'Y-m-d H:i:s' ),\n        'next_payment_date' =\u003e ( new DateTime( '+1 month', wp_timezone() ) )-\u003eformat( 'Y-m-d H:i:s' ),\n        'payment_method' =\u003e '',\n        'billing' =\u003e array(\n            'first_name' =\u003e 'John',\n            'last_name' =\u003e 'Doe',\n            'address_1' =\u003e 'Market',\n            'house_number' =\u003e '1',\n            'address_2' =\u003e '',\n            'city' =\u003e 'Rotterdam',\n            'postcode' =\u003e '3456AB',\n            'country' =\u003e 'NL',\n            'email' =\u003e 'john.doe@example.com',\n        ),\n        'shipping' =\u003e array(\n            'first_name' =\u003e 'John',\n            'last_name' =\u003e 'Doe',\n            'address_1' =\u003e 'Memory Lane',\n            'house_number' =\u003e '1',\n            'address_2' =\u003e '',\n            'city' =\u003e 'Rotterdam',\n            'postcode' =\u003e '3456AB',\n            'country' =\u003e 'NL',\n        ),\n        'line_items' =\u003e array(\n            array(\n                'product_id' =\u003e 1,\n                'quantity' =\u003e 1,\n                'subtotal' =\u003e '10.00',\n                'total' =\u003e '10.00',\n            )\n        )\n    )\n);\n```\n\n## Testcases\nFor most testcases you will want to use `\\LevelLevel\\WPBrowserWooCommerce\\WCTestCase`\n\n### Ajax calls\nFor ajax calls, the regular [\\WPAjaxTestCase](https://wpbrowser.wptestkit.dev/commands#generate-wpajax) would be replaced with `\\LevelLevel\\WPBrowserWooCommerce\\WCAjaxTestCase`\n\nExample:\n\n```php\npublic function test_can_add_sample_to_cart() {\n    WC_AJAX::init();\n\n    $product = $this-\u003efactory()-\u003eproduct-\u003ecreate_and_get(\n        array(\n            'name'          =\u003e 'test',\n            'regular_price' =\u003e '12.12',\n        )\n    );\n    \n    // ... testing logic ...\n    \n    try {\n        $this-\u003e_handleAjax( 'woocommerce_add_to_cart' );\n    } catch ( WPAjaxDieContinueException $e ) {\n        ob_end_flush();\n    }\n    $this-\u003eassertEmpty( wc_get_notices( 'error' ), 'There should be no error notices after making this ajax call.' );\n}\n```\n\n## Development\n`wp-browser-woocommerce` is actively being used at [Level Level](https://level-level.com/). The library will get new features as we need them for client projects.\n\n### Roadmap\n\nThe main focus is on implementing more factories for other WooCommerce objects such as **customers**, and **refunds**.\n\nAfter this, focus might shift to popular extensions for WooCommerce, such as Subscriptions or Bookings.\n\n### Contributing\nFeel free to open issues or create pull requests if you feel something is missing or working incorrectly.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flevel-level%2Fwp-browser-woocommerce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flevel-level%2Fwp-browser-woocommerce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flevel-level%2Fwp-browser-woocommerce/lists"}