简体   繁体   中英

How do I use the Symfony DomCrawler with Laravel's subdomain routing?

Currently my site is designed to serve up multiple subdomains like this:

Route::group(array('domain' => 'site1'.AppHelper::getDomain()), function(){
    Route::get('{state?}/{variant?}', array("uses" => 'Site1Controller@showIndex'));
});

Route::group(array('domain' => 'site2'.AppHelper::getDomain()), function(){
    Route::get('{state?}/{variant?}', array("uses" => 'Site2Controller@showIndex'));
});

Route::group(array('domain' => 'site3'.AppHelper::getDomain()), function(){
    Route::get('{state?}/{variant?}', array("uses" => 'Site3Controller@showIndex'));
});

Now I want to write some tests that crawl these pages checking for specific content. Unfortunately, the only documentation I can find on the Symfony DomCrawler for subdomains tells me to do this (let's say this is on line 312 of my test):

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

Unfortunately when I run PHPUnit and it gets to this test I see this:

1) SiteTest::testSite1ForContent
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:

/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1429
/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1050
/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1014
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:576
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:597
/var/www/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/var/www/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:339
/var/www/app/tests/SiteTest.php:312

So what is it that I'm not understanding here? The site1.domain.dev definitely resolves normally and has no issues that I can see. It seems to be a problem specific to trying to force the header for the subdomain.

Even apart from a solution, how do I debug this properly? I want to figure out why it's not working/where it's failing since this is my first attempt at writing tests.

I started a blank project that just tried to handle this with subdomain routes and it worked perfectly. I realized the issue had to be with what I wrote.

Turns out the issue was actually with a helper I wrote to retrieve the domain. It was trying to access $_SERVER vars that weren't available to the tests when running. Having that return a dev domain if it couldn't retrieve the values solved my issue.

Try this one:

public function refreshApplication() 
{
    parent::refreshApplication();

    $this->client = $this->createClient(
        array('HTTP_HOST' => 'site1.domain.dev'));
}

public function testSomething()
{
    $crawler = $this->client->request('GET', '/something');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM