0

So in my application I've some routes that need special treatment which is why I have something like this:

Route::prefix('external')->group(function () {
    Route::any(Request::path(), 'ExternalController@handle');
});

Basically every route which is /api/external* goes into that part.

Somehow, when I run my feature tests, Request::path() or request()->path() (doesn't matter) is always /

I also tried $_SERVER but this isnt even defined at this point.

My Test looks something like this:

public function testExternal()
{
    $response = $this
        ->put($this->externalUrl . '/' . $some['id'], [
            'data' => []
        ]);
    
    // ...
}

So I've read some articles here on stackoverflow and what I already tried was this:

    $response = $this
        ->withServerVariables(['REQUEST_URI' => $this->externalUrl . '/' . $some['id']])
        ->put($this->externalUrl . '/' . $some['id'], [
            'data' => []
        ]);

and also this:

    Request::shouldReceive('path')
        ->once()
        ->andReturn($this->externalUrl . '/' . $some['id']);

but this doesn't work at all, I just get errors like this:

Received Mockery_2_Illuminate_Http_Request::setUserResolver(), but no expectations were specified

And even tho I try to set an expectation for this, it just doesn't work.

So what I'm looking for is to either Mock $_SERVER or Request::path or to somehow figure out, why Request::path is always just /.

Thanks in advance.

suben
  • 43
  • 4
  • No, you have to use the first thing you showed us, `put`, you do not have to mock anything, BUT what is `externalUrl` ? What is its value ? – matiaslauriti Jun 30 '21 at 20:07
  • It's ``/api/external/lookup/1`` So basically the right path – suben Jun 30 '21 at 20:44
  • It's funny, if I use this: ``Route::any('external/lookup/{id}', function () { dd(request()->server('REQUEST_URI')); });`` this works – suben Jun 30 '21 at 20:50
  • Can you look at the documentation as I am on a phone trying to help you ? On the routing documentation there is a way of doing what you want without using `Request::path()` but instead something about a `match` method, that would do it for you. – matiaslauriti Jun 30 '21 at 23:00
  • In general mocking static functions is a pain and i'm not even sure what you are trying to test? create your routes and create feature tests for those. – mrhn Jul 04 '21 at 19:48

0 Answers0