Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Laravel testing requests

I'm working on a large Laravel app, currently on v8.45.1 which has never had tests, so I'm working to get it to a point where we can start writing unit & feature tests.

I'm hitting an issue where the two request classes (App\Core\Request and App\Core\FormRequest) both use a trait RequestTrait which holds a set of utility methods.

This obviously works fine in local/staging/production, but when I run the test suite it complains that none of the methods provided by the trait exist:

Method Illuminate\Http\Request::isFromTrustedSource does not exist.

They are being called in various places as Request::isFromTrustedSource() or request()->isFromTrustedSource().

I can imagine that when running the app in the test environment, there may be differences to the request. Is it using a different class, or does the trait not apply for some reason?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think, I found your problem - App\Core\Request extends Illuminate\Http\Request and in index.php you use App\Core\Request

The problem is in Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::call()

When you use $this->get(...) in test suite - this method bootstrap app with standard request - not with your App\Core\Request

You can override this method in base tests/TestCase.php and pass your own request.

Unfortunately, it has no contract, than you cannot work with this through $this->app->bind()

Something like this:

class TestCase extends BaseTestCase
{
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
    //other code
    $response = $kernel->handle(
        $request = \App\Core\Request::createFromBase($symfonyRequest)
    );
    //other code
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda