Skip to content

Commit 0a224e7

Browse files
committed
♻️ Apply new assertions to other validation tests
1 parent d516811 commit 0a224e7

2 files changed

Lines changed: 11 additions & 34 deletions

File tree

tests/PHPUnit/StoreSync/CartValidation/CouponValidatorTest.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponContextBuilder;
1212
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\DiscountCalculator;
1313
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponResolutionBuilder;
14-
use WooCommerce\PayPalCommerce\TestCase;
1514

1615
/**
1716
* @covers \WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponValidator
1817
*/
19-
class CouponValidatorTest extends TestCase
18+
class CouponValidatorTest extends ValidationTest
2019
{
2120

2221
private CouponValidator $validator;
@@ -85,14 +84,8 @@ public function test_validate_returns_error_when_coupons_disabled(): void
8584
$this->assertIsArray($result);
8685
$this->assertCount(1, $result);
8786

88-
$issue = $result[0];
89-
$data = $issue->to_array();
90-
91-
$this->assertSame('PRICING_ERROR', $data['code']);
92-
$this->assertSame('BUSINESS_RULE', $data['type']);
93-
$this->assertSame('Coupons are not enabled', $data['message']);
94-
$this->assertStringContainsString('does not accept coupon codes', $data['user_message']);
95-
$this->assertSame('coupons', $data['field']);
87+
$data = $result[0]->to_array();
88+
$this->assertValidationIssue($data, 'PRICING_ERROR', 'BUSINESS_RULE', 'coupons', 'Coupons are not enabled');
9689
}
9790

9891
public function test_validate_filters_only_apply_actions(): void
@@ -117,9 +110,7 @@ public function test_coupon_invalid_issue_has_correct_error_code(): void
117110
->for_field( 'coupons[0]' );
118111

119112
$data = $issue->to_array();
120-
121-
$this->assertSame( 'PRICING_ERROR', $data['code'] );
122-
$this->assertSame( 'BUSINESS_RULE', $data['type'] );
113+
$this->assertValidationIssue( $data, 'PRICING_ERROR', 'BUSINESS_RULE' );
123114
}
124115

125116
public function test_coupon_invalid_truncates_long_messages(): void
@@ -131,6 +122,7 @@ public function test_coupon_invalid_truncates_long_messages(): void
131122
->user_message( $long_user_message );
132123

133124
$data = $issue->to_array();
125+
$this->assertValidationIssue( $data, 'PRICING_ERROR', 'BUSINESS_RULE' );
134126

135127
$this->assertSame( 255, strlen( $data['message'] ) );
136128
$this->assertSame( 500, strlen( $data['user_message'] ) );

tests/PHPUnit/StoreSync/CartValidation/CurrencyValidatorTest.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
namespace WooCommerce\PayPalCommerce\StoreSync\CartValidation;
55

66
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
7-
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
8-
use WooCommerce\PayPalCommerce\TestCase;
9-
107
use function Brain\Monkey\Functions\when;
118

129
/**
1310
* @covers \WooCommerce\PayPalCommerce\StoreSync\CartValidation\CurrencyValidator
1411
*/
15-
class CurrencyValidatorTest extends TestCase {
12+
class CurrencyValidatorTest extends ValidationTest {
1613

1714
private CurrencyValidator $validator;
1815

@@ -51,11 +48,9 @@ public function test_validate_detects_mixed_currencies(): void {
5148

5249
$this->assertIsArray( $result );
5350
$this->assertCount( 1, $result );
54-
$this->assertInstanceOf( ValidationIssue::class, $result[0] );
5551

5652
$issue_data = $result[0]->to_array();
57-
$this->assertStringContainsString( 'Mixed currencies detected', $issue_data['message'] );
58-
$this->assertSame( 'items[1].price.currency_code', $issue_data['field'] );
53+
$this->assertValidationIssue( $issue_data, 'PRICING_ERROR', 'BUSINESS_RULE', 'items[1].price.currency_code', 'Mixed currencies detected' );
5954
}
6055

6156
public function test_validate_detects_store_currency_mismatch(): void {
@@ -72,12 +67,9 @@ public function test_validate_detects_store_currency_mismatch(): void {
7267

7368
$this->assertIsArray( $result );
7469
$this->assertCount( 1, $result );
75-
$this->assertInstanceOf( ValidationIssue::class, $result[0] );
7670

7771
$issue_data = $result[0]->to_array();
78-
$this->assertStringContainsString( 'Cart currency EUR does not match store currency USD', $issue_data['message'] );
79-
$this->assertStringContainsString( 'This store only accepts payments in USD', $issue_data['user_message'] );
80-
$this->assertSame( 'items[0].price.currency_code', $issue_data['field'] );
72+
$this->assertValidationIssue( $issue_data, 'PRICING_ERROR', 'BUSINESS_RULE', 'items[0].price.currency_code', 'Cart currency EUR does not match store currency USD' );
8173
}
8274

8375
public function test_validate_returns_null_for_empty_cart(): void {
@@ -165,13 +157,9 @@ public function test_detects_mismatch_skipping_empty_items(): void {
165157

166158
$this->assertIsArray( $result );
167159
$this->assertCount( 1, $result );
168-
$this->assertInstanceOf( ValidationIssue::class, $result[0] );
169160

170161
$issue_data = $result[0]->to_array();
171-
$this->assertStringContainsString( 'Mixed currencies detected', $issue_data['message'] );
172-
$this->assertStringContainsString( 'EUR', $issue_data['message'] );
173-
$this->assertStringContainsString( 'USD', $issue_data['message'] );
174-
$this->assertSame( 'items[2].price.currency_code', $issue_data['field'] );
162+
$this->assertValidationIssue( $issue_data, 'PRICING_ERROR', 'BUSINESS_RULE', 'items[2].price.currency_code', 'Mixed currencies detected' );
175163
}
176164

177165
public function test_store_mismatch_points_to_correct_index(): void {
@@ -203,11 +191,9 @@ public function test_store_mismatch_points_to_correct_index(): void {
203191

204192
$this->assertIsArray( $result );
205193
$this->assertCount( 1, $result );
206-
$this->assertInstanceOf( ValidationIssue::class, $result[0] );
207194

208195
$issue_data = $result[0]->to_array();
209-
$this->assertStringContainsString( 'Cart currency EUR does not match store currency USD', $issue_data['message'] );
210-
$this->assertSame( 'items[1].price.currency_code', $issue_data['field'] );
196+
$this->assertValidationIssue( $issue_data, 'PRICING_ERROR', 'BUSINESS_RULE', 'items[1].price.currency_code', 'Cart currency EUR does not match store currency USD' );
211197
}
212198

213199
public function test_mixed_currency_prevents_store_check(): void {
@@ -224,10 +210,9 @@ public function test_mixed_currency_prevents_store_check(): void {
224210

225211
$this->assertIsArray( $result );
226212
$this->assertCount( 1, $result );
227-
$this->assertInstanceOf( ValidationIssue::class, $result[0] );
228213

229214
$issue = $result[0]->to_array();
230-
$this->assertStringContainsString( 'Mixed currencies detected', $issue['message'] );
215+
$this->assertValidationIssue( $issue, 'PRICING_ERROR', 'BUSINESS_RULE', null, 'Mixed currencies detected' );
231216
}
232217

233218
/**

0 commit comments

Comments
 (0)