Search code examples
phpsymfonyphpunitfunctional-testing

Absolute URL fails in Symfony2 WebCaseTest


The following code always results in no Route found. But the Route does exist.

$client = static::createClient();
$crawler = $client->request(
   'GET', 
   '/app_dev.php/admin/', 
   array(), 
   array(), 
   array("HTTP_HOST" => "dev.example:8080"));

But always fails. If I go to http://dev.example:8080/app_dev.php/admin/ in my browser then it works fine.

Its like PHPUnit cannot see that host?


Solution

  • $crawler->request() should not receive an actual URI, but the part after the front controller. So in your case, use:

    $client = static::createClient();
    $crawler = $client->request(
       'GET', 
       '/admin/', 
       array(), 
       array(), 
       array("HTTP_HOST" => "dev.example:8080"));
    

    The reason behind this is that the client doesn't actually request for a page (to a host). It just simulates a request, by creating a Request object, passing it to the AppKernel and then parsing the Response. This is much faster.

    If you want to test using a real request, I recommend installing and using Mink or Goutte.