Skip to content

Commit e06984c

Browse files
Adds TaxRate as factory class. (#1)
* Adds TaxRate as factory class. * Update README.md * Update src/Factories/TaxRate.php Co-authored-by: ken-ll <67947342+ken-ll@users.noreply.github.com> Co-authored-by: ken-ll <67947342+ken-ll@users.noreply.github.com>
1 parent c6269e2 commit e06984c

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ $this->factory()->product->create_and_get(
158158
);
159159
```
160160

161+
### Tax rates
162+
163+
You can access the order factory by using `$this->factory()->tax_rate` within a WooCommerce integration test.
164+
165+
The main method you'll use is `create_and_get( $args )`. The input you can give to an order are the same as you can give to the product creation API endpoint.
166+
167+
`create_and_get($args)` returns an array, as tax rates have no data class/model within WooCommerce.
168+
169+
See https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-tax-rate
170+
171+
Example:
172+
173+
```php
174+
$this->factory()->tax_rate->create_and_get(
175+
array(
176+
'country'=>'NL',
177+
'rate'=> '21',
178+
'name'=>'BTW hoog tarief',
179+
'shipping'=>false,
180+
)
181+
);
182+
```
183+
161184
## Testcases
162185
For most testcases you will want to use `\LevelLevel\WPBrowserWooCommerce\WCTestCase`
163186

@@ -198,4 +221,4 @@ The main focus is on implementing more factories for other WooCommerce objects s
198221
After this, focus might shift to popular extensions for WooCommerce, such as Subscriptions or Bookings.
199222

200223
### Contributing
201-
Feel free to open issues or create pull requests if you feel something is missing or working incorrectly.
224+
Feel free to open issues or create pull requests if you feel something is missing or working incorrectly.

src/Factories/TaxRate.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

src/WC_UnitTest_Factory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use LevelLevel\WPBrowserWooCommerce\Factories\Product;
66
use LevelLevel\WPBrowserWooCommerce\Factories\Order;
7-
7+
use LevelLevel\WPBrowserWooCommerce\Factories\TaxRate;
88
use WP_UnitTest_Factory;
99

1010
class WC_UnitTest_Factory extends WP_UnitTest_Factory {
@@ -18,9 +18,15 @@ class WC_UnitTest_Factory extends WP_UnitTest_Factory {
1818
*/
1919
public $order;
2020

21+
/**
22+
* @var TaxRate
23+
*/
24+
public $tax_rate;
25+
2126
public function __construct() {
2227
parent::__construct();
2328
$this->product = new Product( $this );
2429
$this->order = new Order( $this );
30+
$this->tax_rate = new TaxRate( $this );
2531
}
2632
}

0 commit comments

Comments
 (0)