11<?php
2- declare (strict_types= 1 );
2+ declare ( strict_types = 1 );
33
44namespace WooCommerce \PayPalCommerce \StoreSync \CartValidation ;
55
66use Mockery ;
77use WooCommerce \PayPalCommerce \StoreSync \Helper \ProductManager ;
8- use WooCommerce \PayPalCommerce \StoreSync \Schema \PayPalCart ;
98use WooCommerce \PayPalCommerce \StoreSync \Validation \ValidationIssue ;
109use WooCommerce \PayPalCommerce \StoreSync \CartValidation \CouponValidator \CouponValidator ;
1110use WooCommerce \PayPalCommerce \StoreSync \CartValidation \CouponValidator \CouponContextBuilder ;
1514/**
1615 * @covers \WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponValidator
1716 */
18- class CouponValidatorTest extends ValidationTest
19- {
17+ class CouponValidatorTest extends ValidationTest {
2018
2119 private CouponValidator $ validator ;
2220 private $ product_manager ;
2321 private $ context_builder ;
2422 private $ discount_calculator ;
2523 private $ resolution_builder ;
2624
27- public function setUp (): void
28- {
25+ public function setUp (): void {
2926 parent ::setUp ();
30- \Brain \Monkey \Functions \when ('get_woocommerce_currency ' )->justReturn ('USD ' );
27+ \Brain \Monkey \Functions \when ( 'get_woocommerce_currency ' )->justReturn ( 'USD ' );
3128 // Note: wc_coupons_enabled() is stubbed per-test as needed
32- $ this ->product_manager = Mockery::mock (ProductManager::class);
33- $ this ->discount_calculator = new DiscountCalculator ($ this ->product_manager );
34- $ this ->context_builder = new CouponContextBuilder ($ this ->product_manager , $ this ->discount_calculator );
35- $ this ->resolution_builder = new CouponResolutionBuilder ();
36- $ this ->validator = new CouponValidator (
29+ $ this ->product_manager = Mockery::mock ( ProductManager::class );
30+ $ this ->discount_calculator = new DiscountCalculator ( $ this ->product_manager );
31+ $ this ->context_builder =
32+ new CouponContextBuilder ( $ this ->product_manager , $ this ->discount_calculator );
33+ $ this ->resolution_builder = new CouponResolutionBuilder ();
34+ $ this ->validator = new CouponValidator (
3735 $ this ->context_builder ,
3836 $ this ->discount_calculator ,
3937 $ this ->resolution_builder
4038 );
4139 }
4240
43- public function test_validate_returns_null_for_cart_without_coupons (): void
44- {
45- $ cart = $ this ->create_cart_with_coupons (array ());
41+ public function test_validate_returns_null_for_cart_without_coupons (): void {
42+ $ cart = $ this ->create_cart_with_coupons ( array () );
4643
47- $ result = $ this ->validator ->validate ($ cart );
44+ $ result = $ this ->validator ->validate ( $ cart );
4845
49- $ this ->assertNull ($ result );
46+ $ this ->assertNull ( $ result );
5047 }
5148
52- public function test_validate_skips_remove_action_coupons (): void
53- {
49+ public function test_validate_skips_remove_action_coupons (): void {
5450 $ cart = $ this ->create_cart_with_coupons (
5551 array (
56- array ('code ' => 'REMOVE_ME ' , 'action ' => 'REMOVE ' ),
52+ array ( 'code ' => 'REMOVE_ME ' , 'action ' => 'REMOVE ' ),
5753 )
5854 );
5955
60- $ result = $ this ->validator ->validate ($ cart );
56+ $ result = $ this ->validator ->validate ( $ cart );
6157
6258 // REMOVE actions are skipped entirely - no WC_Coupon instantiation needed.
63- $ this ->assertNull ($ result );
59+ $ this ->assertNull ( $ result );
6460 }
6561
66- public function test_validate_returns_error_when_coupons_disabled (): void
67- {
62+ public function test_validate_returns_error_when_coupons_disabled (): void {
6863 // This test requires actual WooCommerce classes, so skip in unit test environment.
6964 // TODO: This test does not run in a unit-test environment. How is it executed?
7065 if ( ! class_exists ( 'WC_Coupon ' ) || ! class_exists ( 'WC_Discounts ' ) ) {
7166 $ this ->markTestSkipped ( 'WooCommerce classes not available in unit test environment ' );
7267 }
7368
7469 // Stub wc_coupons_enabled to return false for this test.
75- \Brain \Monkey \Functions \when ('wc_coupons_enabled ' )->justReturn (false );
70+ \Brain \Monkey \Functions \when ( 'wc_coupons_enabled ' )->justReturn ( false );
7671
7772 $ cart = $ this ->create_cart_with_coupons (
7873 array (
79- array ('code ' => 'TESTCODE ' , 'action ' => 'APPLY ' ),
74+ array ( 'code ' => 'TESTCODE ' , 'action ' => 'APPLY ' ),
8075 )
8176 );
8277
83- $ result = $ this ->validator ->validate ($ cart );
78+ $ result = $ this ->validator ->validate ( $ cart );
8479
85- $ this ->assertIsArray ($ result );
86- $ this ->assertCount (1 , $ result );
80+ $ this ->assertIsArray ( $ result );
81+ $ this ->assertCount ( 1 , $ result );
8782
8883 $ data = $ result [0 ]->to_array ();
89- $ this ->assertValidationIssue ($ data , 'PRICING_ERROR ' , 'BUSINESS_RULE ' , 'coupons ' , 'Coupons are not enabled ' );
84+ $ this ->assertValidationIssue ( $ data , 'PRICING_ERROR ' , 'BUSINESS_RULE ' , 'coupons ' , 'Coupons are not enabled ' );
9085 }
9186
92- public function test_validate_filters_only_apply_actions (): void
93- {
87+ public function test_validate_filters_only_apply_actions (): void {
9488 $ cart = $ this ->create_cart_with_coupons (
9589 array (
96- array ('code ' => 'KEEP_THIS ' , 'action ' => 'REMOVE ' ),
97- array ('code ' => 'ALSO_REMOVE ' , 'action ' => 'REMOVE ' ),
90+ array ( 'code ' => 'KEEP_THIS ' , 'action ' => 'REMOVE ' ),
91+ array ( 'code ' => 'ALSO_REMOVE ' , 'action ' => 'REMOVE ' ),
9892 )
9993 );
10094
101- $ result = $ this ->validator ->validate ($ cart );
95+ $ result = $ this ->validator ->validate ( $ cart );
10296
10397 // All coupons are REMOVE actions, so nothing to validate.
104- $ this ->assertNull ($ result );
98+ $ this ->assertNull ( $ result );
10599 }
106100
107- public function test_coupon_invalid_issue_has_correct_error_code (): void
108- {
101+ public function test_coupon_invalid_issue_has_correct_error_code (): void {
109102 $ issue = ValidationIssue::create_coupon_invalid ( 'Test message ' )
110103 ->user_message ( 'Test user message ' )
111104 ->for_field ( 'coupons[0] ' );
@@ -114,8 +107,7 @@ public function test_coupon_invalid_issue_has_correct_error_code(): void
114107 $ this ->assertValidationIssue ( $ data , 'PRICING_ERROR ' , 'BUSINESS_RULE ' );
115108 }
116109
117- public function test_coupon_invalid_truncates_long_messages (): void
118- {
110+ public function test_coupon_invalid_truncates_long_messages (): void {
119111 $ long_message = str_repeat ( 'a ' , 300 );
120112 $ long_user_message = str_repeat ( 'b ' , 600 );
121113
@@ -129,38 +121,4 @@ public function test_coupon_invalid_truncates_long_messages(): void
129121 $ this ->assertSame ( 500 , strlen ( $ data ['user_message ' ] ) );
130122 }
131123
132- /**
133- * Helper to create a cart with coupons.
134- */
135- private function create_cart_with_coupons (array $ coupons , float $ subtotal = 50.00 , string $ customer_email = '' ): PayPalCart
136- {
137- $ cart_data = array (
138- 'items ' => array (
139- array (
140- 'item_id ' => '1 ' ,
141- 'quantity ' => 1 ,
142- 'name ' => 'Test Product ' ,
143- 'price ' => array (
144- 'currency_code ' => 'USD ' ,
145- 'value ' => $ subtotal ,
146- ),
147- ),
148- ),
149- 'payment_method ' => array (
150- 'type ' => 'paypal ' ,
151- ),
152- );
153-
154- if (!empty ($ coupons )) {
155- $ cart_data ['coupons ' ] = $ coupons ;
156- }
157-
158- if ($ customer_email ) {
159- $ cart_data ['customer ' ] = array (
160- 'email_address ' => $ customer_email ,
161- );
162- }
163-
164- return PayPalCart::from_array ($ cart_data );
165- }
166124}
0 commit comments