当WebTestCase用于功能测试捆绑包时指定配置?

问题描述 投票:2回答:1

我正在尝试做的是功能测试我的捆绑(一个可重用的)。更深入:

  • 创建对给定网址/my/url的请求
  • 检查是否调用了MyParamConverter并将请求转换为MyObject的实例
  • 检查控制器是否抛出my.event

根据文档,我应该扩展Symfony\Bundle\FrameworkBundle\Test\WebTestCase并创建一个新客户端:

    $client  = static::createClient();
    $crawler = $client->request('GET', '/my/url');

这样做,哪些包被加载?如何指定要在环境中使用的配置文件(假设它默认为test)?

编辑:好的,时间更好地解释我的问题。 AcmeMessagingBundle说,我正在拧一个可重复使用的捆绑包。现在我想进行功能测试。场景是对/my/url的调用:

public function testReceiveApiRoute()
{
    $client = $this->createClient();

    /** @var $route \Symfony\Component\Routing\Route */
    $route  = $client->getContainer()->get('router')
        ->getRouteCollection()->get('acme_messaging_receive');

    $this->assertNotNull($route);
    $this->assertEquals('POST', $route->getRequirement('_method'));
    $this->assertEquals('acme_messaging.controller.api:receive',
        $route->getDefault('_controller'));
}

/**
 * @depends testReceiveApiRoute
 */
public funcion testReceiveApiWorkflow()
{
    $client = $this->createClient();

    // Make a POST request
    $request = Request::create('/my/route', 'POST', array(
        'a' => 'value'
    ));

    // Request is convered in MyObject instance and that my.event is fired
}

通过此测试,加载了app/config_test.yml(例如“主配置文件”)。问题是:

不应该测试是“隔离”,意味着不使用主配置文件?如果我的包被另一个空app/config_test.yml的人测试怎么办?测试会失败......

使用前缀路径测试也将失败。如果使用前缀导入AcmeMessagingBundle中的routing.xmltestReceiveApiWorkflow将失败!

testing symfony bundle functional-testing
1个回答
3
投票

使用WebTestCase将在测试环境中使用您自己的AppKernel

您可以为您的应用添加新的环境,并在WebTestCase中使用它,如下所示:

$client = static::createClient(array('environment' => 'new_env'));

更安全的做法是在捆绑包的测试中创建一个沙盒应用程序。您可以使用JMSCommandBundle为您生成它。您还可以使用以下技巧创建查看包的sanboxed应用程序:https://github.com/schmittjoh/JMSPaymentCoreBundle/tree/master/Tests/Functional

© www.soinside.com 2019 - 2024. All rights reserved.