1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* These tests assert that get_level3_data_from_order() returns the correct |
4
|
|
|
* data. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
class WC_Stripe_level3_Data_Test extends WP_UnitTestCase { |
8
|
|
|
public function test_data_for_mutli_item_order() { |
9
|
|
|
// Skip this test because of the complexity of creating products in WC pre-3.0. |
10
|
|
|
if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) { |
11
|
|
|
// Dummy assertion. |
12
|
|
|
$this->assertEquals( WC_Stripe_Helper::is_wc_lt( '3.0' ), true ); |
13
|
|
|
return; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
$store_postcode = '90210'; |
17
|
|
|
update_option( 'woocommerce_store_postcode', $store_postcode ); |
18
|
|
|
|
19
|
|
|
// Arrange: Create a couple of products to use. |
20
|
|
|
$variation_product = WC_Helper_Product::create_variation_product(); |
21
|
|
|
$variation_ids = $variation_product->get_children(); |
22
|
|
|
|
23
|
|
|
$product_1 = wc_get_product ( $variation_ids[0] ); |
24
|
|
|
$product_1->set_regular_price( 19.19 ); |
25
|
|
|
$product_1->set_sale_price( 11.83 ); |
26
|
|
|
$product_1->save(); |
27
|
|
|
|
28
|
|
|
$product_2 = wc_get_product( $variation_ids[1] ); |
29
|
|
|
$product_2->set_regular_price( 20.05 ); |
30
|
|
|
$product_2->save(); |
31
|
|
|
|
32
|
|
|
// Arrange: Set up an order with: |
33
|
|
|
// 1) A variation product. |
34
|
|
|
// 2) The same product added several times. |
35
|
|
|
// 3) A valid US ZIP code |
36
|
|
|
$order = new WC_Order(); |
37
|
|
|
$order->set_shipping_postcode( '90210' ); |
38
|
|
|
$order->add_product( $product_1, 1 ); // Add one item of the first product variation |
39
|
|
|
$order->add_product( $product_2, 2 ); // Add two items of the second product variation |
40
|
|
|
|
41
|
|
|
$order->save(); |
42
|
|
|
$order->calculate_totals(); |
43
|
|
|
|
44
|
|
|
// Act: Call get_level3_data_from_order(). |
45
|
|
|
$gateway = new WC_Gateway_Stripe(); |
46
|
|
|
$result = $gateway->get_level3_data_from_order( $order ); |
47
|
|
|
|
48
|
|
|
// Assert. |
49
|
|
|
$this->assertEquals( |
50
|
|
|
array( |
51
|
|
|
'merchant_reference' => $order->get_id(), |
52
|
|
|
'shipping_address_zip' => $order->get_shipping_postcode(), |
53
|
|
|
'shipping_from_zip' => $store_postcode, |
54
|
|
|
'shipping_amount' => 0, |
55
|
|
|
'line_items' => array( |
56
|
|
|
(object) array( |
57
|
|
|
'product_code' => (string) $product_1->get_id(), |
58
|
|
|
'product_description' => substr( $product_1->get_name(), 0, 26 ), |
59
|
|
|
'unit_cost' => 1183, |
60
|
|
|
'quantity' => 1, |
61
|
|
|
'tax_amount' => 0, |
62
|
|
|
'discount_amount' => 0, |
63
|
|
|
), |
64
|
|
|
(object) array( |
65
|
|
|
'product_code' => (string) $product_2->get_id(), |
66
|
|
|
'product_description' => substr( $product_2->get_name(), 0, 26 ), |
67
|
|
|
'unit_cost' => 2005, |
68
|
|
|
'quantity' => 2, |
69
|
|
|
'tax_amount' => 0, |
70
|
|
|
'discount_amount' => 0, |
71
|
|
|
), |
72
|
|
|
), |
73
|
|
|
), |
74
|
|
|
$result |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
// Assert: Check that Stripe's total charge check passes. |
78
|
|
|
$total_charged = WC_Stripe_Helper::get_stripe_amount( $order->get_total() ); |
79
|
|
|
$sum_of_unit_costs = array_reduce( $result['line_items'], function( $sum, $item ) { |
80
|
|
|
return $sum + $item->quantity * $item->unit_cost; |
81
|
|
|
} ); |
82
|
|
|
$sum_of_taxes = array_reduce( $result['line_items'], function( $sum, $item ) { |
83
|
|
|
return $sum + $item->tax_amount; |
84
|
|
|
} ); |
85
|
|
|
$sum_of_discounts = array_reduce( $result['line_items'], function( $sum, $item ) { |
86
|
|
|
return $sum + $item->discount_amount; |
87
|
|
|
} ); |
88
|
|
|
$shipping_amount = $result['shipping_amount']; |
89
|
|
|
$this->assertEquals( |
90
|
|
|
$total_charged, |
91
|
|
|
$sum_of_unit_costs + $sum_of_taxes - $sum_of_discounts + $shipping_amount |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function test_non_us_shipping_zip_codes() { |
96
|
|
|
// Skip this test because of the complexity of creating products in WC pre-3.0. |
97
|
|
|
if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) { |
98
|
|
|
// Dummy assertion. |
99
|
|
|
$this->assertEquals( WC_Stripe_Helper::is_wc_lt( '3.0' ), true ); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Update the store with the right post code. |
104
|
|
|
update_option( 'woocommerce_store_postcode', 1040 ); |
105
|
|
|
|
106
|
|
|
// Arrange: Create a couple of products to use. |
107
|
|
|
$product = WC_Helper_Product::create_simple_product(); |
108
|
|
|
$product->set_regular_price( 19.19 ); |
109
|
|
|
$product->save(); |
110
|
|
|
|
111
|
|
|
// Arrange: Set up an order with a non-US postcode. |
112
|
|
|
$order = new WC_Order(); |
113
|
|
|
$order->set_shipping_postcode( '1050' ); |
114
|
|
|
$order->add_product( $product, 1 ); |
115
|
|
|
$order->save(); |
116
|
|
|
$order->calculate_totals(); |
117
|
|
|
|
118
|
|
|
// Act: Call get_level3_data_from_order(). |
119
|
|
|
$store_postcode = '1100'; |
|
|
|
|
120
|
|
|
$gateway = new WC_Gateway_Stripe(); |
121
|
|
|
$result = $gateway->get_level3_data_from_order( $order ); |
122
|
|
|
|
123
|
|
|
// Assert. |
124
|
|
|
$this->assertEquals( |
125
|
|
|
array( |
126
|
|
|
'merchant_reference' => $order->get_id(), |
127
|
|
|
'shipping_amount' => 0, |
128
|
|
|
'line_items' => array( |
129
|
|
|
(object) array( |
130
|
|
|
'product_code' => (string) $product->get_id(), |
131
|
|
|
'product_description' => substr( $product->get_name(), 0, 26 ), |
132
|
|
|
'unit_cost' => 1919, |
133
|
|
|
'quantity' => 1, |
134
|
|
|
'tax_amount' => 0, |
135
|
|
|
'discount_amount' => 0, |
136
|
|
|
), |
137
|
|
|
), |
138
|
|
|
), |
139
|
|
|
$result |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function test_pre_30_postal_code_omission() { |
144
|
|
|
if ( ! WC_Stripe_Helper::is_wc_lt( '3.0' ) ) { |
145
|
|
|
// Dummy assertion. |
146
|
|
|
$this->assertEquals( WC_Stripe_Helper::is_wc_lt( '3.0' ), false ); |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$order = new WC_Order(); |
151
|
|
|
$gateway = new WC_Gateway_Stripe(); |
152
|
|
|
$this->assertEquals( array(), $gateway->get_level3_data_from_order( $order ) ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.