2012-05-20 8 views
6

Ho un'applicazione che utilizza i sottodomini di rotta alle agenzie:Come faccio a testare le rotte del sottodominio con sede a Symfony2

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

Questi ciascuna corrispondere ad un'entità Agenzia e controller.

Ho un listener che ascolta l'evento onDomainParse e scrive il sottodominio negli attributi della richiesta.

/** 
* 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']); 
     } 

    } 
    //... 
} 

Ho quindi utilizzare questo controller di reindirizzare ad un'azione di spettacolo:

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 
     )); 
    } 
    // ... 

} 

La mia domanda è:

Come faccio a simulare questo quando si fa un test con WebTestCase?

risposta

7

Visita il sottodominio sovrascrivendo intestazioni HTTP per la richiesta e il test per giusta pagina:

testato, può contenere degli errori

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()); 
    } 
} 
+0

Ah era nella [docs] (http://symfony.com/doc/current/book/testing.html#testing-configuration) pure. Grazie – max

7

Sulla base dei documenti Symfony sulle rotte basati su host, Testing your Controllers:

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

Se non si desidera per riempire tutte le vostre richieste con parametri array, t il suo potrebbe essere migliore:

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

... 

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

C'è anche un metodo setServerParameters() sul client, se si dispone di alcuni parametri per cambiare.

Problemi correlati