From 0fbba35c9d5f7679075fd0f5def286b6f50587f3 Mon Sep 17 00:00:00 2001 From: Max Rice Date: Fri, 5 Sep 2014 14:34:51 -0400 Subject: [PATCH] Add factory for webhook unit tests --- tests/bootstrap.php | 9 ++- .../framework/class-wc-unit-test-factory.php | 28 ++++++++ ...nit-test-factory-for-webhook-delivery.php} | 24 +------ ...class-wc-unit-test-factory-for-webhook.php | 71 +++++++++++++++++++ tests/unit-tests/api/webhooks.php | 25 +------ 5 files changed, 109 insertions(+), 48 deletions(-) create mode 100644 tests/framework/class-wc-unit-test-factory.php rename tests/framework/{wc-unit-test-factory.php => factories/class-wc-unit-test-factory-for-webhook-delivery.php} (78%) create mode 100644 tests/framework/factories/class-wc-unit-test-factory-for-webhook.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c53624c6b2f..69bb3e3f1ea 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -84,8 +84,15 @@ class WC_Unit_Tests_Bootstrap { */ public function includes() { - require_once( $this->tests_dir . '/framework/wc-unit-test-factory.php' ); + // factories + require_once( $this->tests_dir . '/framework/factories/class-wc-unit-test-factory-for-webhook.php' ); + require_once( $this->tests_dir . '/framework/factories/class-wc-unit-test-factory-for-webhook-delivery.php' ); + + // framework + require_once( $this->tests_dir . '/framework/class-wc-unit-test-factory.php' ); require_once( $this->tests_dir . '/framework/class-wc-mock-session-handler.php' ); + + // test cases require_once( $this->tests_dir . '/framework/class-wc-unit-test-case.php' ); require_once( $this->tests_dir . '/framework/class-wc-api-unit-test-case.php' ); } diff --git a/tests/framework/class-wc-unit-test-factory.php b/tests/framework/class-wc-unit-test-factory.php new file mode 100644 index 00000000000..ab65acff3af --- /dev/null +++ b/tests/framework/class-wc-unit-test-factory.php @@ -0,0 +1,28 @@ +webhook = new WC_Unit_Test_Factory_For_Webhook( $this ); + $this->webhook_delivery = new WC_Unit_Test_Factory_For_Webhook_Delivery( $this ); + } + +} diff --git a/tests/framework/wc-unit-test-factory.php b/tests/framework/factories/class-wc-unit-test-factory-for-webhook-delivery.php similarity index 78% rename from tests/framework/wc-unit-test-factory.php rename to tests/framework/factories/class-wc-unit-test-factory-for-webhook-delivery.php index 74202330e30..f9686364acc 100644 --- a/tests/framework/wc-unit-test-factory.php +++ b/tests/framework/factories/class-wc-unit-test-factory-for-webhook-delivery.php @@ -1,30 +1,8 @@ webhook_delivery = new WC_Unit_Test_Factory_For_Webhook_Delivery( $this ); - } -} - /** * Webhook Delivery Test Factory * + * @see \WP_UnitTest_Factory_For_Comment * @since 2.2 */ class WC_Unit_Test_Factory_For_Webhook_Delivery extends WP_UnitTest_Factory_For_Comment { diff --git a/tests/framework/factories/class-wc-unit-test-factory-for-webhook.php b/tests/framework/factories/class-wc-unit-test-factory-for-webhook.php new file mode 100644 index 00000000000..777547abfa6 --- /dev/null +++ b/tests/framework/factories/class-wc-unit-test-factory-for-webhook.php @@ -0,0 +1,71 @@ +default_generation_definitions = array( + 'post_status' => 'publish', + 'post_title' => rand_str(), + 'post_type' => 'shop_webhook', + ); + } + + /** + * Create a mock webhook + * + * @since 2.2 + * @see WP_UnitTest_Factory_For_Post::create_object() + * @param array $args + * @return int webhook (post) ID + */ + public function create_object( $args ) { + + $id = parent::create_object( $args ); + + $meta_args = array( + '_topic' => 'coupon.created', + '_resource' => 'coupon', + '_event' => 'created', + '_hooks' => array( + 'woocommerce_process_shop_coupon_meta', + 'woocommerce_api_create_coupon', + ), + '_delivery_url' => 'http://requestb.in/Tt8675309', + ); + + foreach ( $meta_args as $key => $value ) { + update_post_meta( $id, $key, $value ); + } + + return $id; + } + + /** + * Get a mock webhook object + * + * @since 2.2 + * @see WP_UnitTest_Factory_For_Post::get_object_by_id() + * @param int $id webhook ID + * @return \WC_Webhook webhook instance + */ + public function get_object_by_id( $id ) { + + return new WC_Webhook( $id ); + } + +} diff --git a/tests/unit-tests/api/webhooks.php b/tests/unit-tests/api/webhooks.php index 8f8935e96a7..4926cb0741b 100644 --- a/tests/unit-tests/api/webhooks.php +++ b/tests/unit-tests/api/webhooks.php @@ -28,31 +28,8 @@ class WC_Tests_Webhooks extends WC_API_Unit_Test_Case { $this->endpoint = WC()->api->WC_API_Webhooks; - $post_args = array( - 'post_type' => 'shop_webhook', - 'post_status' => 'publish', - 'post_title' => rand_str(), - ); - - $post_id = $this->factory->post->create( $post_args ); - - $meta_args = array( - '_topic' => 'coupon.created', - '_resource' => 'coupon', - '_event' => 'created', - '_hooks' => array( - 'woocommerce_process_shop_coupon_meta', - 'woocommerce_api_create_coupon', - ), - '_delivery_url' => rand_str(), - ); - - foreach ( $meta_args as $key => $value ) { - update_post_meta( $post_id, $key, $value ); - } - // mock webhook - $this->webhook = new WC_Webhook( $post_id ); + $this->webhook = $this->factory->webhook->create_and_get(); // mock webhook delivery $this->webhook_delivery_id = $this->factory->webhook_delivery->create( array( 'comment_post_ID' => $this->webhook->id ) );