简体   繁体   English

Symfony2 WebCaseTest中的绝对URL失败

[英]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. 如果我在浏览器中转到http://dev.example:8080 / app_dev.php / admin / ,则可以正常运行。

Its like PHPUnit cannot see that host? 就像PHPUnit 看不到那个主机?

$crawler->request() should not receive an actual URI, but the part after the front controller. $crawler->request()不应接收实际的URI,而应接收前端控制器之后的部分。 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 . 它只是通过创建一个Request对象,将其传递给AppKernel然后解析Response来模拟一个请求。 This is much faster. 这要快得多。

If you want to test using a real request, I recommend installing and using Mink or Goutte . 如果要使用实际请求进行测试,建议安装并使用MinkGoutte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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