Issues (334)

unit-tests/Helpers/OrderHelper.php (24 issues)

1
<?php
2
/**
3
 * Orders helper.
4
 */
5
6
namespace Automattic\WooCommerce\RestApi\UnitTests\Helpers;
7
8
defined( 'ABSPATH' ) || exit;
9
10
use \WC_Tax;
11
use \WC_Shipping_Rate;
12
use \WC_Order_Item_Shipping;
13
use \WC_Order_Item_Product;
14
15
/**
16
 * Class OrderHelper.
17
 *
18
 * This helper class should ONLY be used for unit tests!.
19
 */
20
class OrderHelper {
21
22
	/**
23
	 * Delete a product.
24
	 *
25
	 * @param int $order_id ID of the order to delete.
26
	 */
27
	public static function delete_order( $order_id ) {
28
29
		$order = wc_get_order( $order_id );
30
31
		// Delete all products in the order.
32
		foreach ( $order->get_items() as $item ) {
33
			\Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::delete_product( $item['product_id'] );
34
		}
35
36
		ShippingHelper::delete_simple_flat_rate();
37
38
		// Delete the order post.
39
		$order->delete( true );
40
	}
41
42
	/**
43
	 * Create a order.
44
	 *
45
	 * @since   2.4
46
	 * @version 3.0 New parameter $product.
47
	 *
48
	 * @param int        $customer_id
49
	 * @param WC_Product $product
0 ignored issues
show
The type Automattic\WooCommerce\R...ests\Helpers\WC_Product was not found. Did you mean WC_Product? If so, make sure to prefix the type with \.
Loading history...
50
	 *
51
	 * @return WC_Order
0 ignored issues
show
The type Automattic\WooCommerce\R...tTests\Helpers\WC_Order was not found. Did you mean WC_Order? If so, make sure to prefix the type with \.
Loading history...
52
	 */
53
	public static function create_order( $customer_id = 1, $product = null ) {
54
55
		if ( ! is_a( $product, 'WC_Product' ) ) {
56
			$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
57
		}
58
59
		ShippingHelper::create_simple_flat_rate();
60
61
		$order_data = array(
62
			'status'        => 'pending',
63
			'customer_id'   => $customer_id,
64
			'customer_note' => '',
65
			'total'         => '',
66
		);
67
68
		$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception
69
		$order                  = wc_create_order( $order_data );
70
71
		// Add order products
72
		$item = new WC_Order_Item_Product();
73
		$item->set_props(
74
			array(
75
				'product'  => $product,
76
				'quantity' => 4,
77
				'subtotal' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ),
78
				'total'    => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ),
79
			)
80
		);
81
		$item->save();
82
		$order->add_item( $item );
0 ignored issues
show
The method add_item() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
		$order->/** @scrutinizer ignore-call */ 
83
          add_item( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
84
		// Set billing address
85
		$order->set_billing_first_name( 'Jeroen' );
0 ignored issues
show
The method set_billing_first_name() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
		$order->/** @scrutinizer ignore-call */ 
86
          set_billing_first_name( 'Jeroen' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
		$order->set_billing_last_name( 'Sormani' );
0 ignored issues
show
The method set_billing_last_name() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
		$order->/** @scrutinizer ignore-call */ 
87
          set_billing_last_name( 'Sormani' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
		$order->set_billing_company( 'WooCompany' );
0 ignored issues
show
The method set_billing_company() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
		$order->/** @scrutinizer ignore-call */ 
88
          set_billing_company( 'WooCompany' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
		$order->set_billing_address_1( 'WooAddress' );
0 ignored issues
show
The method set_billing_address_1() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
		$order->/** @scrutinizer ignore-call */ 
89
          set_billing_address_1( 'WooAddress' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
		$order->set_billing_address_2( '' );
0 ignored issues
show
The method set_billing_address_2() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
		$order->/** @scrutinizer ignore-call */ 
90
          set_billing_address_2( '' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
		$order->set_billing_city( 'WooCity' );
0 ignored issues
show
The method set_billing_city() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
		$order->/** @scrutinizer ignore-call */ 
91
          set_billing_city( 'WooCity' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
		$order->set_billing_state( 'NY' );
0 ignored issues
show
The method set_billing_state() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
		$order->/** @scrutinizer ignore-call */ 
92
          set_billing_state( 'NY' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
		$order->set_billing_postcode( '123456' );
0 ignored issues
show
The method set_billing_postcode() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
		$order->/** @scrutinizer ignore-call */ 
93
          set_billing_postcode( '123456' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
		$order->set_billing_country( 'US' );
0 ignored issues
show
The method set_billing_country() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
		$order->/** @scrutinizer ignore-call */ 
94
          set_billing_country( 'US' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
		$order->set_billing_email( '[email protected]' );
0 ignored issues
show
The method set_billing_email() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
		$order->/** @scrutinizer ignore-call */ 
95
          set_billing_email( '[email protected]' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
		$order->set_billing_phone( '555-32123' );
0 ignored issues
show
The method set_billing_phone() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
		$order->/** @scrutinizer ignore-call */ 
96
          set_billing_phone( '555-32123' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
97
		// Add shipping costs
98
		$shipping_taxes = WC_Tax::calc_shipping_tax( '10', WC_Tax::get_shipping_tax_rates() );
0 ignored issues
show
'10' of type string is incompatible with the type double expected by parameter $price of WC_Tax::calc_shipping_tax(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
		$shipping_taxes = WC_Tax::calc_shipping_tax( /** @scrutinizer ignore-type */ '10', WC_Tax::get_shipping_tax_rates() );
Loading history...
99
		$rate           = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate' );
100
		$item           = new WC_Order_Item_Shipping();
101
		$item->set_props(
102
			array(
103
				'method_title' => $rate->label,
104
				'method_id'    => $rate->id,
105
				'total'        => wc_format_decimal( $rate->cost ),
106
				'taxes'        => $rate->taxes,
107
			)
108
		);
109
		foreach ( $rate->get_meta_data() as $key => $value ) {
110
			$item->add_meta_data( $key, $value, true );
111
		}
112
		$order->add_item( $item );
113
114
		// Set payment gateway
115
		$payment_gateways = WC()->payment_gateways->payment_gateways();
0 ignored issues
show
The method payment_gateways() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
		/** @scrutinizer ignore-call */ 
116
  $payment_gateways = WC()->payment_gateways->payment_gateways();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
		$order->set_payment_method( $payment_gateways['bacs'] );
0 ignored issues
show
The method set_payment_method() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
		$order->/** @scrutinizer ignore-call */ 
117
          set_payment_method( $payment_gateways['bacs'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
		// Set totals
119
		$order->set_shipping_total( 10 );
0 ignored issues
show
The method set_shipping_total() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
		$order->/** @scrutinizer ignore-call */ 
120
          set_shipping_total( 10 );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
		$order->set_discount_total( 0 );
0 ignored issues
show
The method set_discount_total() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
		$order->/** @scrutinizer ignore-call */ 
121
          set_discount_total( 0 );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
		$order->set_discount_tax( 0 );
0 ignored issues
show
The method set_discount_tax() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
		$order->/** @scrutinizer ignore-call */ 
122
          set_discount_tax( 0 );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
		$order->set_cart_tax( 0 );
0 ignored issues
show
The method set_cart_tax() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
		$order->/** @scrutinizer ignore-call */ 
123
          set_cart_tax( 0 );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
		$order->set_shipping_tax( 0 );
0 ignored issues
show
The method set_shipping_tax() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
		$order->/** @scrutinizer ignore-call */ 
124
          set_shipping_tax( 0 );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
		$order->set_total( 50 ); // 4 x $10 simple helper product
0 ignored issues
show
The method set_total() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
		$order->/** @scrutinizer ignore-call */ 
125
          set_total( 50 ); // 4 x $10 simple helper product

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
		$order->save();
0 ignored issues
show
The method save() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
		$order->/** @scrutinizer ignore-call */ 
126
          save();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
127
		return $order;
128
	}
129
}
130