Antonella's avatar

test add ip server with local method

I wanted to test this method with a feature test:

public function getTask(Request $requestSvr)
{
   
        $requestSvr->all();
        $requestSvr['id_server'] = (string) $requestSvr['id_server'];

        $task = Task::whereIn('works', $requestSvr['task_available'])->where('process_status','=','new')->first();

        if(!empty($task))
        {
            $task->process_status = 'processing';
            $task->id_server = $requestSvr['id_server'];

            if(!empty($requestSvr->server('SERVER_ADDR')))
            {
                $task->ip_server = (string) $requestSvr->server('SERVER_ADDR');
            }

            $task->save();

            return response()->json($task, 200);

        }
        else
        {
            return response()->json([], 204);
        }

}

doing the test locally I would not know how to verify that ip_server is entered

I would like to test it as follows:

//so he simulated the server call $requestSvr1 = [ "id_server" => server_1', "task_available" => ["A","B"] ];

        $response = $this->put('/api/get_task',$requestSvr1);
   	 $ja = Task::find($response['id']);
//this FAIL $response->server('SERVER_ADDR') is empty
    $this->assertSame($response->server('SERVER_ADDR'),$ja['ip_server']);

this $requestSvr->server ('SERVER_ADDR') value is null. It will only ever be null locally.

how could I test such a thing?

0 likes
4 replies
Tray2's avatar

What I usually do is dd($response); and then look for the key and value I need and when I found it I make the assertion.

Antonella's avatar

yes ok I could add an ip_server field in the request. or do something like this:

$requestSvr1 = [ "id_server" => server_1', 
				"task_available" => ["A","B"] ,
				ip_server => '111.111.111'];

and then work it taking the value as follows:

$requestSvr->all();
$requestSvr['ip_server']	

but this would result in server side changes which I cannot do. What I was saying was to derive the ip value. server from the call.

and I found that this value can be obtained as follows:

$response->server('SERVER_ADDR')

I just have no idea how to test locally

@tray2

Tray2's avatar

Like I said start with a

dd($response);

It will give you a response object. Inside of that you will find lots of information that you can use. The server address is probably in there somewhere.

Antonella's avatar

I'm doing the test locally. dd($response->server('SERVER_ADDR')) will always be null. i think i go mocked in this case @tray2

Please or to participate in this conversation.