J
J
Jaroslaw Goszowski2019-01-24 12:07:24
Laravel
Jaroslaw Goszowski, 2019-01-24 12:07:24

What's wrong with parameters in HTTP testing?

Here is my test:

public function testGetDataGoodResponse()
{
    $response = $this->call('GET', '/getdata', [
        'inn' => mt_rand(1111111111, 9999999999),
        'id' => mt_rand(11111111, 99999999) . '_0_' . mt_rand(111111111, 999999999),
    ]);

    $response->assertStatus(200);
}

Here is the controller method (request dump at the beginning)
public function getData(Request $request)
{

    dd($request->all());

    $validator = Validator::make($request->all(), [
        'inn' => 'required|numeric|digits:10',
        'id' => 'required|string',
    ]);

    if($validator->fails())
    {
        dd($validator->errors());
        throw new \Exception('error inn or id');
    }
}

But this dump outputs an empty array:
>vendor\bin\phpunit
PHPUnit 7.5.2 by Sebastian Bergmann and contributors.

.[]

If you remove the dump, then the validator swears:
vendor\bin\phpunit
PHPUnit 7.5.2 by Sebastian Bergmann and contributors.

.Illuminate\Support\MessageBag {#208
  #messages: array:2 [
    "inn" => array:1 [
      0 => "The inn field is required."
    ]
    "id" => array:1 [
      0 => "The id field is required."
    ]
  ]
  #format: ":message"
}

I conclude that the parameters are not passed, but why? How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2019-01-24
@dark1112

$response = $this->call('GET', '/getdata', [
    'query' => [
        'inn' => mt_rand(1111111111, 9999999999), 
        'id' => mt_rand(11111111, 99999999) . '_0_' . mt_rand(111111111, 999999999),
    ]
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question