Skip to content

Commit 1443e9b

Browse files
Adds a trait for API-calls. (#6)
Simplifies editing and processing of api calls. Cleans classes to only their own functional implementation. Fixes #5
1 parent 84e5699 commit 1443e9b

File tree

6 files changed

+56
-175
lines changed

6 files changed

+56
-175
lines changed

src/Factories/APICalling.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce\Factories;
4+
5+
use Exception;
6+
use WC_API_Server;
7+
use WP_REST_Request;
8+
use WP_REST_Response;
9+
10+
trait APICalling{
11+
private function api_call_setup(): void{
12+
$this->old_user = get_current_user_id();
13+
14+
// Setup the administrator user so we can actually retrieve the order.
15+
$user = new \WP_User( 1 );
16+
wp_set_current_user( $user->ID );
17+
18+
WC()->api->includes();
19+
WC()->api->register_resources( new WC_API_Server( '/' ) );
20+
}
21+
22+
private function do_request( WP_REST_Request $request ): WP_REST_Response{
23+
$this->api_call_setup();
24+
$response = rest_do_request( $request );
25+
$this->api_call_teardown();
26+
if ( $response->is_error() ) {
27+
throw new Exception( $response->get_data()['message'] );
28+
}
29+
return $response;
30+
}
31+
32+
private function api_call_teardown(): void{
33+
wp_set_current_user( $this->old_user );
34+
}
35+
}

src/Factories/Coupon.php

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use WP_UnitTest_Factory_For_Thing;
99

1010
class Coupon extends WP_UnitTest_Factory_For_Thing {
11+
use APICalling;
12+
1113
/**
1214
* Creates a coupon. Using the API method.
1315
*
@@ -16,19 +18,11 @@ class Coupon extends WP_UnitTest_Factory_For_Thing {
1618
* @return int
1719
*/
1820
public function create_object( $args ) {
19-
$this->api_call_setup();
20-
2121
$request = new \WP_REST_Request( 'post', '/wc/v3/coupons' );
2222
$request->add_header( 'Content-Type', 'application/json' );
23-
2423
$request->set_body( json_encode( $args ) ); //phpcs:ignore
25-
$response = rest_do_request( $request );
26-
27-
$this->api_call_teardown();
2824

29-
if ( $response->is_error() ) {
30-
throw new Exception( $response->get_data()['message'] );
31-
}
25+
$response = $this->do_request( $request );
3226
return $response->get_data()['id'];
3327
}
3428

@@ -44,20 +38,12 @@ public function update_object( $object, $fields ) {
4438
if ( ! is_int( $object ) ) {
4539
throw new TypeError( '$object must be an int' );
4640
}
47-
$this->api_call_setup();
4841

4942
$request = new \WP_REST_Request( 'put', '/wc/v3/coupons/' . $object );
5043
$request->add_header( 'Content-Type', 'application/json' );
51-
5244
$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-
}
6045

46+
$response = $this->do_request( $request );
6147
return $response->get_data()['id'];
6248
}
6349

@@ -75,17 +61,4 @@ public function get_object_by_id( $object_id ) {
7561
}
7662
return $coupon;
7763
}
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-
}
9164
}

src/Factories/Order.php

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use Exception;
66
use TypeError;
7-
use WC_API_Server;
87
use WC_Order;
98
use WP_UnitTest_Factory_For_Thing;
109

1110
class Order extends WP_UnitTest_Factory_For_Thing {
11+
use APICalling;
1212
/**
1313
* Creates a product. Using the API method.
1414
*
@@ -17,20 +17,11 @@ class Order extends WP_UnitTest_Factory_For_Thing {
1717
* @return int
1818
*/
1919
public function create_object( $args ) {
20-
$this->api_call_setup();
21-
2220
$request = new \WP_REST_Request( 'post', '/wc/v3/orders' );
2321
$request->add_header( 'Content-Type', 'application/json' );
24-
2522
$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-
}
3323

24+
$response = $this->do_request( $request );
3425
return $response->get_data()['id'];
3526
}
3627

@@ -46,20 +37,12 @@ public function update_object( $object, $fields ) {
4637
if ( ! is_int( $object ) ) {
4738
throw new TypeError( '$object must be an int' );
4839
}
49-
$this->api_call_setup();
5040

5141
$request = new \WP_REST_Request( 'put', '/wc/v3/orders/' . $object );
5242
$request->add_header( 'Content-Type', 'application/json' );
53-
5443
$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-
44+
45+
$response = $this->do_request( $request );
6346
return $response->get_data()['id'];
6447
}
6548

@@ -77,20 +60,4 @@ public function get_object_by_id( $object_id ) {
7760
}
7861
return $order;
7962
}
80-
81-
private function api_call_setup() {
82-
$this->old_user = get_current_user_id();
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->ID );
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-
}
9663
}

src/Factories/Product.php

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Exception;
66
use TypeError;
7-
use WC_API_Server;
87
use WC_Product;
9-
use WP_Error;
108
use WP_UnitTest_Factory_For_Thing;
119

1210
class Product extends WP_UnitTest_Factory_For_Thing {
11+
use APICalling;
12+
1313
/**
1414
* Creates a product. Using the API method.
1515
*
@@ -18,19 +18,11 @@ class Product extends WP_UnitTest_Factory_For_Thing {
1818
* @return int
1919
*/
2020
public function create_object( $args ) {
21-
$this->api_call_setup();
22-
2321
$request = new \WP_REST_Request( 'post', '/wc/v3/products' );
2422
$request->add_header( 'Content-Type', 'application/json' );
25-
2623
$request->set_body( json_encode( $args ) ); //phpcs:ignore
27-
$response = rest_do_request( $request );
28-
29-
$this->api_call_teardown();
3024

31-
if ( $response->is_error() ) {
32-
throw new Exception( $response->get_data()['message'] );
33-
}
25+
$response = $this->do_request( $request );
3426
return $response->get_data()['id'];
3527
}
3628

@@ -46,20 +38,12 @@ public function update_object( $object, $fields ) {
4638
if ( ! is_int( $object ) ) {
4739
throw new TypeError( '$object must be an int' );
4840
}
49-
$this->api_call_setup();
5041

5142
$request = new \WP_REST_Request( 'put', '/wc/v3/products/' . $object );
5243
$request->add_header( 'Content-Type', 'application/json' );
53-
5444
$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-
45+
46+
$response = $this->do_request( $request );
6347
return $response->get_data()['id'];
6448
}
6549

@@ -77,17 +61,4 @@ public function get_object_by_id( $object_id ) {
7761
}
7862
return $product;
7963
}
80-
81-
private function api_call_setup() {
82-
$this->old_user = get_current_user_id();
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->ID );
87-
}
88-
89-
90-
private function api_call_teardown() {
91-
wp_set_current_user( $this->old_user );
92-
}
9364
}

src/Factories/Subscription.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use WP_UnitTest_Factory_For_Thing;
99

1010
class Subscription extends WP_UnitTest_Factory_For_Thing {
11+
use APICalling;
1112
/**
1213
* Creates a subscription. Using the API method.
1314
*
@@ -16,19 +17,11 @@ class Subscription extends WP_UnitTest_Factory_For_Thing {
1617
* @return int
1718
*/
1819
public function create_object( $args ) {
19-
$this->api_call_setup();
20-
2120
$request = new \WP_REST_Request( 'post', '/wc/v1/subscriptions' );
2221
$request->add_header( 'Content-Type', 'application/json' );
23-
2422
$request->set_body( json_encode( $args ) ); //phpcs:ignore
25-
$response = rest_do_request( $request );
2623

27-
$this->api_call_teardown();
28-
29-
if ( $response->is_error() ) {
30-
throw new Exception( $response->get_data()['message'] );
31-
}
24+
$response = $this->do_request( $request );
3225
return $response->get_data()['id'];
3326
}
3427

@@ -44,20 +37,12 @@ public function update_object( $object, $fields ) {
4437
if ( ! is_int( $object ) ) {
4538
throw new TypeError( '$object must be an int' );
4639
}
47-
$this->api_call_setup();
4840

4941
$request = new \WP_REST_Request( 'put', '/wc/v1/subscriptions/' . $object );
5042
$request->add_header( 'Content-Type', 'application/json' );
51-
5243
$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-
}
6044

45+
$response = $this->do_request( $request );
6146
return $response->get_data()['id'];
6247
}
6348

@@ -75,17 +60,4 @@ public function get_object_by_id( $object_id ) {
7560
}
7661
return $subscription;
7762
}
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-
}
9163
}

0 commit comments

Comments
 (0)