2

我要做的是对我的捆绑包进行功能测试(可重复使用的捆绑包)。更深入:

  • 创建对给定 url 的请求/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怎么办?测试会失败...

带有前缀路由的测试也会失败。如果routing.xml从 AcmeMessagingBundle 导入带有前缀,testReceiveApiWorkflow将会失败!

4

1 回答 1

3

UsingWebTestCase将使用您自己AppKernel的测试环境。

您可以向您的应用程序添加一个新的环境,并WebTestCase像这样使用它:

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

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

于 2012-09-27T08:10:30.070 回答