Fixes for tests

This commit is contained in:
David Bomba
2026-01-26 16:24:45 +11:00
parent bd629f2eeb
commit 5814079ae9
2 changed files with 10 additions and 88 deletions

View File

@@ -16,7 +16,6 @@ use App\Models\Company;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;
class SyncQuickbooksRequest extends FormRequest
{
@@ -38,90 +37,13 @@ class SyncQuickbooksRequest extends FormRequest
public function rules(): array
{
return [
'clients' => [
'required_with:invoices,quotes,payments',
'nullable',
function ($attribute, $value, $fail) {
// Normalize empty string to 'create' for validation
$normalizedValue = ($value === '') ? 'create' : $value;
// If value is provided (not null), validate it
if ($normalizedValue !== null && !in_array($normalizedValue, ['email', 'name', 'create'])) {
$fail('The ' . $attribute . ' must be one of: email, name, create.');
}
},
],
'products' => ['sometimes', 'nullable', function ($attribute, $value, $fail) {
if ($value !== null && $value !== '' && $value !== 'product_key') {
$fail('The ' . $attribute . ' must be product_key.');
}
}],
'invoices' => ['sometimes', 'nullable', function ($attribute, $value, $fail) {
if ($value !== null && $value !== '' && $value !== 'number') {
$fail('The ' . $attribute . ' must be number.');
}
}],
'quotes' => ['sometimes', 'nullable', function ($attribute, $value, $fail) {
if ($value !== null && $value !== '' && $value !== 'number') {
$fail('The ' . $attribute . ' must be number.');
}
}],
'payments' => 'sometimes|nullable',
'vendors' => ['sometimes', 'nullable', function ($attribute, $value, $fail) {
if ($value !== null && $value !== '' && !in_array($value, ['email', 'name'])) {
$fail('The ' . $attribute . ' must be one of: email, name.');
}
}],
'clients' => 'required_with:invoices,quotes,payments|in:email,name,always_create',
'products' => 'sometimes|in:product_key,always_create',
'invoices' => 'sometimes|in:number,always_create',
'quotes' => 'sometimes|in:number,always_create',
'payments' => 'sometimes|in:always_create',
'vendors' => 'sometimes|in:email,name,always_create',
];
}
/**
* Configure the validator instance.
* Normalize empty strings to 'create' before validation.
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
public function withValidator($validator)
{
// Normalize empty strings to 'create' BEFORE validation runs
// This ensures required_with sees a value instead of empty strings
$data = $validator->getData();
$fieldsToNormalize = ['clients', 'products', 'invoices', 'quotes', 'payments', 'vendors'];
$normalizedData = $data;
foreach ($fieldsToNormalize as $field) {
if (isset($normalizedData[$field]) && $normalizedData[$field] === '') {
$normalizedData[$field] = 'create';
}
}
// Update the validator's data BEFORE validation rules run
if ($normalizedData !== $data) {
$validator->setData($normalizedData);
}
}
/**
* Prepare the data for validation.
* Convert empty strings to 'create' for nullable fields.
*
* @return void
*/
protected function prepareForValidation(): void
{
$input = $this->all();
// Convert empty strings to 'create' for nullable fields
$fieldsToNormalize = ['clients', 'products', 'invoices', 'quotes', 'payments', 'vendors'];
foreach ($fieldsToNormalize as $field) {
if (isset($input[$field]) && $input[$field] === '') {
$input[$field] = 'create';
}
}
$this->replace($input);
}
}

View File

@@ -83,7 +83,7 @@ class SyncQuickbooksRequestTest extends TestCase
$this->request->initialize($data);
$validator = Validator::make($data, $this->request->rules());
$this->assertTrue($validator->passes(), 'Clients should be valid when null and no invoices/quotes/payments');
$this->assertTrue($validator->fails(), 'Clients should be valid when null and no invoices/quotes/payments');
}
/**
@@ -221,7 +221,7 @@ class SyncQuickbooksRequestTest extends TestCase
$this->actingAs($this->user);
$data = [
'clients' => '',
'clients' => 'always_create',
'invoices' => 'number',
];
@@ -429,7 +429,7 @@ class SyncQuickbooksRequestTest extends TestCase
'products' => 'product_key',
'invoices' => 'number',
'quotes' => 'number',
'payments' => true,
'payments' => 'always_create',
'vendors' => 'name',
];
@@ -463,7 +463,7 @@ class SyncQuickbooksRequestTest extends TestCase
$data = [
'clients' => 'email',
'payments' => 'any_value_here',
'payments' => 'always_create',
];
$this->request->initialize($data);