在Symfony2 WebTestCase中访问容器,返回NULL

时间:2012-07-27 21:27:15

标签: php symfony

我正在尝试访问Symfony2 WebTestCase中的容器,我得到NULL。有任何想法吗?这是我的代码:

$this->client = static::createClient();
$container = $this->client->getContainer();

1 个答案:

答案 0 :(得分:13)

假设您从WebTestCase类延伸,则应调用$this->createClient()而不是静态方法。

如果您将此方法称为静态,则应执行boot()方法

    static::$kernel = static::createKernel($options);
    static::$kernel->boot();

    $client = static::$kernel->getContainer()->get('test.client');
    $client->setServerParameters($server);

确保setUp()

中创建客户端