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

vin*_*nux 6 php symfony

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

$this->client = static::createClient();
$container = $this->client->getContainer();
Run Code Online (Sandbox Code Playgroud)

Vit*_*ian 15

假设你从WebTestCase类扩展,你应该调用$this->createClient()而不是静态方法.

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

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

    $client = static::$kernel->getContainer()->get('test.client');
    $client->setServerParameters($server);
Run Code Online (Sandbox Code Playgroud)

一定要在里面创建客户端setUp()