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