Summer Sale! All accounts are 50% off this week.

tylernathanreed's avatar

PHPUnit Missing $_SERVER['REQUEST_URI']

I use the $_SERVER['REQUEST_URI'] variable in the Footer of my Website, which displays on all pages.

I'm using this variable for a reporting system, so that when a link is clicked on, I know where the User came from, and thus know what page they initially clicked "Report Error" on.

This feature works just fine. However, when I'm running PHPUnit, I discovered that the 'REQUEST_URI' index does not exist within the $_SERVER variable.

Seeing as I'd like to be able to test my Reporting System, how can I mock this address? Is there a better way of determining this address?

I don't want to hard-code it, because redirects may break it. I don't want to just ignore the variable, because then I can't test Reporting within my Application.

0 likes
7 replies
tylernathanreed's avatar

Well, I'm using the Laravel Framework. Laracasts also covers PHPUnit both inside Laravel and out.

bobbybouwmann's avatar
Level 88

If you use Laravel you shouldn't be using the $_SERVER variables at all. If you want that kind of stuff you can use the following

$uri = Request::server('REQUEST_URI');

However you can also get the last requested page much simpler

$url = URL::previous();
3 likes
Punksolid's avatar

@tylernathanreed how did you do it?, I was using $_SERVER too, but \Request::server('REQUEST_URI') just give me '/', that just happens when using PHPUnit, accessing from the browser or curl it works as expected. You were trying to access also that variable from a Global Scope?

Thanks

2 likes
jeffpuckett's avatar

This still needs an answer for mocking purposes in phpunit because the selected answer merely sidesteps the question. Everything returned by Request::server in phpunit is empty.

2 likes

Please or to participate in this conversation.