Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test subdomain based routes in Symfony2

I have a app which uses subdomains to route to agencies:

foo.domain.dev -> Agency:showAction(foo)
bar.domain.dev -> Agency:showAction(bar)
domain.dev     -> Agency:indexAction()

These each correspond to an Agency entity and controller.

I have a listener that listens for the onDomainParse event and writes the subdomain into the request attributes.

/**
* Listens for on domainParse event
* Writes to request attributes
*/
class SubdomainListener {
   public function onDomainParse(Event $event)
   {
       $request = $event->getRequest();
       $session = $request->getSession();
       // Split the host name into tokens
       $tokens = $this->tokenizeHost($request->getHost());

       if (isset($tokens['subdomain'])){
           $request->attributes->set('_subdomain',$tokens['subdomain']);
       }

   }
   //...
 }

I then use this in the controller to reroute to a show action:

class AgencyController extends Controller
{

    /**
     * Lists all Agency entities.
     *
     */
    public function indexAction()
    {
        // We reroute to show action here.
        $subdomain = $this->getRequest()
                        ->attributes
                        ->get('_subdomain');
        if ($subdomain)
            return $this->showAction($subdomain);


        $em = $this->getDoctrine()->getEntityManager();

        $agencies = $em->getRepository('NordRvisnCoreBundle:Agency')->findAll();

        return $this->render('NordRvisnCoreBundle:Agency:index.html.twig', array(
            'agencies' => $agencies
        ));
    }
    // ...

}

My question is:

How do i fake this when doing a test with WebTestCase?

like image 813
max Avatar asked May 20 '12 21:05

max


2 Answers

Visit the subdomain by overriding HTTP headers for the request and test for the right page:

Untested, may contain errors

class AgencyControllerTest extends WebTestCase
{
    public function testShowFoo()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/', array(), array(), array(
            'HTTP_HOST'       => 'foo.domain.dev',
            'HTTP_USER_AGENT' => 'Symfony/2.0',
        ));

        $this->assertGreaterThan(0, $crawler->filter('html:contains("Text of foo domain")')->count());
    }
}
like image 165
ilanco Avatar answered Oct 22 '22 18:10

ilanco


Based on the Symfony docs on host-based routes, Testing your Controllers:

$crawler = $client->request(
    'GET',
    '/',
    array(),
    array(),
    array('HTTP_HOST' => 'foo.domain.dev')
);

If you don't want to pad all your requests with array parameters, this might be better:

$client->setServerParameter('HTTP_HOST', 'foo.domain.dev');
$crawler = $client->request('GET', '/');

...

$crawler2 = $client->request('GET', /foo'); // still sends the HTTP_HOST

There's also a setServerParameters() method on the client, if you have a few parameters to change.

like image 44
fazy Avatar answered Oct 22 '22 17:10

fazy