From 65b98b77a38ea31b7d971903234208bcf71d820e Mon Sep 17 00:00:00 2001 From: Claudiu Lodromanean Date: Wed, 1 Mar 2017 08:58:03 -0800 Subject: [PATCH 1/2] Better class-enforcing checking --- includes/class-wc-data-store.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-data-store.php b/includes/class-wc-data-store.php index ad8995388c1..318edc49f59 100644 --- a/includes/class-wc-data-store.php +++ b/includes/class-wc-data-store.php @@ -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 ); From 1eafb487d7aeb0ae550561476609e2ce5a967f70 Mon Sep 17 00:00:00 2001 From: Claudiu Lodromanean Date: Wed, 1 Mar 2017 09:24:30 -0800 Subject: [PATCH 2/2] Allow invalid data store exceptions to bubble --- includes/class-wc-data-store.php | 8 ++------ tests/unit-tests/crud/data-store.php | 13 +++++++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/includes/class-wc-data-store.php b/includes/class-wc-data-store.php index 318edc49f59..282b35ccc78 100644 --- a/includes/class-wc-data-store.php +++ b/includes/class-wc-data-store.php @@ -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 ); } /** diff --git a/tests/unit-tests/crud/data-store.php b/tests/unit-tests/crud/data-store.php index 89ff7b1cd08..14a7908f95a 100644 --- a/tests/unit-tests/crud/data-store.php +++ b/tests/unit-tests/crud/data-store.php @@ -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.' ); } /**