Add factory for webhook unit tests

This commit is contained in:
Max Rice 2014-09-05 14:34:51 -04:00
parent 81b50b6bd6
commit 0fbba35c9d
5 changed files with 109 additions and 48 deletions

View File

@ -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' );
}

View File

@ -0,0 +1,28 @@
<?php
/**
* WC Unit Test Factory
*
* Provides WooCommerce-specific factories
*
* @since 2.2
*/
class WC_Unit_Test_Factory extends WP_UnitTest_Factory {
/** @var \WC_Unit_Test_Factory_For_Webhook */
public $webhook;
/** @var \WC_Unit_Test_Factory_For_Webhook_Delivery */
public $webhook_delivery;
/**
* Setup factories
*/
public function __construct() {
parent::__construct();
$this->webhook = new WC_Unit_Test_Factory_For_Webhook( $this );
$this->webhook_delivery = new WC_Unit_Test_Factory_For_Webhook_Delivery( $this );
}
}

View File

@ -1,30 +1,8 @@
<?php
/**
* WC Unit Test Factory
*
* Provides WooCommerce-specific factories
*
* @since 2.2
*/
class WC_Unit_Test_Factory extends WP_UnitTest_Factory {
/** @var \WC_Unit_Test_Factory_For_Webhook_Delivery */
public $webhook_delivery;
/**
* Setup factories
*/
public function __construct() {
parent::__construct();
$this->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 {

View File

@ -0,0 +1,71 @@
<?php
/**
* Webhook Test Factory
*
* @see \WP_UnitTest_Factory_For_Post
* @since 2.2
*/
class WC_Unit_Test_Factory_For_Webhook extends WP_UnitTest_Factory_For_Post {
/**
* Setup factory
*
* @since 2.2
* @param null $factory
*/
public function __construct( $factory = null ) {
parent::__construct( $factory );
// set default
$this->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 );
}
}

View File

@ -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 ) );