WC_Stripe_level3_Data_Test   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B test_data_for_mutli_item_order() 0 79 1
A test_non_us_shipping_zip_codes() 0 40 1
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
		$store_postcode = '90210';
10
		update_option( 'woocommerce_store_postcode', $store_postcode );
11
12
		// Arrange: Create a couple of products to use.
13
		$variation_product = WC_Helper_Product::create_variation_product();
14
		$variation_ids     = $variation_product->get_children();
15
16
		$product_1 = wc_get_product ( $variation_ids[0] );
17
		$product_1->set_regular_price( 19.19 );
18
		$product_1->set_sale_price( 11.83 );
19
		$product_1->save();
20
21
		$product_2 = wc_get_product( $variation_ids[1] );
22
		$product_2->set_regular_price( 20.05 );
23
		$product_2->save();
24
25
		// Arrange: Set up an order with:
26
		// 1) A variation product.
27
		// 2) The same product added several times.
28
		// 3) A valid US ZIP code
29
		$order = new WC_Order();
30
		$order->set_shipping_postcode( '90210' );
31
		$order->add_product( $product_1, 1 ); // Add one item of the first product variation
32
		$order->add_product( $product_2, 2 ); // Add two items of the second product variation
33
34
		$order->save();
35
		$order->calculate_totals();
36
37
		// Act: Call get_level3_data_from_order().
38
		$gateway = new WC_Gateway_Stripe();
39
		$result = $gateway->get_level3_data_from_order( $order );
40
41
		// Assert.
42
		$this->assertEquals(
43
			array(
44
				'merchant_reference' => $order->get_id(),
45
				'shipping_address_zip' => $order->get_shipping_postcode(),
46
				'shipping_from_zip' => $store_postcode,
47
				'shipping_amount' => 0,
48
				'line_items' => array(
49
					(object) array(
50
						'product_code'        => (string) $product_1->get_id(),
51
						'product_description' => substr( $product_1->get_name(), 0, 26 ),
52
						'unit_cost'           => 1183,
53
						'quantity'            => 1,
54
						'tax_amount'          => 0,
55
						'discount_amount'     => 0,
56
					),
57
					(object) array(
58
						'product_code'        => (string) $product_2->get_id(),
59
						'product_description' => substr( $product_2->get_name(), 0, 26 ),
60
						'unit_cost'           => 2005,
61
						'quantity'            => 2,
62
						'tax_amount'          => 0,
63
						'discount_amount'     => 0,
64
					),
65
				),
66
			),
67
			$result
68
		);
69
70
		// Assert: Check that Stripe's total charge check passes.
71
		$total_charged = WC_Stripe_Helper::get_stripe_amount( $order->get_total() );
72
		$sum_of_unit_costs = array_reduce( $result['line_items'], function( $sum, $item ) {
73
			return $sum + $item->quantity * $item->unit_cost;
74
		}  );
75
		$sum_of_taxes = array_reduce( $result['line_items'], function( $sum, $item ) {
76
			return $sum + $item->tax_amount;
77
		}  );
78
		$sum_of_discounts = array_reduce( $result['line_items'], function( $sum, $item ) {
79
			return $sum + $item->discount_amount;
80
		}  );
81
		$shipping_amount = $result['shipping_amount'];
82
		$this->assertEquals(
83
			$total_charged,
84
			$sum_of_unit_costs + $sum_of_taxes - $sum_of_discounts + $shipping_amount
85
		);
86
	}
87
88
	public function test_non_us_shipping_zip_codes() {
89
		// Update the store with the right post code.
90
		update_option( 'woocommerce_store_postcode', 1040 );
91
92
		// Arrange: Create a couple of products to use.
93
		$product = WC_Helper_Product::create_simple_product();
94
		$product->set_regular_price( 19.19 );
95
		$product->save();
96
97
		// Arrange: Set up an order with a non-US postcode.
98
		$order = new WC_Order();
99
		$order->set_shipping_postcode( '1050' );
100
		$order->add_product( $product, 1 );
101
		$order->save();
102
		$order->calculate_totals();
103
104
		// Act: Call get_level3_data_from_order().
105
		$store_postcode = '1100';
0 ignored issues
show
Unused Code introduced by
$store_postcode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
106
		$gateway = new WC_Gateway_Stripe();
107
		$result = $gateway->get_level3_data_from_order( $order );
108
109
		// Assert.
110
		$this->assertEquals(
111
			array(
112
				'merchant_reference' => $order->get_id(),
113
				'shipping_amount' => 0,
114
				'line_items' => array(
115
					(object) array(
116
						'product_code'        => (string) $product->get_id(),
117
						'product_description' => substr( $product->get_name(), 0, 26 ),
118
						'unit_cost'           => 1919,
119
						'quantity'            => 1,
120
						'tax_amount'          => 0,
121
						'discount_amount'     => 0,
122
					),
123
				),
124
			),
125
			$result
126
		);
127
	}
128
}
129