简体   繁体   English

Symfony3使用phpunit测试redirectToRoute()方法

[英]Symfony3 testing the redirectToRoute() method with phpunit

I am trying to make one of the most simplest (I think) functional test by PHPUnit for a Symfony3 controller. 我正在尝试通过PHPUnit对Symfony3控制器进行最简单的(我认为)功能测试之一。

My controller code is just a redirect: 我的控制器代码只是一个重定向:

class DefaultController extends Controller
{
    public function indexAction()
    {
        return $this->redirectToRoute('admin_dashboard', array(), 301);
    }
}

and my test function is as follows: 我的测试功能如下:

class DefaultControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient(array(), array(
            'HTTP_HOST' => 'www.admin.dev',
        ));

        $client->request('GET', '/');
        $client->followRedirect();
        $this->assertTrue($client->getResponse()->isRedirect('/dashboard/'));
    }
}

The error that I get here is as follows: 我在这里得到的错误如下:

LogicException: The request was not redirected

Also some of the results that I get for some values are as follows: 另外,对于某些值,我得到的一些结果如下:

$client->getRequest()->getUri() -> "http://www.admin.dev/"
$client->getResponse()->getStatusCode() -> 404
$client->getResponse() instanceof RedirectResponse -> false
$client->getResponse()->headers->get('location') -> null

A little more background for this issue: 有关此问题的更多背景信息:

  • I have multiple domains pointing to this same project 我有多个域指向同一项目
  • The bundle is only loaded when the relative domain is asked for. 仅在要求相对域时才加载捆绑软件。

For example: 例如:

www.admin.dev -> admin bundle
www.admin2.dev -> admin2 bundle
www.admin3.dev -> admin3 bundle

Any ideas what am I doing wrong that I am stuck with this simple problem? 有什么想法让我困扰于这个简单的问题吗?

My routing configuration is: 我的路由配置是:

homepage:
    host: "www.%domain%"
    path:     /
    defaults: { _controller: MyBundle:Default:index}

admin_dashboard:
    host: "www.%domain%"
    prefix:   /dashboard
    path:     /
    defaults: { _controller: MyBundle:Dashboard:index}

And

%domain% is a parameter in the parameter file. The value is 'admin.dev'

You are try this . 您正在尝试这个。 it is work. 这是工作。

$host="www.admin.dev" // $host= "http://127.1.1.2(local ip)"

public function testIndex()
{
    $client = static::createClient(array(), array(
        'HTTP_HOST' => $host,
    ));
    $client->request('GET', '/forgetpass');
    $this->assertEquals(true,$client->getResponse()->isRedirect('/dashboard'));
}

At last I solved it. 最后我解决了。 Thanks to ccKep for pointing me to the right direction. 感谢ccKep为我指出正确的方向。 All my domains had their own environment and hence, all the routes were not available. 我所有的域都有自己的环境,因此,所有路由都不可用。 Like: 喜欢:

www.admin.dev -> loads routes for only admin bundle
www.admin2.dev -> loads routes for only admin2 bundle
www.admin3.dev -> loads routes for only admin3 bundle

So, to solve the problem I first debug the routes for the 'test' environment (as this environment is responsible for the tests) by calling: 因此,要解决该问题,我首先通过调用以下命令来调试“测试”环境的路由(因为该环境负责测试):

php bin/console debug:router --env=test

and found that it only includes the generic routes and not the domain specific routes. 并发现它仅包含通用路由,而不包含域特定路由。 So, to have the routes to be included in my test environment, I included the routing information of my domain into my test routing file. 因此,为了将路由包含在测试环境中,我将域的路由信息​​包含在测试路由文件中。

For those who are still curious about my strange configuration: 对于仍然对我的奇怪配置感到好奇的人:

I have 3 domains pointing to my project: 我有3个域指向我的项目:

www.admin.dev
www.admin2.dev
www.admin3.dev

Depending on this domains, the bundles are loaded as: 根据此域的不同,捆绑软件的加载方式为:

www.admin.dev  -> loads MyAdminBundle
www.admin2.dev -> loads MyAdmin2Bundle
www.admin3.dev -> loads MyAdmin3Bundle

Each of my domains have 2 environment of them as: 我的每个域都有2个环境,分别是:

www.admin.dev  -> dev_admin  and prod_admin
www.admin2.dev -> dev_admin2 and prod_admin2
www.admin3.dev -> dev_admin3 and prod_admin3

And based on these domains and their environment, I have my configuration and routing defined so that when www.domain.dev is called, it uses the configuration, routing and the respective bundle based on its environment and nothing of the MyAdmin2Bundle or MyAdmin3Bundle or any of their routes or configuration. 根据这些域及其环境,我定义了我的配置和路由,以便在调用www.domain.dev时,它将根据其环境使用配置,路由以及相应的捆绑软件,而MyAdmin2Bundle或MyAdmin3Bundle或任何他们的路线或配置。

To make it a bit more clear, I have configuration files like: 为了更清楚一点,我有如下配置文件:

config.yml
config_dev.yml
config_dev_domain.yml
config_dev_domain2.yml
config_dev_domain3.yml
config_prod.yml
config_prod_domain.yml
config_prod_domain2.yml
config_prod_domain3.yml
config_test.yml

And routing files like: 和路由文件,如:

routing.yml
routing_dev.yml
routing_dev_domain.yml
routing_dev_domain2.yml
routing_dev_domain3.yml
routing_prod.yml
routing_prod_domain.yml
routing_prod_domain2.yml
routing_prod_domain3.yml
routing_test.yml

Now what I have done it, just included the routing files of domain, domain2 and domain3 into the routing_test.yml . 现在,我所做的只是将domain,domain2和routing_test.yml的路由文件包含到routing_test.yml I hope this is enough to understand. 我希望这足以理解。

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

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