Skip to content

Commit acc9379

Browse files
committed
Adds helpers.
1 parent 7c8914c commit acc9379

File tree

6 files changed

+259
-1
lines changed

6 files changed

+259
-1
lines changed

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
"description": "WooCommerce object factories for WP Browser integration tests.",
44
"type": "library",
55
"license": "MIT",
6-
"require": {}
6+
"require": {
7+
"lucatume/wp-browser": "^3.0"
8+
},
9+
"autoload": {
10+
"psr-4":{
11+
"LevelLevel\\WPBrowserWooCommerce\\": "src"
12+
}
13+
}
714
}

src/Factories/Order.php

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

src/Factories/Product.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce\Factories;
4+
5+
use Exception;
6+
use TypeError;
7+
use WC_API_Server;
8+
use WC_Product;
9+
use WP_Error;
10+
use WP_UnitTest_Factory_For_Thing;
11+
12+
class Product extends WP_UnitTest_Factory_For_Thing {
13+
/**
14+
* Creates a product. Using the API method.
15+
*
16+
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product
17+
*
18+
* @return int
19+
*/
20+
public function create_object( $args ) {
21+
$this->api_call_setup();
22+
23+
$request = new \WP_REST_Request( 'post', '/wc/v3/products' );
24+
$request->add_header( 'Content-Type', 'application/json' );
25+
26+
$request->set_body( json_encode( $args ) ); //phpcs:ignore
27+
$response = rest_do_request( $request );
28+
29+
$this->api_call_teardown();
30+
31+
if ( $response->is_error() ) {
32+
throw new Exception( $response->get_data()['message'] );
33+
}
34+
return $response->get_data()['id'];
35+
}
36+
37+
/**
38+
* Updates a product.
39+
*
40+
* @param int $object Product ID.
41+
* @param array $args See https://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-product
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/products/' . $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 product.
68+
*
69+
* @param int $object_id The product ID.
70+
*
71+
* @return WC_Product
72+
*/
73+
public function get_object_by_id( $object_id ) {
74+
$product = wc_get_product( $object_id );
75+
if ( ! $product instanceof WC_Product ) {
76+
throw new Exception( 'Could not retrieve product with ID ' . $object_id );
77+
}
78+
return $product;
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+
89+
90+
private function api_call_teardown() {
91+
wp_set_current_user( $this->old_user );
92+
}
93+
}

src/WCAjaxTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce;
4+
5+
use Codeception\TestCase\WPAjaxTestCase;
6+
7+
class WCAjaxTestCase extends WPAjaxTestCase {
8+
/**
9+
* @return WC_UnitTest_Factory
10+
*/
11+
protected static function factory() {
12+
static $factory = null;
13+
if ( ! $factory ) {
14+
$factory = new WC_UnitTest_Factory();
15+
}
16+
return $factory;
17+
}
18+
}

src/WCTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce;
4+
5+
use Codeception\TestCase\WPTestCase;
6+
7+
class WCTestCase extends WPTestCase {
8+
/**
9+
* @return WC_UnitTest_Factory
10+
*/
11+
protected static function factory() {
12+
static $factory = null;
13+
if ( ! $factory ) {
14+
$factory = new WC_UnitTest_Factory();
15+
}
16+
return $factory;
17+
}
18+
}

src/WC_UnitTest_Factory.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace LevelLevel\WPBrowserWooCommerce;
4+
5+
use LevelLevel\WPBrowserWooCommerce\Factories\Product;
6+
use LevelLevel\WPBrowserWooCommerce\Factories\Order;
7+
8+
use WP_UnitTest_Factory;
9+
10+
class WC_UnitTest_Factory extends WP_UnitTest_Factory {
11+
/**
12+
* @var Product
13+
*/
14+
public $product;
15+
16+
/**
17+
* @var Order
18+
*/
19+
public $order;
20+
21+
public function __construct() {
22+
parent::__construct();
23+
$this->product = new Product( $this );
24+
$this->order = new Order( $this );
25+
}
26+
}

0 commit comments

Comments
 (0)