I have a problem with a test that fails me, but nevertheless in production or local, using postman the validation works perfectly.
ElkWidgetRequest
<?php
namespace App\Http\Requests;
use App\Rules\EmptyValue;
use Bouncer;
use DateTime;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
class ElkWidgetsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Auth::user()->isSuperAdmin() || Bouncer::is(Auth::user())->a('administrator');
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'command_center_id' => 'sometimes|filled|array|min:1',
'command_center_id.*' => 'required|integer|exists:command_centers,id',
'consumption' => ['sometimes','nullable',new EmptyValue()],
'power' => ['sometimes','nullable',new EmptyValue()],
'perPage' => 'prohibited',
'page' => 'prohibited',
];
}
}
ElkWidgetsRequestTest
<?php
namespace Tests\Feature\Http\Requests;
use App\Http\Requests\ElkWidgetsRequest;
use Illuminate\Support\Facades\Validator;
use App\Models\CommandCenter;
use App\Models\User;
use Tests\TestCase;
class ElkWidgetsRequestTest extends TestCase
{
// Others test for validate other rules
/** @test */
function it_fails_with_empty_modem_id_elk_widgets()
{
$request = new ElkWidgetsRequest();
$request->merge(['command_center_id']);
$validator = Validator::make($request->all(), $request->rules());
$this->assertFalse($validator->passes());
$this->assertEquals('The command center id field must have a value.', $validator->errors()->first('command_center_id'));
}
}
In test pass empty value for command_center and test fail becasue validation is correct.
FAILED Tests\Feature\Http\Requests\ElkWidgetsRequestTest > it fails with empty modem id elk widgets
Failed asserting that true is false.
But in postman work fine
{
"error": {
"command_center_id": [
"The command center id field must have a value."
]
}
}
Call in postman
curl --location 'http://localhost/api/v2/elk-widget' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer 15|12n36VVVVVVVVVVVVVUUEFj7W' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'command_center_id='