Merge pull request #13408 from woocommerce/fix-13396
Better invalid data store handling
This commit is contained in:
commit
c29f6bba50
|
@ -72,7 +72,7 @@ class WC_Data_Store {
|
||||||
if ( array_key_exists( $object_type, $this->stores ) ) {
|
if ( array_key_exists( $object_type, $this->stores ) ) {
|
||||||
$store = apply_filters( 'woocommerce_' . $object_type . '_data_store', $this->stores[ $object_type ] );
|
$store = apply_filters( 'woocommerce_' . $object_type . '_data_store', $this->stores[ $object_type ] );
|
||||||
if ( is_object( $store ) ) {
|
if ( is_object( $store ) ) {
|
||||||
if ( ! class_implements( $store, WC_Object_Data_Store_Interface::class ) ) {
|
if ( ! $store instanceof WC_Object_Data_Store_Interface ) {
|
||||||
throw new Exception( __( 'Invalid data store.', 'woocommerce' ) );
|
throw new Exception( __( 'Invalid data store.', 'woocommerce' ) );
|
||||||
}
|
}
|
||||||
$this->current_class_name = get_class( $store );
|
$this->current_class_name = get_class( $store );
|
||||||
|
@ -90,17 +90,13 @@ class WC_Data_Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a data store for us or returns null if an invalid store.
|
* Loads a data store.
|
||||||
*
|
*
|
||||||
* @param string $object_type Name of object.
|
* @param string $object_type Name of object.
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
*/
|
*/
|
||||||
public static function load( $object_type ) {
|
public static function load( $object_type ) {
|
||||||
try {
|
|
||||||
return new WC_Data_Store( $object_type );
|
return new WC_Data_Store( $object_type );
|
||||||
} catch ( Exception $e ) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,13 +23,18 @@ class WC_Tests_Data_Store extends WC_Unit_Test_Case {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure ::load returns null if an invalid store is found.
|
* Make sure ::load throws an exception for invalid data stores
|
||||||
*
|
*
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
*/
|
*/
|
||||||
function test_invalid_store_load_returns_null() {
|
function test_invalid_store_load_throws_exception() {
|
||||||
|
try {
|
||||||
$product_store = WC_Data_Store::load( 'does-not-exist' );
|
$product_store = WC_Data_Store::load( 'does-not-exist' );
|
||||||
$this->assertNull( $product_store );
|
} catch ( Exception $e ) {
|
||||||
|
$this->assertEquals( $e->getMessage(), 'Invalid data store.' );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->fail( 'Invalid data store exception not correctly raised.' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue