简体   繁体   English

在测试类Symfony2中获取容器

[英]Get container in test class Symfony2

how I can get container to be able getting mys services in tests classes. 我如何获取容器才能在测试类中获取mys服务。 I found the solution to require_once dir(__FILE__). '/...../AppKernel.php; 我找到了require_once dir(__FILE__). '/...../AppKernel.php;的解决方案require_once dir(__FILE__). '/...../AppKernel.php; require_once dir(__FILE__). '/...../AppKernel.php; , But when I go into WebTestCase class and found ,但是当我进入WebTestCase类并发现

protected static function createClient(array $options = array(), array $server = array())
{
    static::bootKernel($options);

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

    return $client;
}

Where Kernel booting, and another class KernelTestCase so I want to know I need to require_once AppKernel file or there is exist some more flexible and better method to get service container? 在哪里启动内核,以及另一个类KernelTestCase所以我想知道我需要require_once AppKernel文件,还是存在一些更灵活,更好的方法来获取服务容器? Thanks! 谢谢!

Example : 范例:

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class Test extends WebTestCase
{
   public function testCreate()
   {
      $client = static::$kernel->getContainer()->get('yourservice')
   }
}

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

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