Symfony 4测试
我想在单元测试(WebTestCase和KernelTestCase)中插入ParameterBagInterface和EntityManagerInterface,但找不到正确返回其命名空间和名称的方法(Symfony 4测试,symfony,phpunit,symfony4,Symfony,Phpunit,Symfony4,我想在单元测试(WebTestCase和KernelTestCase)中插入ParameterBagInterface和EntityManagerInterface,但找不到正确返回其命名空间和名称的方法(Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface)。有没有办法做到这一点 我尝试的是: $this->parameterBag=self::$container->get(ParameterBagI
Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface
)。有没有办法做到这一点
我尝试的是:
$this->parameterBag=self::$container->get(ParameterBagInterface::class)代码>
返回Symfony\Component\DependencyInjection\ParameterBag\ContainerBag
$this->parameterBag=$this->PropherSize(ParameterBagInterface::class)->reveal()代码>
返回Double\ParameterBagInterface\P1
$this->parameterBag=$this->createMock(ParameterBagInterface::class)代码>
返回Mock_参数bagInterface\u fccf09f9
我所有的类都使用ParameterBagInterface,并且都是这样暗示类型的
以下是示例测试类:
/**
*
* @package App\Tests\Entity
*/
class LogCollectTest extends WebTestCase
{
use CronManagerCron;
/**
* @var EntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
protected function setUp()
{
self::bootKernel();
$this->parameterBag = self::$container->get(ParameterBagInterface::class);
}
/**
* Test saving click
*/
public function testSavingClick()
{
// truncate the log collect table to be sure to get the right click
$this->truncateLogCollectTable();
$userAgents = [...];
foreach ($userAgents as $agent => $expectedResult) {
// we make fake client requests and record them in database (test enviropment)
$clientStatus = $this->sendClientData($agent);
// the controller is resulting properly
$this->assertEquals(200, $clientStatus);
/**
* @var LogCollect $logCollectEntry
*/
$logCollectEntry = $this->em->getRepository(LogCollect::class)->getLast(); <--
...
// later we process this client requests with cron and later assert the data
$logCollectorCron = new LogCollectorCron(
$this->container,
$this->em,
$this->parameterBag,
'test'
);
$logCollectorCron->run();
...
}
/**
*
*@package-App\Tests\Entity
*/
类LogCollectTest扩展WebTestCase
{
使用CronManagerCron;
/**
*@var EntityManager
*/
私人股本$em;
/**
*{@inheritardoc}
*/
受保护的函数设置()
{
self::引导内核();
$this->parameterBag=self::$container->get(ParameterBagInterface::class);
}
/**
*测试保存单击
*/
公共函数测试保存单击()
{
//截断日志收集表以确保获得右键单击
$this->truncateLogCollectTable();
$userAgents=[…];
foreach($userAgents as$agent=>$expectedResult){
//我们伪造客户机请求并将其记录在数据库中(测试环境设备)
$clientStatus=$this->sendClientData($agent);
//控制器的结果正确
$this->assertEquals(200,$clientStatus);
/**
*@var LogCollect$logCollectEntry
*/
$logCollectEntry=$this->em->getRepository(LogCollect::class)->getLast();容器,
$this->em,
$this->parameterBag,
“测试”
);
$logCollectorCron->run();
...
}
有什么建议吗?你不会得到任何接口,因为接口本质上永远无法实例化,所以要清楚:ParameterBagInterface对象不可能存在。
当您要求容器为您提供ParameterBagInterface时,容器会为您提供一个实现此接口的服务