PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Module / WeightCalculatorTest.php

WeightCalculatorTest.php in Packeta 2.1, at tests/Module/WeightCalculatorTest.php

235 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Tests\Module;
6
7 use Packetery\Module\Framework\WcAdapter;
8 use Packetery\Module\Options\OptionsProvider;
9 use Packetery\Module\WeightCalculator;
10 use PHPUnit\Framework\MockObject\Exception;
11 use PHPUnit\Framework\TestCase;
12 use WC_Order;
13 use WC_Order_Item_Product;
14 use WC_Product;
15
16 class WeightCalculatorTest extends TestCase {
17 private function createOrderItemMock( float $productQuantity, float $weight ): WC_Order_Item_Product {
18 $wcProduct = $this->createMock( WC_Product::class );
19 $wcProduct
20 ->method( 'get_weight' )
21 ->willReturn( $weight );
22
23 $wcOrderItemProduct = $this->createMock( WC_Order_Item_Product::class );
24 $wcOrderItemProduct
25 ->method( 'get_product' )
26 ->willReturn( $wcProduct );
27 $wcOrderItemProduct
28 ->method( 'get_quantity' )
29 ->willReturn( $productQuantity );
30
31 return $wcOrderItemProduct;
32 }
33
34 public static function getOrderWeightCalculationProvider(): array {
35 return [
36 'no items, only packaging weight' => [
37 'isDefaultWeightEnabled' => true,
38 'defaultWeight' => 1.3,
39 'packagingWeight' => 0.1,
40 'expectedWeight' => 0.1,
41 'orderItems' => [],
42 ],
43 'no items, no packaging weight' => [
44 'isDefaultWeightEnabled' => true,
45 'defaultWeight' => 100,
46 'packagingWeight' => 0,
47 'expectedWeight' => 0,
48 'orderItems' => [],
49 ],
50 'no items, only high packaging weight' => [
51 'isDefaultWeightEnabled' => true,
52 'defaultWeight' => 0,
53 'packagingWeight' => 100,
54 'expectedWeight' => 100,
55 'orderItems' => [],
56 ],
57 'no items, no weight at all' => [
58 'isDefaultWeightEnabled' => true,
59 'defaultWeight' => 0,
60 'packagingWeight' => 0,
61 'expectedWeight' => 0,
62 'orderItems' => [],
63 ],
64 'no items, small packaging weight' => [
65 'isDefaultWeightEnabled' => true,
66 'defaultWeight' => 1.0,
67 'packagingWeight' => 0.1,
68 'expectedWeight' => 0.1,
69 'orderItems' => [],
70 ],
71 'no items, small packaging weight duplicate' => [
72 'isDefaultWeightEnabled' => true,
73 'defaultWeight' => 1.0,
74 'packagingWeight' => 0.1,
75 'expectedWeight' => 0.1,
76 'orderItems' => [],
77 ],
78 'one item, with packaging weight' => [
79 'isDefaultWeightEnabled' => true,
80 'defaultWeight' => 0.0,
81 'packagingWeight' => 0.1,
82 'expectedWeight' => 2.1,
83 'orderItems' => [
84 [
85 'productQuantity' => 1,
86 'weight' => 2,
87 ],
88 ],
89 ],
90 'one item, default weight disabled' => [
91 'isDefaultWeightEnabled' => false,
92 'defaultWeight' => 10.0,
93 'packagingWeight' => 0.1,
94 'expectedWeight' => 2.1,
95 'orderItems' => [
96 [
97 'productQuantity' => 1,
98 'weight' => 2,
99 ],
100 ],
101 ],
102 'one item, no packaging weight' => [
103 'isDefaultWeightEnabled' => true,
104 'defaultWeight' => 10.0,
105 'packagingWeight' => 0.0,
106 'expectedWeight' => 2.0,
107 'orderItems' => [
108 [
109 'productQuantity' => 1,
110 'weight' => 2,
111 ],
112 ],
113 ],
114 'two items, high packaging weight' => [
115 'isDefaultWeightEnabled' => true,
116 'defaultWeight' => 10.0,
117 'packagingWeight' => 20.0,
118 'expectedWeight' => 24.4,
119 'orderItems' => [
120 [
121 'productQuantity' => 2,
122 'weight' => 2.2,
123 ],
124 ],
125 ],
126 'two items, one with zero quantity' => [
127 'isDefaultWeightEnabled' => true,
128 'defaultWeight' => 10.0,
129 'packagingWeight' => 20.0,
130 'expectedWeight' => 24.4,
131 'orderItems' => [
132 [
133 'productQuantity' => 2,
134 'weight' => 2.2,
135 ],
136 [
137 'productQuantity' => 0,
138 'weight' => 2.2,
139 ],
140 ],
141 ],
142 'negative weight item' => [
143 'isDefaultWeightEnabled' => true,
144 'defaultWeight' => 10.0,
145 'packagingWeight' => 20.0,
146 'expectedWeight' => 20.0,
147 'orderItems' => [
148 [
149 'productQuantity' => 2,
150 'weight' => -2.2,
151 ],
152 ],
153 ],
154 'multiple items, high packaging weight' => [
155 'isDefaultWeightEnabled' => true,
156 'defaultWeight' => 10.0,
157 'packagingWeight' => 20.0,
158 'expectedWeight' => 28.8,
159 'orderItems' => [
160 [
161 'productQuantity' => 2,
162 'weight' => 2.2,
163 ],
164 [
165 'productQuantity' => 2,
166 'weight' => 2.2,
167 ],
168 ],
169 ],
170 ];
171 }
172
173 /**
174 * @dataProvider getOrderWeightCalculationProvider
175 *
176 * @param bool $isDefaultWeightEnabled
177 * @param float $defaultWeight
178 * @param float $packagingWeight
179 * @param float $expectedWeight
180 * @param array[] $orderItems
181 *
182 * @throws Exception
183 */
184 public function testOrderWeightCalculation(
185 bool $isDefaultWeightEnabled,
186 float $defaultWeight,
187 float $packagingWeight,
188 float $expectedWeight,
189 array $orderItems
190 ): void {
191 $optionsProvider = $this->createMock( OptionsProvider::class );
192 $optionsProvider
193 ->method( 'isDefaultWeightEnabled' )
194 ->willReturn( $isDefaultWeightEnabled );
195 $optionsProvider
196 ->method( 'getDefaultWeight' )
197 ->willReturn( $defaultWeight );
198 $optionsProvider
199 ->method( 'getPackagingWeight' )
200 ->willReturn( $packagingWeight );
201
202 $wcAdapter = $this->createMock( WcAdapter::class );
203 $wcAdapter
204 ->method( 'getWeight' )
205 ->willReturnCallback(
206 static function ( $weight ): float {
207 if ( $weight < 0 ) {
208 return 0.0;
209 }
210
211 return (float) $weight;
212 }
213 );
214
215 $weightCalculator = new WeightCalculator(
216 $optionsProvider,
217 $wcAdapter
218 );
219
220 $items = [];
221 foreach ( $orderItems as $item ) {
222 $items[] = $this->createOrderItemMock( ...$item );
223 }
224
225 $order = $this->createMock( WC_Order::class );
226 $order
227 ->method( 'get_items' )
228 ->willReturn( $items );
229
230 $weight = $weightCalculator->calculateOrderWeight( $order );
231
232 $this->assertSame( $expectedWeight, $weight );
233 }
234 }
235