2020-10-27 23:46:28 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Factory class for the WooCommerce's coupon data objects.
|
|
|
|
|
*
|
2021-02-26 09:29:19 -05:00
|
|
|
* @since v0.8.0
|
2020-10-27 23:46:28 -04:00
|
|
|
* @package Tests\WPGraphQL\WooCommerce\Factory
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Tests\WPGraphQL\WooCommerce\Factory;
|
|
|
|
|
|
|
|
|
|
use Tests\WPGraphQL\WooCommerce\Utils\Dummy;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Coupon factory class for testing.
|
|
|
|
|
*/
|
|
|
|
|
class CouponFactory extends \WP_UnitTest_Factory_For_Thing {
|
2022-02-04 15:00:44 -05:00
|
|
|
public function __construct( $factory = null ) {
|
2020-10-27 23:46:28 -04:00
|
|
|
parent::__construct( $factory );
|
|
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
$this->default_generation_definitions = [
|
2022-02-04 15:00:44 -05:00
|
|
|
'coupon_class' => '\WC_Coupon',
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-10-27 23:46:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create_object( $args ) {
|
2022-02-04 15:00:44 -05:00
|
|
|
if ( is_wp_error( $args ) ) {
|
|
|
|
|
codecept_debug( $args );
|
|
|
|
|
}
|
|
|
|
|
$coupon_class = $args['coupon_class'];
|
2020-10-27 23:46:28 -04:00
|
|
|
unset( $args['coupon_class'] );
|
|
|
|
|
|
|
|
|
|
$coupon = new $coupon_class();
|
|
|
|
|
|
2022-08-26 20:32:37 -04:00
|
|
|
$amount = Dummy::instance()->number( 25, 75 );
|
2020-10-27 23:46:28 -04:00
|
|
|
$coupon->set_props(
|
|
|
|
|
array_merge(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'code' => $amount . 'off',
|
|
|
|
|
'amount' => floatval( $amount ),
|
|
|
|
|
'date_expires' => null,
|
|
|
|
|
'discount_type' => 'percent',
|
|
|
|
|
'description' => 'Test coupon',
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2020-10-27 23:46:28 -04:00
|
|
|
$args
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Set meta data.
|
|
|
|
|
if ( ! empty( $args['meta_data'] ) ) {
|
|
|
|
|
$coupon->set_meta_data( $args['meta_data'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $coupon->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update_object( $object, $fields ) {
|
|
|
|
|
if ( ! $object instanceof \WC_Coupon && 0 !== absint( $object ) ) {
|
|
|
|
|
$object = $this->get_object_by_id( $object );
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 15:00:44 -05:00
|
|
|
foreach ( $fields as $field => $field_value ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
if ( ! is_callable( [ $object, "set_{$field}" ] ) ) {
|
2020-10-27 23:46:28 -04:00
|
|
|
throw new \Exception(
|
|
|
|
|
sprintf( '"%1$s" is not a valid %2$s coupon field.', $field, $object->get_type() )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$object->{"set_{$field}"}( $field_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$object->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function get_object_by_id( $id ) {
|
|
|
|
|
return new \WC_Coupon( $id );
|
|
|
|
|
}
|
|
|
|
|
}
|