0

Using Laravel framework and it's REPL named Tinker in my project, I want to set the request object to the same state it would be if I made some real HTTP request through my browser.

When I dump the request using

 dd($request);

I receive a lot of data in $request like headers, form input data, and so on.

I want to receive the same data in $request in Tinker REPL.

How can I emulate HTTP request in Tinker from the command line?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Ilya Kolesnikov
  • 623
  • 8
  • 17

3 Answers3

0

You should be able to instantiate a request object and then use replace to get some input data into it. Something like this should work in tinker...

>> $r = new Illuminate\Foundation\Http\FormRequest()

>> $r->replace(['yada' => 'bla bla bla'])

>> $r->yada

That should output bla bla bla.

Tarek Adam
  • 3,387
  • 3
  • 27
  • 52
  • thank you, I know that I can edit any object in PHP, but the question is that I want to fill this object not with any data, but with real data or data looks like real, and to made it automatically, using some functionality of framework that is used when framework is bootstraped from real page request It's really a lot of data in Request object if you'll look it's content from debugger or dd() – Ilya Kolesnikov Mar 18 '19 at 17:57
  • @IlyaKolesnikov I don't understand how you expect a HTTP request to come into a Laravel command line app. – keyboardSmasher Mar 18 '19 at 19:12
  • @keyboardSmasher, I don't know, just finding how it could be. As I suppose, it could be these cases: 1) - to set _SERVER and so on variables and initiate Request 2) - to use some create method of Request and to send some initial values to it like url and so on – Ilya Kolesnikov Mar 18 '19 at 21:04
  • @keyboardSmasher, when I try to do `$r = new Illuminate\Foundation\Http\FormRequest()` it gives an error: "Unexpected ErrorException thrown from a caster: Undefined index: " – Ilya Kolesnikov Mar 18 '19 at 21:12
  • @keyboardSmasher, I found static method of FormRequest - create($url, bla-bla) Think it should create and fill Request depending on input params. But error described below doesn't allow to do it – Ilya Kolesnikov Mar 18 '19 at 21:19
  • @IlyaKolesnikov what laravel version are you on? – Tarek Adam Mar 18 '19 at 22:35
  • @TarekAdam, I have tried in 5.5 and 5.7. Actually I found that this error is not taking effect at usage of created object and it works and it filled by data as I wanted to be – Ilya Kolesnikov Mar 19 '19 at 07:12
  • But now I have one more question about it: Can I put this Request object to somewhere in app for another components could see it when they try to attach it like `Request $request` or some other way from the app? – Ilya Kolesnikov Mar 19 '19 at 07:14
  • @IlyaKolesnikov You can use Request::create([..route..,..get/post..]) and Route::dispatch($request) within the app. – Tarek Adam Mar 19 '19 at 15:30
  • @TarekAdam, yes, it was my answer, but it will not set this object in app context, so some third module which will connect Request by DI will not have same data on it, but I want to have an ability to do it – Ilya Kolesnikov Mar 20 '19 at 11:56
0

Request class has some set of methods to initiate it with names begins from create... And the create method allow to initiate it with manually gived params like url, method and additional optional parameters:

Illuminate\Foundation\Http\FormRequest::create('http://your-url', 'METHOD', ...)

so you can use it from REPL to play with your controllers and initiate them like you come from some route

Ilya Kolesnikov
  • 623
  • 8
  • 17
0

I know this question is pretty old but I recently wanted to test a controller POST and found it difficult to find an answer, so here is my solution:

I created a formRequest class which I wanted to test in Tinker. The class looks something like this:

    <?php

namespace App\Http\Requests;

use App\Models\Task;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class TaskRequest extends FormRequest
{
private $_validator;
private $_task;
/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
// public function authorize()
// {
//     return false;
// }

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'task_name' => ['required', 'max:100'],
        'task_description' => ['nullable', 'max:1024'],
        'due_date' => ['nullable', 'max:50','after:today'],
        // ... and more
    ];
}

public function validator() 
{
    return $this->_validator ?? $this->_validator = Validator::make($this->sanitize(), $this->rules());
}

/**
* Convert any of your incoming variables, such as dates to required format
**/
public function sanitize()
{
    // get request vars
    $vars = $this->all();
    if ( !empty($vars['due_date']) )
    {
        $date = new Carbon($vars['due_date'] );
        $vars['due_date'] = $date->toDateTimeString();
    }
    return $vars;
}


public function save()
{
    if ( $this->validator()->fails() )
        return false;

    return $this->_task = Task::create(
       $this->validator()->validated()
    );
}

In tinker:

 $form = new App\Http\Requests\TaskRequest();
 $form->merge(['task_name'=>'test task','due_date'=>'2022-04-22Z15:45:13UTC']);
 $form->validator()->validated(); 


=> [
 "task_name" => "test task",
 "due_date" => "2022-04-22 15:45:13"
] 
ChrisB
  • 611
  • 11
  • 24