|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LevelLevel\WPBrowserWooCommerce\Factories; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use TypeError; |
| 7 | +use WC_Coupon; |
| 8 | +use WP_UnitTest_Factory_For_Thing; |
| 9 | + |
| 10 | +class Coupon extends WP_UnitTest_Factory_For_Thing { |
| 11 | + /** |
| 12 | + * Creates a coupon. Using the API method. |
| 13 | + * |
| 14 | + * @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-coupon |
| 15 | + * |
| 16 | + * @return int |
| 17 | + */ |
| 18 | + public function create_object( $args ) { |
| 19 | + $this->api_call_setup(); |
| 20 | + |
| 21 | + $request = new \WP_REST_Request( 'post', '/wc/v3/coupons' ); |
| 22 | + $request->add_header( 'Content-Type', 'application/json' ); |
| 23 | + |
| 24 | + $request->set_body( json_encode( $args ) ); //phpcs:ignore |
| 25 | + $response = rest_do_request( $request ); |
| 26 | + |
| 27 | + $this->api_call_teardown(); |
| 28 | + |
| 29 | + if ( $response->is_error() ) { |
| 30 | + throw new Exception( $response->get_data()['message'] ); |
| 31 | + } |
| 32 | + return $response->get_data()['id']; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Updates a coupon. |
| 37 | + * |
| 38 | + * @param int $object Coupon ID. |
| 39 | + * @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-coupon |
| 40 | + * |
| 41 | + * @return int |
| 42 | + */ |
| 43 | + public function update_object( $object, $fields ) { |
| 44 | + if ( ! is_int( $object ) ) { |
| 45 | + throw new TypeError( '$object must be an int' ); |
| 46 | + } |
| 47 | + $this->api_call_setup(); |
| 48 | + |
| 49 | + $request = new \WP_REST_Request( 'put', '/wc/v3/coupons/' . $object ); |
| 50 | + $request->add_header( 'Content-Type', 'application/json' ); |
| 51 | + |
| 52 | + $request->set_body( json_encode( $fields ) ); //phpcs:ignore |
| 53 | + $response = rest_do_request( $request ); |
| 54 | + |
| 55 | + $this->api_call_teardown(); |
| 56 | + |
| 57 | + if ( $response->is_error() ) { |
| 58 | + throw new Exception( $response->get_data()['message'] ); |
| 59 | + } |
| 60 | + |
| 61 | + return $response->get_data()['id']; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets a woocommerce coupon. |
| 66 | + * |
| 67 | + * @param int $object_id The coupon ID. |
| 68 | + * |
| 69 | + * @return WC_Coupon |
| 70 | + */ |
| 71 | + public function get_object_by_id( $object_id ) { |
| 72 | + $coupon = new WC_Coupon( $object_id ); |
| 73 | + if ( ! $coupon instanceof WC_Coupon ) { |
| 74 | + throw new Exception( 'Could not retrieve coupon with ID ' . $object_id ); |
| 75 | + } |
| 76 | + return $coupon; |
| 77 | + } |
| 78 | + |
| 79 | + private function api_call_setup() { |
| 80 | + $this->old_user = get_current_user_id(); |
| 81 | + |
| 82 | + // Setup the administrator user so we can actually retrieve the order. |
| 83 | + $user = new \WP_User( 1 ); |
| 84 | + wp_set_current_user( $user->ID ); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + private function api_call_teardown() { |
| 89 | + wp_set_current_user( $this->old_user ); |
| 90 | + } |
| 91 | +} |
0 commit comments