php 通过单元测试访问 Symfony 2 容器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8972737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Symfony 2 container via Unit test?
提问by Tower
How do I access the Symfony 2 container within a Unit test? My libraries need it so it's essential.
如何在单元测试中访问 Symfony 2 容器?我的图书馆需要它,所以它是必不可少的。
The test classes extend \PHPUnit_Framework_TestCase
so there is no container.
测试类扩展\PHPUnit_Framework_TestCase
所以没有容器。
回答by Jakub Zalas
Support is now built into Symfony. See http://symfony.com/doc/master/cookbook/testing/doctrine.html
Symfony 现在内置了支持。请参阅http://symfony.com/doc/master/cookbook/testing/doctrine.html
Here's what you could do:
你可以这样做:
namespace AppBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class MyDatabaseTest extends KernelTestCase
{
private $container;
public function setUp()
{
self::bootKernel();
$this->container = self::$kernel->getContainer();
}
}
For a bit more modern and re-usable technique see https://gist.github.com/jakzal/a24467c2e57d835dcb65.
有关更现代和可重用的技术,请参阅https://gist.github.com/jakzal/a24467c2e57d835dcb65。
Note that using container in unit tests smells. Generally it means your classes depend on the whole container (whole world) and that is not good. You should rather limit your dependencies and mock them.
请注意,在单元测试中使用容器会有异味。通常,这意味着您的课程依赖于整个容器(整个世界),这并不好。您应该限制您的依赖项并模拟它们。
回答by Babou34090
You can use this , in your set up function
您可以在设置功能中使用它
protected $client;
protected $em;
/**
* PHP UNIT SETUP FOR MEMORY USAGE
* @SuppressWarnings(PHPMD.UnusedLocalVariable) crawler set instance for test.
*/
public function setUp()
{
$this->client = static::createClient(array(
'environment' => 'test',
),
array(
'HTTP_HOST' => 'host.tst',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0',
));
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
$crawler = $this->client->followRedirects();
}
Don't forget to set your teardown function
不要忘记设置你的拆卸功能
protected function tearDown()
{
$this->em->close();
unset($this->client, $this->em,);
}
回答by Tomá? Votruba
Update 2018: Since Symfony 3.4/4.0 there is a problem with service testing.
2018 年更新:自 Symfony 3.4/4.0 以来,服务测试存在问题。
It's called "testing private services", possible solutions are described here
它被称为“测试私人服务”,这里描述了可能的解决方案
For various different configs you also use lastzero/test-toolspackage.
对于各种不同的配置,您还可以使用lastzero/test-tools包。
It sets up a container for you and is ready to use:
它为您设置了一个容器并准备好使用:
use TestTools\TestCase\UnitTestCase;
class FooTest extends UnitTestCase
{
protected $foo;
public function setUp()
{
$this->foo = $this->get('foo');
}
public function testBar()
{
$result = $this->foo->bar('Pi', 2);
$this->assertEquals(3.14, $result);
}
}