2

I am using an extended Request class in Laravel 5. I followed this post to setup the custom request class. When I run my code through a browser it works.

However, when I run my integration tests (with PHPUnit), my extended class is not being used by the call method in CrawlerTrait.php, because the call method creates a new Illuminate\Http\Request object. I tried to override the call method, with this:

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
    $this->currentUri = $this->prepareUrlForRequest($uri);

    $request = ApiRequest::create(
        $this->currentUri, $method, $parameters,
        $cookies, $files, $server, $content
    );

    return $this->response = $this->app->make('Illuminate\Contracts\Http\Kernel')->handle($request);
}

The ApiRequest class is my extended request class.

use Illuminate\Http\Request; 

class ApiRequest extends Request {

public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
    parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);

    // other code here
}

The ApiRequest class is injected into the ApiController constructor.

abstract class ApiController extends Controller
{
  public function __construct(ApiRequest $request, ApiResponse $response)
    {

    }
}

At the point where the ApiRequest object is injected into ApiController, it is not the same instance of the object created in the call method.It seems to create a new ApiRequest object when injecting it. Consequently, the request object is empty and my tests fail.

How can I get my tests to work with my extended Request class?

Community
  • 1
  • 1

0 Answers0