level-level / wp-browser-woocommerce
为 WP Browser 集成测试提供的 WooCommerce 对象工厂。
Requires
- lucatume/wp-browser: ^2|^3
This package is auto-updated.
Last update: 2024-08-27 12:45:29 UTC
README
此库简化了使用 wp-browser 测试 WooCommerce 主题和插件的过程。添加了几个 单元测试工厂,允许您在集成测试中快速创建 WooCommerce 产品和订单。
入门指南
在开始使用 wp-browser-woocommerce
之前,请确保您首先阅读了 wp-browser 的优秀 文档。
安装
要安装 wp-browser-woocommerce
,您使用 composer。该库在 packagist 上发布。
composer require --dev level-level/wp-browser-woocommerce
您的第一个 WooCommerce 测试
使用 wp-browser-woocommerce
编写的测试与常规的 wp-browser
集成测试非常相似。通过从 \LevelLevel\WPBrowserWooCommerce\WCTestCase
扩展而不是常规的 \WPTestCase
,您将能够访问 WooCommerce 单元测试工厂。
<?php // ./tests/wpunit/ExampleTest.php use LevelLevel\WPBrowserWooCommerce\WCTestCase; class ExampleTest extends WCTestCase{ public function test_something(){ // Create a WooCommerce product. $product = $this->factory()->product->create_and_get( array( 'name' => 'test', 'regular_price' => '12.12', ) ); // Create a WooCommerce order with two products. $order = $this->factory()->order->create_and_get( array( 'payment_method' => 'bacs', 'payment_method_title' => 'BACS', 'set_paid' => true, 'line_items' => array( array( 'product_id' => $product->get_id(), 'quantity' => 2, ), ), ) ); // Make sure the order total price is correct. $this->assertEquals( 24.24, $order->get_total() ); } }
工厂
工厂提供方法以允许快速创建对象。通过在测试用例中使用 $this->factory()
方法来访问工厂。
在后台,工厂使用 WooCommerce REST API 方法来创建和检索对象。这些不是实际的 GET
/POST
请求,而是对处理常规 API 请求的方法的内部调用。
所有工厂都扩展自 WordPress 默认的 WP_UnitTest_Factory_For_Thing。在此基类上指定的所有方法都在您将用于 WooCommerce 测试的工厂中可用。
在此文档中,您将仅找到最常用的方法,有关其他方法,请参阅基类或 WordPress 文档。
订单
您可以在 WooCommerce 集成测试中使用 $this->factory()->order
访问订单工厂。
您将主要使用的方法是 create_and_get( $args )
。您可以提供给订单的输入与您可以提供给订单创建 API 端点的输入相同。
create_and_get($args)
返回创建对象的 wc_get_order()
的结果。
请参阅 https://woocommerce.github.io/woocommerce-rest-api-docs/#create-an-order
示例
$order = $this->factory()->order->create_and_get( array( 'payment_method' => 'bacs', 'payment_method_title' => 'BACS', 'set_paid' => true, 'billing' => array( 'first_name' => 'John', 'last_name' => 'Doe', 'address_1' => 'Market', 'house_number' => '1', 'address_2' => '', 'city' => 'Rotterdam', 'postcode' => '3456AB', 'country' => 'NL', 'email' => 'john.doe@example.com', ), 'shipping' => array( 'first_name' => 'John', 'last_name' => 'Doe', 'address_1' => 'Memory Lane', 'house_number' => '1', 'address_2' => '', 'city' => 'Rotterdam', 'postcode' => '3456AB', 'country' => 'NL', ), 'line_items' => array( array( 'product_id' => 1, 'quantity' => 1, 'meta_data' => array( array( 'key' => 'made_by', 'value' => 'Level Level', ), array( 'key' => 'with_love', 'value' => 'obviously' ), ), ), ), 'shipping_lines': array( array( 'method_id': 'flat_rate', 'method_title': 'Flat Rate', 'total': '10.00' ) ) ) );
产品
您可以在 WooCommerce 集成测试中使用 $this->factory()->product
访问产品工厂。
您将主要使用的方法是 create_and_get( $args )
。您可以提供给订单的输入与您可以提供给产品创建 API 端点的输入相同。
create_and_get($args)
返回创建对象的 wc_get_product()
的结果。
请参阅 https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product
示例
$this->factory()->product->create_and_get( array( 'name' => 'test', 'regular_price' => '103.11', 'weight' => '14', 'dimensions' => array( 'height' => '1', ), 'reviews_allowed' => false, 'manage_stock' => true, 'stock_status' => 'onbackorder', 'backorders' => 'yes', 'meta_data' => array( array( 'key' => 'made_in', 'value' => 'Rotterdam', ), ), ) );
税率
您可以在 WooCommerce 集成测试中使用 $this->factory()->tax_rate
访问税率工厂。
您将主要使用的方法是 create_and_get( $args )
。您可以提供给订单的输入与您可以提供给产品创建 API 端点的输入相同。
create_and_get($args)
返回一个数组,因为 WooCommerce 中没有用于税率的 data class/model。
请参阅https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-tax-rate
示例
$this->factory()->tax_rate->create_and_get( array( 'country'=>'NL', 'rate'=> '21', 'name'=>'BTW hoog tarief', 'shipping'=>false, ) );
优惠券
您可以在WooCommerce集成测试中使用 $this->factory()->coupon
访问优惠券工厂。
您将使用的主要方法是 create_and_get( $args )
。您可以提供给优惠券的输入与您可以提供给优惠券创建API端点的输入相同。
create_and_get($args)
返回创建对象的 new WC_Coupon( $coupon_id )
的结果。
请参阅https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-coupon
示例
$this->factory()->coupon->create_and_get( array( 'code' => '25off', 'discount_type' => 'percent', 'amount' => '10', 'individual_use' => true, 'exclude_sale_items' => true, 'minimum_amount' => '100.00', ) );
配送区域
您可以在WooCommerce集成测试中使用 $this->factory()->shipping_zone
访问配送区域工厂。
您将使用的主要方法是 create_and_get( $args )
。您可以提供给配送区域的输入与您可以提供给配送区域创建API端点的输入相同。
create_and_get($args)
返回创建对象的 new WC_Shipping_Zone( $shipping_zone_id )
的结果。
请参阅https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-shipping-zone
示例
$this->factory()->shipping_zone->create_and_get( array( 'name' => 'Global' ) );
添加位置
您可以将位置规则添加到配送区域中。以下代码示例。
$this->factory()->shipping_zone->set_zone_locations( 1, array( array( 'code' => '3024*', 'type' => 'postcode', ), ) );
配送区域方法
您可以在WooCommerce集成测试中使用 $this->factory()->shipping_zone_method
访问配送区域方法工厂。
您将使用的主要方法是 create_and_get( $args )
。您可以提供给配送区域方法的输入与您可以提供给配送区域方法创建API端点的输入相同。
create_and_get($args)
返回一个 WC_Shipping_Method
对象。
请注意,您必须设置一个 zone_id,该 zone_id 是使用 shipping_zone
工厂创建的。
示例
$this->factory()->shipping_zone_method->zone_id(5)->create_and_get( array( 'method_id' => 'flat_rate', 'settings' => array( 'cost'=> '20.00', ), ), );
订阅
当安装并激活了 WooCommerce Subscriptions 插件时,可以使用订阅工厂。
您可以在WooCommerce集成测试中使用 $this->factory()->subscription
访问订阅工厂。
您将使用的主要方法是 create_and_get( $args )
。您可以提供给订阅的输入与您可以提供给订阅创建API端点的输入相同。
create_and_get($args)
返回创建对象 wcs_get_subscription( $subscription_id )
的结果。
请参阅https://woocommerce.github.io/subscriptions-rest-api-docs/v1.html#create-a-subscription
示例
$this->factory()->subscription->create_and_get( array( 'customer_id' => 1, 'parent_id' => 1, 'status' => 'pending', 'billing_period' => 'month', 'billing_interval' => 1, 'start_date' => ( new DateTime( 'now', wp_timezone() ) )->format( 'Y-m-d H:i:s' ), 'next_payment_date' => ( new DateTime( '+1 month', wp_timezone() ) )->format( 'Y-m-d H:i:s' ), 'payment_method' => '', 'billing' => array( 'first_name' => 'John', 'last_name' => 'Doe', 'address_1' => 'Market', 'house_number' => '1', 'address_2' => '', 'city' => 'Rotterdam', 'postcode' => '3456AB', 'country' => 'NL', 'email' => 'john.doe@example.com', ), 'shipping' => array( 'first_name' => 'John', 'last_name' => 'Doe', 'address_1' => 'Memory Lane', 'house_number' => '1', 'address_2' => '', 'city' => 'Rotterdam', 'postcode' => '3456AB', 'country' => 'NL', ), 'line_items' => array( array( 'product_id' => 1, 'quantity' => 1, 'subtotal' => '10.00', 'total' => '10.00', ) ) ) );
测试用例
对于大多数测试用例,您希望使用 \LevelLevel\WPBrowserWooCommerce\WCTestCase
Ajax调用
对于Ajax调用,常规的 \WPAjaxTestCase 将被替换为 \LevelLevel\WPBrowserWooCommerce\WCAjaxTestCase
示例
public function test_can_add_sample_to_cart() { WC_AJAX::init(); $product = $this->factory()->product->create_and_get( array( 'name' => 'test', 'regular_price' => '12.12', ) ); // ... testing logic ... try { $this->_handleAjax( 'woocommerce_add_to_cart' ); } catch ( WPAjaxDieContinueException $e ) { ob_end_flush(); } $this->assertEmpty( wc_get_notices( 'error' ), 'There should be no error notices after making this ajax call.' ); }
开发
wp-browser-woocommerce
正在被 Level Level 使用。随着我们为客户项目需要新功能,库将获得新功能。
路线图
主要重点是实现更多其他WooCommerce对象的工厂,例如 客户 和 退款。
之后,重点可能转向WooCommerce的流行扩展,如订阅或预订。
贡献
如果您觉得某些东西缺失或工作不正确,请随时打开问题或创建拉取请求。