WebTestCase, Silex and $_GET

298 views Asked by At

I'm experiencing some issues on a WebTestCase using Silex: on one of my controller's action, I need a parameter passed through a normal $_GET (I have to as it's an URL, and Apaches interprets the %2F if it's outside of the query string -- see Url Variables with %2f not handled by silex for example)

Here's how my route is defined:

$controller
   ->get('/get', <controller action>)
   ->value('url', (!empty($_GET['url']) ? $_GET['url'] : false));

It works fine in browser, but it doesn't seem to be working inside a WebTestCase like this one: $_GET stays empty...

$client = $this->createClient();
$client->request('GET', '/get?url=' . urlencode($url));

edit

I just did a quick experiment: if I do the following in my route:

$controller
        ->get('/get/{url}', <action>)
        ->assert('url', '.*');

And this in the test:

$client = $this->createClient();
$client->request('GET', '/get/' . urlencode($url));

Everything if fine, $url gets passed to the controller... but well, it doesn't work on the browser anymore as it passes through Apache.

1

There are 1 answers

1
Catalin Trandafir On BEST ANSWER

The server globals (like $_GET) are populated by Apache. When running the functional test, Apache is skipped so $_GET is not populated anymore. Instead of using the server globals, you should use the Request object to extract the GET parameters. This way, the framework will intercept both the PHPUnit injected variables and the Apache injected variables; it will then make them available in your action method via a Request object that can be injected as a function parameter.

Sample how to extract the url parameter:

$url = $request->query->get('url');