Merge pull request #13408 from woocommerce/fix-13396

Better invalid data store handling
This commit is contained in:
Mike Jolley 2017-03-02 12:46:51 +00:00 committed by GitHub
commit c29f6bba50
2 changed files with 12 additions and 11 deletions

View File

@ -72,7 +72,7 @@ class WC_Data_Store {
if ( array_key_exists( $object_type, $this->stores ) ) {
$store = apply_filters( 'woocommerce_' . $object_type . '_data_store', $this->stores[ $object_type ] );
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' ) );
}
$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.
* @since 2.7.0
*/
public static function load( $object_type ) {
try {
return new WC_Data_Store( $object_type );
} catch ( Exception $e ) {
return null;
}
return new WC_Data_Store( $object_type );
}
/**

View File

@ -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
*/
function test_invalid_store_load_returns_null() {
$product_store = WC_Data_Store::load( 'does-not-exist' );
$this->assertNull( $product_store );
function test_invalid_store_load_throws_exception() {
try {
$product_store = WC_Data_Store::load( 'does-not-exist' );
} catch ( Exception $e ) {
$this->assertEquals( $e->getMessage(), 'Invalid data store.' );
return;
}
$this->fail( 'Invalid data store exception not correctly raised.' );
}
/**