PluginProbe
Packeta / trunk
Packeta vtrunk
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 / Checkout / CheckoutValidatorTest.php

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

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