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