Php 如何将Symfony DomCrawler与Laravel';s子域路由?
目前,我的网站设计用于提供以下多个子域:Php 如何将Symfony DomCrawler与Laravel';s子域路由?,php,unit-testing,symfony,laravel,phpunit,Php,Unit Testing,Symfony,Laravel,Phpunit,目前,我的网站设计用于提供以下多个子域: Route::group(array('domain' => 'site1'.AppHelper::getDomain()), function(){ Route::get('{state?}/{variant?}', array("uses" => 'Site1Controller@showIndex')); }); Route::group(array('domain' => 'site2'.AppHelper::getD
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'));
});
现在我想编写一些测试,对这些页面进行爬网,检查特定内容。不幸的是,我在Symfony DomCrawler上找到的唯一一个子域命令我这样做(假设这是在我测试的第312行):
不幸的是,当我运行PHPUnit并进行此测试时,我看到:
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
我在这里不明白的是什么?site1.domain.dev肯定能正常解决问题,而且我看不到任何问题。这似乎是一个特定于试图强制子域的头的问题
即使除了解决方案之外,我如何正确地调试它?因为这是我第一次尝试编写测试,所以我想弄清楚它为什么不工作/哪里失败了。我启动了一个空白项目,只是尝试用子域路由来处理这个问题,它工作得很好。我意识到问题在于我写的东西 事实证明,问题实际上与我为检索域而编写的助手有关。它试图访问运行时测试不可用的$\u服务器变量。如果无法检索到值,则返回一个dev域,解决了我的问题。试试这个:
public function refreshApplication()
{
parent::refreshApplication();
$this->client = $this->createClient(
array('HTTP_HOST' => 'site1.domain.dev'));
}
public function testSomething()
{
$crawler = $this->client->request('GET', '/something');
}
我不明白。你有willcard建立的子域吗?为什么不设置第二个参数fixed
http://site1.domain.dev/
而不是/
?嗯,子域被声明为路由组和{state?}/{variant?}实际路线的一部分是处理类似site1.domain.dev/TX/a的事情,这意味着它是本地化到德克萨斯州的site1站点,并使用站点的a变体(a/B测试)。据我所知,这些都不是问题,因为它们都是可选的。转到site1.domain.dev/仍将匹配该模式,并且在手动访问时会匹配。只是一个问题,当试图创建一个DomCrawler重新阅读您的评论时,您似乎在谈论实际的DomCrawler。我以前曾尝试将其定义为$crawler=$this->client->request('GET'),http://site1.domain.dev/');代码>但这给了我同样的错误。我不明白为什么会这样。您确定要通过$this->$client
实现这一点吗?您是否将对象名称保留在变量中?您可能发现了我后来指出的一个输入错误。这似乎是Laravel中子域组特有的问题。如果我改为使用$crawler=$this->client->request('GET','/')
并创建一个非基于组的路由关闭测试工作正常,可以爬网。它只在解决子域问题时遇到问题。这可能是我必须去拉威尔IRC的原因,但我会继续努力解决它。
public function refreshApplication()
{
parent::refreshApplication();
$this->client = $this->createClient(
array('HTTP_HOST' => 'site1.domain.dev'));
}
public function testSomething()
{
$crawler = $this->client->request('GET', '/something');
}