Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abkrim's avatar
Level 13

Problem with atest. Fails tes but not fail test in postman

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='
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue seems to be with the test case. The test case is not passing the correct value for the command_center_id field. Instead of passing an empty value, it is passing the field name only.

To fix the issue, the test case should pass an empty array for the command_center_id field.

Here's the updated test case code:

/** @test */
function it_fails_with_empty_modem_id_elk_widgets()
{
    $request = new ElkWidgetsRequest();

    $request->merge(['command_center_id' => []]); // pass an empty array

    $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'));
}
1 like

Please or to participate in this conversation.