PluginProbe
Packeta / 2.1.2
Packeta v2.1.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 1.7.0 All 55 releases
packeta / tests / Module / Checkout / CheckoutValidatorTest.php

CheckoutValidatorTest.php in Packeta 2.1.2, at tests/Module/Checkout/CheckoutValidatorTest.php

295 lines 12.0 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\Checkout;
6
7 use Packetery\Module\Carrier\CarrierOptionsFactory;
8 use Packetery\Module\Carrier\EntityRepository;
9 use Packetery\Module\Carrier\Options as CarrierOptions;
10 use Packetery\Module\Checkout\CartService;
11 use Packetery\Module\Checkout\CheckoutService;
12 use Packetery\Module\Checkout\CheckoutStorage;
13 use Packetery\Module\Checkout\CheckoutValidator;
14 use Packetery\Module\Checkout\SessionService;
15 use Packetery\Module\Framework\WcAdapter;
16 use Packetery\Module\Framework\WpAdapter;
17 use PHPUnit\Framework\MockObject\MockObject;
18 use PHPUnit\Framework\TestCase;
19 use WC_REST_Exception;
20
21 class CheckoutValidatorTest extends TestCase {
22
23 private WpAdapter&MockObject $wpAdapter;
24 private WcAdapter&MockObject $wcAdapter;
25 private CheckoutService&MockObject $checkoutService;
26 private CartService&MockObject $cartService;
27 private SessionService&MockObject $sessionService;
28 private CheckoutStorage&MockObject $checkoutStorage;
29 private CarrierOptionsFactory&MockObject $carrierOptionsFactory;
30 private EntityRepository&MockObject $carrierEntityRepository;
31
32 private function createCheckoutValidator(): CheckoutValidator {
33 $this->wpAdapter = $this->createMock( WpAdapter::class );
34 $this->wcAdapter = $this->createMock( WcAdapter::class );
35 $this->checkoutService = $this->createMock( CheckoutService::class );
36 $this->cartService = $this->createMock( CartService::class );
37 $this->sessionService = $this->createMock( SessionService::class );
38 $this->checkoutStorage = $this->createMock( CheckoutStorage::class );
39 $this->carrierOptionsFactory = $this->createMock( CarrierOptionsFactory::class );
40 $this->carrierEntityRepository = $this->createMock( EntityRepository::class );
41
42 return new CheckoutValidator(
43 $this->wpAdapter,
44 $this->wcAdapter,
45 $this->checkoutService,
46 $this->cartService,
47 $this->sessionService,
48 $this->checkoutStorage,
49 $this->carrierOptionsFactory,
50 $this->carrierEntityRepository
51 );
52 }
53
54 /**
55 * @return array<string, array{callable, bool, string|null}>
56 */
57 public static function actionValidateBlockCheckoutDataProvider(): array {
58 return [
59 'not packetery shipping method' => [
60 'scenarioParameters' => [
61 'resolveChosenMethod' => 'third_party_method',
62 'isPacketeryShippingMethod' => false,
63 ],
64 'shouldThrow' => false,
65 ],
66 'category restriction' => [
67 'scenarioParameters' => [
68 'resolveChosenMethod' => 'packetery_method',
69 'isPacketeryShippingMethod' => true,
70 'getPostDataIncludingStoredData' => [],
71 'cartGetCartContents' => [],
72 'isShippingRateRestrictedByProductsCategory' => true,
73 ],
74 'shouldThrow' => true,
75 ],
76 'payment method disallowed' => [
77 'scenarioParameters' => [
78 'resolveChosenMethod' => 'packetery_method',
79 'isPacketeryShippingMethod' => true,
80 'getPostDataIncludingStoredData' => [],
81 'cartGetCartContents' => [],
82 'isShippingRateRestrictedByProductsCategory' => false,
83 'getCarrierIdFromPacketeryShippingMethod' => '789',
84 'createByCarrierId' => new CarrierOptions( 'dummyId', [ 'disallowed_checkout_payment_methods' => [ 'cod' ] ] ),
85 'getChosenPaymentMethod' => 'cod',
86 ],
87 'shouldThrow' => true,
88 ],
89 'car delivery missing validation' => [
90 'scenarioParameters' => [
91 'resolveChosenMethod' => 'packetery_method',
92 'isPacketeryShippingMethod' => true,
93 'getPostDataIncludingStoredData' => [],
94 'cartGetCartContents' => [],
95 'isShippingRateRestrictedByProductsCategory' => false,
96 'getCarrierIdFromPacketeryShippingMethod' => '456',
97 'getChosenPaymentMethod' => null,
98 'isPickupPointOrder' => false,
99 'isHomeDeliveryOrder' => false,
100 'isCarDeliveryOrder' => true,
101 ],
102 'shouldThrow' => true,
103 ],
104 'pickup point ok' => [
105 'scenarioParameters' => [
106 'resolveChosenMethod' => 'packetery_method',
107 'isPacketeryShippingMethod' => true,
108 'getPostDataIncludingStoredData' => [ 'packetery_point_id' => '123' ],
109 'cartGetCartContents' => [],
110 'isShippingRateRestrictedByProductsCategory' => false,
111 'getCarrierIdFromPacketeryShippingMethod' => 'dummyPpId',
112 'getChosenPaymentMethod' => null,
113 'isPickupPointOrder' => true,
114 'getCustomerCountry' => 'CZ',
115 'isValidForCountry' => true,
116 ],
117 'shouldThrow' => false,
118 ],
119 'pickup point missing' => [
120 'scenarioParameters' => [
121 'resolveChosenMethod' => 'packetery_method',
122 'isPacketeryShippingMethod' => true,
123 'getPostDataIncludingStoredData' => [],
124 'cartGetCartContents' => [],
125 'isShippingRateRestrictedByProductsCategory' => false,
126 'getCarrierIdFromPacketeryShippingMethod' => 'dummyPpId',
127 'getChosenPaymentMethod' => null,
128 'isPickupPointOrder' => true,
129 'getCustomerCountry' => 'CZ',
130 'isValidForCountry' => true,
131 ],
132 'shouldThrow' => true,
133 ],
134 'pickup point no country' => [
135 'scenarioParameters' => [
136 'resolveChosenMethod' => 'packetery_method',
137 'isPacketeryShippingMethod' => true,
138 'getPostDataIncludingStoredData' => [ 'packetery_point_id' => '123' ],
139 'cartGetCartContents' => [],
140 'isShippingRateRestrictedByProductsCategory' => false,
141 'getCarrierIdFromPacketeryShippingMethod' => 'dummyPpId',
142 'getChosenPaymentMethod' => null,
143 'isPickupPointOrder' => true,
144 'getCustomerCountry' => null,
145 ],
146 'shouldThrow' => true,
147 ],
148 'pickup point carrier not valid' => [
149 'scenarioParameters' => [
150 'resolveChosenMethod' => 'packetery_method',
151 'isPacketeryShippingMethod' => true,
152 'getPostDataIncludingStoredData' => [ 'packetery_point_id' => '123' ],
153 'cartGetCartContents' => [],
154 'isShippingRateRestrictedByProductsCategory' => false,
155 'getCarrierIdFromPacketeryShippingMethod' => 'dummyPpId',
156 'getChosenPaymentMethod' => null,
157 'isPickupPointOrder' => true,
158 'getCustomerCountry' => 'CZ',
159 'isValidForCountry' => false,
160 ],
161 'shouldThrow' => true,
162 ],
163 'home delivery required not validated' => [
164 'scenarioParameters' => [
165 'resolveChosenMethod' => 'packetery_method',
166 'isPacketeryShippingMethod' => true,
167 'getPostDataIncludingStoredData' => [],
168 'cartGetCartContents' => [],
169 'isShippingRateRestrictedByProductsCategory' => false,
170 'getCarrierIdFromPacketeryShippingMethod' => 'dummyHdId',
171 'getChosenPaymentMethod' => null,
172 'isPickupPointOrder' => false,
173 'isHomeDeliveryOrder' => true,
174 'getOption' => [ 'address_validation' => 'required' ],
175 ],
176 'shouldThrow' => true,
177 ],
178 'home delivery validated' => [
179 'scenarioParameters' => [
180 'resolveChosenMethod' => 'packetery_method',
181 'isPacketeryShippingMethod' => true,
182 'getPostDataIncludingStoredData' => [ 'packetery_address_isValidated' => '1' ],
183 'cartGetCartContents' => [],
184 'isShippingRateRestrictedByProductsCategory' => false,
185 'getCarrierIdFromPacketeryShippingMethod' => 'dummyHdId',
186 'getChosenPaymentMethod' => null,
187 'isPickupPointOrder' => false,
188 'isHomeDeliveryOrder' => true,
189 'getOption' => [ 'address_validation' => 'required' ],
190 ],
191 'shouldThrow' => false,
192 ],
193 'home delivery no options' => [
194 'scenarioParameters' => [
195 'resolveChosenMethod' => 'packetery_method',
196 'isPacketeryShippingMethod' => true,
197 'getPostDataIncludingStoredData' => [],
198 'cartGetCartContents' => [],
199 'isShippingRateRestrictedByProductsCategory' => false,
200 'getCarrierIdFromPacketeryShippingMethod' => 'dummyHdId',
201 'getChosenPaymentMethod' => null,
202 'isPickupPointOrder' => false,
203 'isHomeDeliveryOrder' => true,
204 'getOption' => false,
205 ],
206 'shouldThrow' => false,
207 ],
208 'mysterious packetery method' => [
209 'scenarioParameters' => [
210 'resolveChosenMethod' => 'packetery_method',
211 'isPacketeryShippingMethod' => true,
212 'isPickupPointOrder' => false,
213 'isHomeDeliveryOrder' => false,
214 'getOption' => false,
215 ],
216 'shouldThrow' => false,
217 ],
218 ];
219 }
220
221 /**
222 * @dataProvider actionValidateBlockCheckoutDataProvider
223 */
224 public function testActionValidateBlockCheckoutData( array $scenarioParameters, bool $shouldThrow ): void {
225 $checkoutValidator = $this->createCheckoutValidator();
226
227 $this->checkoutService->method( 'resolveChosenMethod' )
228 ->willReturn( $scenarioParameters['resolveChosenMethod'] );
229 $this->checkoutService->method( 'isPacketeryShippingMethod' )
230 ->willReturn( $scenarioParameters['isPacketeryShippingMethod'] );
231 $this->wpAdapter->method( '__' )
232 ->willReturnCallback(
233 static fn( string $text ): string => $text
234 );
235
236 if ( isset( $scenarioParameters['getPostDataIncludingStoredData'] ) ) {
237 $this->checkoutStorage->method( 'getPostDataIncludingStoredData' )
238 ->willReturn( $scenarioParameters['getPostDataIncludingStoredData'] );
239 }
240 if ( isset( $scenarioParameters['cartGetCartContents'] ) ) {
241 $this->wcAdapter->method( 'cartGetCartContents' )
242 ->willReturn( $scenarioParameters['cartGetCartContents'] );
243 }
244 if ( isset( $scenarioParameters['isShippingRateRestrictedByProductsCategory'] ) ) {
245 $this->cartService->method( 'isShippingRateRestrictedByProductsCategory' )
246 ->willReturn( $scenarioParameters['isShippingRateRestrictedByProductsCategory'] );
247 }
248 if ( isset( $scenarioParameters['getCarrierIdFromPacketeryShippingMethod'] ) ) {
249 $this->checkoutService->method( 'getCarrierIdFromPacketeryShippingMethod' )
250 ->willReturn( $scenarioParameters['getCarrierIdFromPacketeryShippingMethod'] );
251 }
252 if ( isset( $scenarioParameters['getChosenPaymentMethod'] ) ) {
253 $this->sessionService->method( 'getChosenPaymentMethod' )
254 ->willReturn( $scenarioParameters['getChosenPaymentMethod'] );
255 }
256 if ( isset( $scenarioParameters['isPickupPointOrder'] ) ) {
257 $this->checkoutService->method( 'isPickupPointOrder' )
258 ->willReturn( $scenarioParameters['isPickupPointOrder'] );
259 }
260 if ( isset( $scenarioParameters['isHomeDeliveryOrder'] ) ) {
261 $this->checkoutService->method( 'isHomeDeliveryOrder' )
262 ->willReturn( $scenarioParameters['isHomeDeliveryOrder'] );
263 }
264 if ( isset( $scenarioParameters['isCarDeliveryOrder'] ) ) {
265 $this->checkoutService->method( 'isCarDeliveryOrder' )
266 ->willReturn( $scenarioParameters['isCarDeliveryOrder'] );
267 }
268 if ( isset( $scenarioParameters['getCustomerCountry'] ) ) {
269 $this->checkoutService->method( 'getCustomerCountry' )
270 ->willReturn( $scenarioParameters['getCustomerCountry'] );
271 }
272 if ( isset( $scenarioParameters['isValidForCountry'] ) ) {
273 $this->carrierEntityRepository->method( 'isValidForCountry' )
274 ->willReturn( $scenarioParameters['isValidForCountry'] );
275 }
276 if ( isset( $scenarioParameters['getOption'] ) ) {
277 $this->wpAdapter->method( 'getOption' )
278 ->willReturn( $scenarioParameters['getOption'] );
279 }
280 if ( isset( $scenarioParameters['createByCarrierId'] ) ) {
281 $this->carrierOptionsFactory->method( 'createByCarrierId' )
282 ->willReturn( $scenarioParameters['createByCarrierId'] );
283 }
284
285 if ( $shouldThrow ) {
286 // Throws fake exception from woocommerce-stubs without details.
287 $this->expectException( WC_REST_Exception::class );
288 } else {
289 $this->expectNotToPerformAssertions();
290 }
291
292 $checkoutValidator->actionValidateBlockCheckoutData();
293 }
294 }
295