Skip to content

Commit e5ab336

Browse files
authored
Merge pull request #3 from level-level/feature/coupon_factory
add coupon factory
2 parents 45dcf5c + 4f47321 commit e5ab336

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,31 @@ $this->factory()->tax_rate->create_and_get(
181181
);
182182
```
183183

184+
### Coupons
185+
186+
You can access the coupon factory by using `$this->factory()->coupon` within a WooCommerce integration test.
187+
188+
The main method you'll use is `create_and_get( $args )`. The input you can give to a coupon are the same as you can give to the coupon creation API endpoint.
189+
190+
`create_and_get($args)` returns the result of `new WC_Coupon( $coupon_id )` for the created object.
191+
192+
See https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-coupon
193+
194+
Example:
195+
196+
```php
197+
$this->factory()->coupon->create_and_get(
198+
array(
199+
'code' => '25off',
200+
'discount_type' => 'percent',
201+
'amount' => '10',
202+
'individual_use' => true,
203+
'exclude_sale_items' => true,
204+
'minimum_amount' => '100.00',
205+
)
206+
);
207+
```
208+
184209
## Testcases
185210
For most testcases you will want to use `\LevelLevel\WPBrowserWooCommerce\WCTestCase`
186211

src/Factories/Coupon.php

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

src/WC_UnitTest_Factory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LevelLevel\WPBrowserWooCommerce;
44

5+
use LevelLevel\WPBrowserWooCommerce\Factories\Coupon;
56
use LevelLevel\WPBrowserWooCommerce\Factories\Product;
67
use LevelLevel\WPBrowserWooCommerce\Factories\Order;
78
use LevelLevel\WPBrowserWooCommerce\Factories\TaxRate;
@@ -25,8 +26,9 @@ class WC_UnitTest_Factory extends WP_UnitTest_Factory {
2526

2627
public function __construct() {
2728
parent::__construct();
28-
$this->product = new Product( $this );
29-
$this->order = new Order( $this );
30-
$this->tax_rate = new TaxRate( $this );
29+
$this->product = new Product( $this );
30+
$this->order = new Order( $this );
31+
$this->tax_rate = new TaxRate( $this );
32+
$this->coupon = new Coupon( $this );
3133
}
3234
}

0 commit comments

Comments
 (0)