Skip to content

Commit

Permalink
Shipping factory (#9)
Browse files Browse the repository at this point in the history
* Adds shipping zone factory.

For #8

* Ads shipping zone method factory class.

Fixes #8.
  • Loading branch information
NielsdeBlaauw authored May 16, 2022
1 parent 1443e9b commit f85f833
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 6 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,51 @@ $this->factory()->coupon->create_and_get(
);
```

### Shipping zones

You can access the shipping zone factory by using `$this->factory()->shipping_zone` within a WooCommerce integration test.

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.

`create_and_get($args)` returns the result of `new WC_Shipping_Zone( $shipping_zone_id )` for the created object.

See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-shipping-zone

Example:

```php
$this->factory()->shipping_zone->create_and_get(
array(
'name' => 'Global'
)
);
```

### Shipping zone methods

You can access the shipping zone method factory by using `$this->factory()->shipping_zone_method` within a WooCommerce integration test.

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.

`create_and_get($args)` returns an `WC_Shipping_Method` object.

Not that you have to set a zone_id, as created with the `shipping_zone` factory.

See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#include-a-shipping-method-to-a-shipping-zone

Example:

```php
$this->factory()->shipping_zone_method->zone_id(5)->create_and_get(
array(
'method_id' => 'flat_rate',
'settings' => array(
'cost'=> '20.00',
),
),
);
```

### Subscriptions

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

### Roadmap

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

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

Expand Down
64 changes: 64 additions & 0 deletions src/Factories/ShippingZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace LevelLevel\WPBrowserWooCommerce\Factories;

use Exception;
use TypeError;
use WC_Shipping_Zone;
use WP_UnitTest_Factory_For_Thing;

class ShippingZone extends WP_UnitTest_Factory_For_Thing {
use APICalling;

/**
* Creates a shipping zone. Using the API method.
*
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-shipping-zone
*
* @return int
*/
public function create_object( $args ) {
$request = new \WP_REST_Request( 'post', '/wc/v3/shipping/zones' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( json_encode( $args ) ); //phpcs:ignore

$response = $this->do_request( $request );
return $response->get_data()['id'];
}

/**
* Updates a shipping zone.
*
* @param int $object Shipping zone ID.
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#update-a-shipping-zone
*
* @return int
*/
public function update_object( $object, $fields ) {
if ( ! is_int( $object ) ) {
throw new TypeError( '$object must be an int' );
}

$request = new \WP_REST_Request( 'put', '/wc/v3/shipping/zones/' . $object );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( json_encode( $fields ) ); //phpcs:ignore

$response = $this->do_request( $request );
return $response->get_data()['id'];
}

/**
* Gets a woocommerce shipping zone.
*
* @param int $object_id The shipping zone.
*
* @return WC_Shipping_Zone
*/
public function get_object_by_id( $object_id ) {
$shipping_zone = new WC_Shipping_Zone( $object_id );
if ( ! $shipping_zone instanceof WC_Shipping_Zone ) {
throw new Exception( 'Could not retrieve shipping zone with ID ' . $object_id );
}
return $shipping_zone;
}
}
93 changes: 93 additions & 0 deletions src/Factories/ShippingZoneMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace LevelLevel\WPBrowserWooCommerce\Factories;

use BadMethodCallException;
use Exception;
use TypeError;
use WC_Shipping_Method;
use WC_Shipping_Zone;
use WC_Shipping_Zones;
use WP_UnitTest_Factory_For_Thing;

class ShippingZoneMethod extends WP_UnitTest_Factory_For_Thing {
use APICalling;

/**
* @var int
*/
private $zone_id = null;

public function zone_id( int $zone_id ): self {
$this->zone_id = $zone_id;
return $this;
}

/**
* Creates a shipping zone method. Using the API method.
*
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#include-a-shipping-method-to-a-shipping-zone
*
* @return int
*/
public function create_object( $args ) {
if ( empty( $this->zone_id ) ) {
throw new BadMethodCallException( 'Zone ID not set.' );
}
$request = new \WP_REST_Request( 'post', '/wc/v3/shipping/zones/' . $this->zone_id . '/methods' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( json_encode( $args ) ); //phpcs:ignore

$response = $this->do_request( $request );
return $response->get_data()['instance_id'];
}

/**
* Updates a shipping zone method.
*
* @param int $object Shipping zone method ID.
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#update-a-shipping-method-of-a-shipping-zone
*
* @return int
*/
public function update_object( $object, $fields ) {
if ( ! is_int( $object ) ) {
throw new TypeError( '$object must be an int' );
}

if ( empty( $this->zone_id ) ) {
throw new BadMethodCallException( 'Zone ID not set.' );
}

$request = new \WP_REST_Request( 'put', 'wc/v3/shipping/zones/' . $this->zone_id . '/methods/' . $object );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( json_encode( $fields ) ); //phpcs:ignore

$response = $this->do_request( $request );
return $response->get_data()['instance_id'];
}

/**
* Gets a woocommerce shipping zone.
*
* @param int $object_id The shipping zone.
*
* @return WC_Shipping_Method
*/
public function get_object_by_id( $object_id ) {
if ( empty( $this->zone_id ) ) {
throw new BadMethodCallException( 'Zone ID not set.' );
}
$shipping_zone = new WC_Shipping_Zone( $this->zone_id );
if ( ! $shipping_zone instanceof WC_Shipping_Zone ) {
throw new Exception( 'Could not retrieve shipping zone method with ID ' . $object_id );
}
$methods = $shipping_zone->get_shipping_methods();
foreach($methods as $method){
if( $method->get_instance_id() === $object_id && $method instanceof WC_Shipping_Method ) {
return $method;
}
}
throw new Exception( 'Could not retrieve shipping zone method with ID ' . $object_id );
}
}
14 changes: 9 additions & 5 deletions src/WC_UnitTest_Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use LevelLevel\WPBrowserWooCommerce\Factories\Coupon;
use LevelLevel\WPBrowserWooCommerce\Factories\Product;
use LevelLevel\WPBrowserWooCommerce\Factories\Order;
use LevelLevel\WPBrowserWooCommerce\Factories\ShippingZone;
use LevelLevel\WPBrowserWooCommerce\Factories\ShippingZoneMethod;
use LevelLevel\WPBrowserWooCommerce\Factories\Subscription;
use LevelLevel\WPBrowserWooCommerce\Factories\TaxRate;
use WP_UnitTest_Factory;
Expand Down Expand Up @@ -37,10 +39,12 @@ class WC_UnitTest_Factory extends WP_UnitTest_Factory {

public function __construct() {
parent::__construct();
$this->product = new Product( $this );
$this->order = new Order( $this );
$this->tax_rate = new TaxRate( $this );
$this->coupon = new Coupon( $this );
$this->subscription = new Subscription( $this );
$this->product = new Product( $this );
$this->order = new Order( $this );
$this->tax_rate = new TaxRate( $this );
$this->coupon = new Coupon( $this );
$this->shipping_zone = new ShippingZone( $this );
$this->shipping_zone_method = new ShippingZoneMethod( $this );
$this->subscription = new Subscription( $this );
}
}

0 comments on commit f85f833

Please sign in to comment.