Unit tests for shipping zones
This commit is contained in:
parent
76405ed220
commit
a33b9b9750
|
@ -47,7 +47,18 @@ abstract class WC_Settings_API {
|
|||
* @return array of options
|
||||
*/
|
||||
public function get_form_fields() {
|
||||
return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, $this->form_fields );
|
||||
return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default required properties for each field.
|
||||
* @param array
|
||||
*/
|
||||
private function set_defaults( $field ) {
|
||||
if ( ! isset( $field['default'] ) ) {
|
||||
$field['default'] = '';
|
||||
}
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -483,7 +483,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_locations (
|
|||
PRIMARY KEY (location_id),
|
||||
KEY location_id (location_id),
|
||||
KEY location_type (location_type),
|
||||
KEY zone_location_type_code (location_type(40),location_code(90))
|
||||
KEY location_type_code (location_type(40),location_code(90))
|
||||
) $collate;
|
||||
CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods (
|
||||
zone_id bigint(20) NOT NULL,
|
||||
|
|
|
@ -102,6 +102,7 @@ class WC_Unit_Tests_Bootstrap {
|
|||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php' );
|
||||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php' );
|
||||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-order.php' );
|
||||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping-zones.php' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class WC_Helper_Shipping_Zones.
|
||||
*
|
||||
* This helper class should ONLY be used for unit tests!.
|
||||
*/
|
||||
class WC_Helper_Shipping_Zones {
|
||||
|
||||
/**
|
||||
* Create some mock shipping zones to test against
|
||||
*/
|
||||
public static function create_mock_zones() {
|
||||
// Local zone
|
||||
$zone = new WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'Local' );
|
||||
$zone->set_zone_order( 1 );
|
||||
$zone->add_location( 'GB', 'country' );
|
||||
$zone->add_location( 'CB*', 'postcode' );
|
||||
$zone->save();
|
||||
|
||||
// Europe zone
|
||||
$zone = new WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'Europe' );
|
||||
$zone->set_zone_order( 2 );
|
||||
$zone->add_location( 'EU', 'continent' );
|
||||
$zone->save();
|
||||
|
||||
// US california zone
|
||||
$zone = new WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'California' );
|
||||
$zone->set_zone_order( 3 );
|
||||
$zone->add_location( 'US:CA', 'state' );
|
||||
$zone->save();
|
||||
|
||||
// US zone
|
||||
$zone = new WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'US' );
|
||||
$zone->set_zone_order( 4 );
|
||||
$zone->add_location( 'US', 'country' );
|
||||
$zone->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all zones
|
||||
*/
|
||||
public static function remove_mock_zones() {
|
||||
global $wpdb;
|
||||
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_shipping_zone_methods;" );
|
||||
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_shipping_zone_locations;" );
|
||||
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_shipping_zones;" );
|
||||
$wpdb->query( "TRUNCATE " . $wpdb->prefix . "woocommerce_shipping_zone_methods" );
|
||||
$wpdb->query( "TRUNCATE " . $wpdb->prefix . "woocommerce_shipping_zone_locations" );
|
||||
$wpdb->query( "TRUNCATE " . $wpdb->prefix . "woocommerce_shipping_zones" );
|
||||
WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,156 @@
|
|||
<?php
|
||||
namespace WooCommerce\Tests\Shipping_Zones;
|
||||
|
||||
/**
|
||||
* Class Shipping_Zones.
|
||||
* @package WooCommerce\Tests\Shipping_Zones
|
||||
*/
|
||||
class Shipping_Zones extends \WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Test: WC_Shipping_Zones::get_zones
|
||||
*/
|
||||
public function test_get_zones() {
|
||||
// Setup
|
||||
\WC_Helper_Shipping_Zones::create_mock_zones();
|
||||
|
||||
// Test
|
||||
$zones = \WC_Shipping_Zones::get_zones();
|
||||
|
||||
// Assert
|
||||
$this->assertTrue( \is_array( $zones ) );
|
||||
$this->assertTrue( 4 === \sizeof( $zones ) );
|
||||
|
||||
// Clean
|
||||
\WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test: WC_Shipping_Zones::get_zone
|
||||
*/
|
||||
public function test_get_zone() {
|
||||
// Setup
|
||||
\WC_Helper_Shipping_Zones::create_mock_zones();
|
||||
|
||||
// Test
|
||||
$zone = \WC_Shipping_Zones::get_zone( 1 );
|
||||
|
||||
// Assert that the first zone is our local zone
|
||||
$this->assertInstanceOf( 'WC_Shipping_Zone', $zone );
|
||||
$this->assertEquals( $zone->get_zone_name(), 'Local' );
|
||||
|
||||
// Clean
|
||||
\WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test: WC_Shipping_Zones::get_zone_by
|
||||
*/
|
||||
public function test_get_zone_by() {
|
||||
// Setup
|
||||
\WC_Helper_Shipping_Zones::create_mock_zones();
|
||||
|
||||
// Test
|
||||
$zone = \WC_Shipping_Zones::get_zone_by( 'zone_id', 2 );
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf( 'WC_Shipping_Zone', $zone );
|
||||
$this->assertEquals( $zone->get_zone_name(), 'Europe' );
|
||||
|
||||
// Test instance_id
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
|
||||
$zone = \WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id );
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf( 'WC_Shipping_Zone', $zone );
|
||||
$this->assertEquals( $zone->get_zone_name(), 'Europe' );
|
||||
|
||||
// Clean
|
||||
\WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test: WC_Shipping_Zones::get_shipping_method
|
||||
*/
|
||||
public function test_get_shipping_method() {
|
||||
// Setup
|
||||
\WC_Helper_Shipping_Zones::create_mock_zones();
|
||||
|
||||
// Test
|
||||
$zone = \WC_Shipping_Zones::get_zone_by( 'zone_id', 1 );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$shipping_method = \WC_Shipping_Zones::get_shipping_method( $instance_id );
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf( 'WC_Shipping_Flat_Rate', $shipping_method );
|
||||
|
||||
// Clean
|
||||
\WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test: WC_Shipping_Zones::delete_zone
|
||||
*/
|
||||
public function test_delete_zone() {
|
||||
// Setup
|
||||
\WC_Helper_Shipping_Zones::create_mock_zones();
|
||||
|
||||
// Test
|
||||
\WC_Shipping_Zones::delete_zone( 1 );
|
||||
$zones = \WC_Shipping_Zones::get_zones();
|
||||
|
||||
// Assert
|
||||
$this->assertTrue( 3 === \sizeof( $zones ) );
|
||||
|
||||
// Clean
|
||||
\WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test: WC_Shipping_Zones::get_zone_matching_package
|
||||
*/
|
||||
public function test_get_zone_matching_package() {
|
||||
// Setup
|
||||
\WC_Helper_Shipping_Zones::create_mock_zones();
|
||||
|
||||
// Test
|
||||
$zone1 = \WC_Shipping_Zones::get_zone_matching_package( array(
|
||||
'destination' => array(
|
||||
'country' => 'GB',
|
||||
'state' => 'Cambs',
|
||||
'postcode' => 'CB23 1GG',
|
||||
)
|
||||
) );
|
||||
$zone2 = \WC_Shipping_Zones::get_zone_matching_package( array(
|
||||
'destination' => array(
|
||||
'country' => 'GB',
|
||||
'state' => 'Cambs',
|
||||
'postcode' => 'PE12 1BG',
|
||||
)
|
||||
) );
|
||||
$zone3 = \WC_Shipping_Zones::get_zone_matching_package( array(
|
||||
'destination' => array(
|
||||
'country' => 'US',
|
||||
'state' => 'CA',
|
||||
'postcode' => '90210',
|
||||
)
|
||||
) );
|
||||
$zone4 = \WC_Shipping_Zones::get_zone_matching_package( array(
|
||||
'destination' => array(
|
||||
'country' => 'US',
|
||||
'state' => 'AL',
|
||||
'postcode' => '12345',
|
||||
)
|
||||
) );
|
||||
|
||||
// Assert
|
||||
$this->assertEquals( $zone1->get_zone_name(), 'Local' );
|
||||
$this->assertEquals( $zone2->get_zone_name(), 'Europe' );
|
||||
$this->assertEquals( $zone3->get_zone_name(), 'California' );
|
||||
$this->assertEquals( $zone4->get_zone_name(), 'US' );
|
||||
|
||||
// Clean
|
||||
\WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue