Skip to content

Commit f85f833

Browse files
Shipping factory (#9)
* Adds shipping zone factory. For #8 * Ads shipping zone method factory class. Fixes #8.
1 parent 1443e9b commit f85f833

File tree

4 files changed

+212
-6
lines changed

4 files changed

+212
-6
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,51 @@ $this->factory()->coupon->create_and_get(
206206
);
207207
```
208208

209+
### Shipping zones
210+
211+
You can access the shipping zone factory by using `$this->factory()->shipping_zone` within a WooCommerce integration test.
212+
213+
The 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.
214+
215+
`create_and_get($args)` returns the result of `new WC_Shipping_Zone( $shipping_zone_id )` for the created object.
216+
217+
See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-shipping-zone
218+
219+
Example:
220+
221+
```php
222+
$this->factory()->shipping_zone->create_and_get(
223+
array(
224+
'name' => 'Global'
225+
)
226+
);
227+
```
228+
229+
### Shipping zone methods
230+
231+
You can access the shipping zone method factory by using `$this->factory()->shipping_zone_method` within a WooCommerce integration test.
232+
233+
The 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.
234+
235+
`create_and_get($args)` returns an `WC_Shipping_Method` object.
236+
237+
Not that you have to set a zone_id, as created with the `shipping_zone` factory.
238+
239+
See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#include-a-shipping-method-to-a-shipping-zone
240+
241+
Example:
242+
243+
```php
244+
$this->factory()->shipping_zone_method->zone_id(5)->create_and_get(
245+
array(
246+
'method_id' => 'flat_rate',
247+
'settings' => array(
248+
'cost'=> '20.00',
249+
),
250+
),
251+
);
252+
```
253+
209254
### Subscriptions
210255

211256
The subscription factory can only be used when the [WooCommerce Subscriptions](https://woocommerce.com/products/woocommerce-subscriptions/) plugin is installed and activated.
@@ -299,7 +344,7 @@ public function test_can_add_sample_to_cart() {
299344

300345
### Roadmap
301346

302-
The main focus is on implementing more factories for other WooCommerce objects such as **customers**, **refunds** and **shipping methods**.
347+
The main focus is on implementing more factories for other WooCommerce objects such as **customers**, and **refunds**.
303348

304349
After this, focus might shift to popular extensions for WooCommerce, such as Subscriptions or Bookings.
305350

src/Factories/ShippingZone.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce\Factories;
4+
5+
use Exception;
6+
use TypeError;
7+
use WC_Shipping_Zone;
8+
use WP_UnitTest_Factory_For_Thing;
9+
10+
class ShippingZone extends WP_UnitTest_Factory_For_Thing {
11+
use APICalling;
12+
13+
/**
14+
* Creates a shipping zone. Using the API method.
15+
*
16+
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-shipping-zone
17+
*
18+
* @return int
19+
*/
20+
public function create_object( $args ) {
21+
$request = new \WP_REST_Request( 'post', '/wc/v3/shipping/zones' );
22+
$request->add_header( 'Content-Type', 'application/json' );
23+
$request->set_body( json_encode( $args ) ); //phpcs:ignore
24+
25+
$response = $this->do_request( $request );
26+
return $response->get_data()['id'];
27+
}
28+
29+
/**
30+
* Updates a shipping zone.
31+
*
32+
* @param int $object Shipping zone ID.
33+
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#update-a-shipping-zone
34+
*
35+
* @return int
36+
*/
37+
public function update_object( $object, $fields ) {
38+
if ( ! is_int( $object ) ) {
39+
throw new TypeError( '$object must be an int' );
40+
}
41+
42+
$request = new \WP_REST_Request( 'put', '/wc/v3/shipping/zones/' . $object );
43+
$request->add_header( 'Content-Type', 'application/json' );
44+
$request->set_body( json_encode( $fields ) ); //phpcs:ignore
45+
46+
$response = $this->do_request( $request );
47+
return $response->get_data()['id'];
48+
}
49+
50+
/**
51+
* Gets a woocommerce shipping zone.
52+
*
53+
* @param int $object_id The shipping zone.
54+
*
55+
* @return WC_Shipping_Zone
56+
*/
57+
public function get_object_by_id( $object_id ) {
58+
$shipping_zone = new WC_Shipping_Zone( $object_id );
59+
if ( ! $shipping_zone instanceof WC_Shipping_Zone ) {
60+
throw new Exception( 'Could not retrieve shipping zone with ID ' . $object_id );
61+
}
62+
return $shipping_zone;
63+
}
64+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce\Factories;
4+
5+
use BadMethodCallException;
6+
use Exception;
7+
use TypeError;
8+
use WC_Shipping_Method;
9+
use WC_Shipping_Zone;
10+
use WC_Shipping_Zones;
11+
use WP_UnitTest_Factory_For_Thing;
12+
13+
class ShippingZoneMethod extends WP_UnitTest_Factory_For_Thing {
14+
use APICalling;
15+
16+
/**
17+
* @var int
18+
*/
19+
private $zone_id = null;
20+
21+
public function zone_id( int $zone_id ): self {
22+
$this->zone_id = $zone_id;
23+
return $this;
24+
}
25+
26+
/**
27+
* Creates a shipping zone method. Using the API method.
28+
*
29+
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#include-a-shipping-method-to-a-shipping-zone
30+
*
31+
* @return int
32+
*/
33+
public function create_object( $args ) {
34+
if ( empty( $this->zone_id ) ) {
35+
throw new BadMethodCallException( 'Zone ID not set.' );
36+
}
37+
$request = new \WP_REST_Request( 'post', '/wc/v3/shipping/zones/' . $this->zone_id . '/methods' );
38+
$request->add_header( 'Content-Type', 'application/json' );
39+
$request->set_body( json_encode( $args ) ); //phpcs:ignore
40+
41+
$response = $this->do_request( $request );
42+
return $response->get_data()['instance_id'];
43+
}
44+
45+
/**
46+
* Updates a shipping zone method.
47+
*
48+
* @param int $object Shipping zone method ID.
49+
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#update-a-shipping-method-of-a-shipping-zone
50+
*
51+
* @return int
52+
*/
53+
public function update_object( $object, $fields ) {
54+
if ( ! is_int( $object ) ) {
55+
throw new TypeError( '$object must be an int' );
56+
}
57+
58+
if ( empty( $this->zone_id ) ) {
59+
throw new BadMethodCallException( 'Zone ID not set.' );
60+
}
61+
62+
$request = new \WP_REST_Request( 'put', 'wc/v3/shipping/zones/' . $this->zone_id . '/methods/' . $object );
63+
$request->add_header( 'Content-Type', 'application/json' );
64+
$request->set_body( json_encode( $fields ) ); //phpcs:ignore
65+
66+
$response = $this->do_request( $request );
67+
return $response->get_data()['instance_id'];
68+
}
69+
70+
/**
71+
* Gets a woocommerce shipping zone.
72+
*
73+
* @param int $object_id The shipping zone.
74+
*
75+
* @return WC_Shipping_Method
76+
*/
77+
public function get_object_by_id( $object_id ) {
78+
if ( empty( $this->zone_id ) ) {
79+
throw new BadMethodCallException( 'Zone ID not set.' );
80+
}
81+
$shipping_zone = new WC_Shipping_Zone( $this->zone_id );
82+
if ( ! $shipping_zone instanceof WC_Shipping_Zone ) {
83+
throw new Exception( 'Could not retrieve shipping zone method with ID ' . $object_id );
84+
}
85+
$methods = $shipping_zone->get_shipping_methods();
86+
foreach($methods as $method){
87+
if( $method->get_instance_id() === $object_id && $method instanceof WC_Shipping_Method ) {
88+
return $method;
89+
}
90+
}
91+
throw new Exception( 'Could not retrieve shipping zone method with ID ' . $object_id );
92+
}
93+
}

src/WC_UnitTest_Factory.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use LevelLevel\WPBrowserWooCommerce\Factories\Coupon;
66
use LevelLevel\WPBrowserWooCommerce\Factories\Product;
77
use LevelLevel\WPBrowserWooCommerce\Factories\Order;
8+
use LevelLevel\WPBrowserWooCommerce\Factories\ShippingZone;
9+
use LevelLevel\WPBrowserWooCommerce\Factories\ShippingZoneMethod;
810
use LevelLevel\WPBrowserWooCommerce\Factories\Subscription;
911
use LevelLevel\WPBrowserWooCommerce\Factories\TaxRate;
1012
use WP_UnitTest_Factory;
@@ -37,10 +39,12 @@ class WC_UnitTest_Factory extends WP_UnitTest_Factory {
3739

3840
public function __construct() {
3941
parent::__construct();
40-
$this->product = new Product( $this );
41-
$this->order = new Order( $this );
42-
$this->tax_rate = new TaxRate( $this );
43-
$this->coupon = new Coupon( $this );
44-
$this->subscription = new Subscription( $this );
42+
$this->product = new Product( $this );
43+
$this->order = new Order( $this );
44+
$this->tax_rate = new TaxRate( $this );
45+
$this->coupon = new Coupon( $this );
46+
$this->shipping_zone = new ShippingZone( $this );
47+
$this->shipping_zone_method = new ShippingZoneMethod( $this );
48+
$this->subscription = new Subscription( $this );
4549
}
4650
}

0 commit comments

Comments
 (0)