From de1c7fdc0e00dc0d6a07c079346aab73dba6ada6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 3 Nov 2024 19:13:58 +1100 Subject: [PATCH] Fixes for tests --- .../EInvoice/UpdateEInvoiceConfiguration.php | 88 ++++++++++--------- .../UpdateEInvoiceConfigurationTest.php | 44 +++++----- 2 files changed, 69 insertions(+), 63 deletions(-) diff --git a/app/Http/Requests/EInvoice/UpdateEInvoiceConfiguration.php b/app/Http/Requests/EInvoice/UpdateEInvoiceConfiguration.php index b99ede0daf..8f1100b41c 100644 --- a/app/Http/Requests/EInvoice/UpdateEInvoiceConfiguration.php +++ b/app/Http/Requests/EInvoice/UpdateEInvoiceConfiguration.php @@ -38,47 +38,53 @@ class UpdateEInvoiceConfiguration extends Request public function rules() { - return [ - 'entity' => 'required|bail|in:invoice,client,company', - 'payment_means' => 'sometimes|bail|array', - 'payment_means.*.code' => ['required_with:payment_means', 'bail', Rule::in(PaymentMeans::getPaymentMeansCodelist())], - 'payment_means.*.bic_swift' => ['bail', - Rule::requiredIf(function () { - $code = $this->input('payment_means.code'); - $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; - return in_array('bic_swift', $requirements); - }), - ], - 'payment_means.*.iban' => ['bail', 'sometimes', 'string', 'min:15', 'max:34', - Rule::requiredIf(function () { - $code = $this->input('payment_means.code'); - $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; - return in_array('iban', $requirements); - }), - ], - 'payment_means.*.account_holder' => ['bail', 'sometimes', 'string', 'max:255', - Rule::requiredIf(function () { - $code = $this->input('payment_means.code'); - $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; - return in_array('account_holder', $requirements); - }), - ], - 'payment_means.*.information' => ['bail', 'sometimes', 'nullable', 'string'], - 'payment_means.*.card_type' => ['bail', 'sometimes', 'nullable', 'string', 'min:4', - Rule::requiredIf(function () { - $code = $this->input('payment_means.code'); - $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; - return in_array('card_type', $requirements); - }), - ], - 'payment_means.*.card_holder' => ['bail', 'sometimes', 'nullable', 'string', 'min:4', - Rule::requiredIf(function () { - $code = $this->input('payment_means.code'); - $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; - return in_array('card_holder', $requirements); - }), - ], - ]; + +return [ + 'entity' => 'required|bail|in:invoice,client,company', + 'payment_means' => 'sometimes|bail|array', + 'payment_means.*.code' => ['required_with:payment_means', 'bail', Rule::in(PaymentMeans::getPaymentMeansCodelist())], + 'payment_means.*.bic_swift' => Rule::forEach(function (string|null $value, string $attribute) { + $index = explode('.', $attribute)[1]; + $code = $this->input("payment_means.{$index}.code"); + $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; + + return ['bail', Rule::requiredIf(in_array('bic_swift', $requirements))]; + }), + 'payment_means.*.iban' => Rule::forEach(function (string|null $value, string $attribute) { + $index = explode('.', $attribute)[1]; + $code = $this->input("payment_means.{$index}.code"); + $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; + + return ['bail', 'sometimes', 'string', 'min:15', 'max:34', + Rule::requiredIf(in_array('iban', $requirements))]; + }), + 'payment_means.*.account_holder' => Rule::forEach(function (string|null $value, string $attribute) { + $index = explode('.', $attribute)[1]; + $code = $this->input("payment_means.{$index}.code"); + $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; + + return ['bail', 'sometimes', 'string', 'max:255', + Rule::requiredIf(in_array('account_holder', $requirements))]; + }), + 'payment_means.*.information' => ['bail', 'sometimes', 'nullable', 'string'], + 'payment_means.*.card_type' => Rule::forEach(function (string|null $value, string $attribute) { + $index = explode('.', $attribute)[1]; + $code = $this->input("payment_means.{$index}.code"); + $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; + + return ['bail', 'sometimes', 'nullable', 'string', 'min:4', + Rule::requiredIf(in_array('card_type', $requirements))]; + }), + 'payment_means.*.card_holder' => Rule::forEach(function (string|null $value, string $attribute) { + $index = explode('.', $attribute)[1]; + $code = $this->input("payment_means.{$index}.code"); + $requirements = PaymentMeans::$payment_means_requirements_codes[$code] ?? []; + + return ['bail', 'sometimes', 'nullable', 'string', 'min:4', + Rule::requiredIf(in_array('card_holder', $requirements))]; + }), + ]; + } diff --git a/tests/Feature/EInvoice/RequestValidation/UpdateEInvoiceConfigurationTest.php b/tests/Feature/EInvoice/RequestValidation/UpdateEInvoiceConfigurationTest.php index 6299aade4d..8ccfc37ea6 100644 --- a/tests/Feature/EInvoice/RequestValidation/UpdateEInvoiceConfigurationTest.php +++ b/tests/Feature/EInvoice/RequestValidation/UpdateEInvoiceConfigurationTest.php @@ -50,9 +50,9 @@ class UpdateEInvoiceConfigurationTest extends TestCase { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => 'invalidcodehere' - ] + ]] ]; $this->request->initialize($data); @@ -65,12 +65,12 @@ class UpdateEInvoiceConfigurationTest extends TestCase { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '30', - 'iban' => '12345678', + 'iban' => '123456789101112254', 'bic_swift' => 'DEUTDEFF', 'account_holder' => 'John Doe Company Limited' - ] + ]] ]; $this->request->initialize($data); @@ -83,29 +83,29 @@ class UpdateEInvoiceConfigurationTest extends TestCase { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '48', 'card_type' => 'VISA', 'iban' => '12345678' - ] + ]] ]; $this->request->initialize($data); $validator = Validator::make($data, $this->request->rules()); - $this->assertTrue($validator->passes()); + $this->assertFalse($validator->passes()); } public function testValidatesPaymentMeansForCreditCard() { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '54', 'card_type' => 'VISA', 'card_number' => '************1234', 'card_holder' => 'John Doe' - ] + ]] ]; $this->request->initialize($data); @@ -118,44 +118,44 @@ class UpdateEInvoiceConfigurationTest extends TestCase { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '30', - ] + ]] ]; $this->request->initialize($data); $validator = Validator::make($data, $this->request->rules()); - $this->assertFalse($validator->passes()); - $this->assertTrue($validator->errors()->has('payment_means.iban')); - $this->assertTrue($validator->errors()->has('payment_means.bic_swift')); + + $this->assertTrue($validator->errors()->has('payment_means.0.bic_swift')); + } public function testFailsValidationWithInvalidPaymentMeansCode() { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '999', - ] + ]] ]; $this->request->initialize($data); $validator = Validator::make($data, $this->request->rules()); $this->assertFalse($validator->passes()); - $this->assertTrue($validator->errors()->has('payment_means.code')); + $this->assertTrue($validator->errors()->has('payment_means.0.code')); } public function testValidatesPaymentMeansForDirectDebit() { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '49', 'payer_bank_account' => '12345678', 'bic_swift' => 'DEUTDEFF' - ] + ]] ]; $this->request->initialize($data); @@ -168,11 +168,11 @@ class UpdateEInvoiceConfigurationTest extends TestCase { $data = [ 'entity' => 'invoice', - 'payment_means' => [ + 'payment_means' => [[ 'code' => '15', 'account_holder' => 'John Doe Company Limited', 'bsb_sort' => '123456' - ] + ]] ]; $this->request->initialize($data);